//算法6.4 P131 有改动 //按先序次序输入二叉树中结点的值,构造二叉链表表示的二叉树T void createBiTree(BiTreeLink *T){ TElemType ch; scanf("%c",&ch); fflush(stdin); if(ch==Nil||ch=='#'){//空 *T = NULL; }else{ *T = (BiTreeLink)malloc(sizeof(BiTNode)); if(!*T){ exit(0); } (*T)->data = ch;//生成根结点 createBiTree(&(*T)->lchild);//构造左子树 createBiTree(&(*T)->rchild);//构造右子树 } }
//按先序次序输入二叉树中结点的值(一个字符),点号字符表示空树,构造二叉链表表示的二叉树T void createBiTree(BiTree &T, int &root, int &i){ TElemType e; scanf("%c", &e); if(e!='#'){ if(e!='.'){//输入的当前节点不是“空树”,是实结点 T[i].data = e; //T[i].llink = 0; //T[i].rlink = 0; root = i; createBiTree(T, T[root].llink, ++i); createBiTree(T, T[root].rlink, ++i); }else{ root = 0;//输入的当前节点是“空树”,是虚结点,则对应指向它的链为0 i--;//静态链表下标回退,因为空树没有进表,只有实节点进表了 } } }
/** * 构造二叉树 */ void createBiTree(BiTree *T) { TElemType ch; //scanf("%c", &ch); ch = arr[i++]; if(ch=='#') *T = NULL; else { *T = (BiTree)malloc(sizeof(BiTNode)); if(!*T) exit(-1); (*T)->data = ch; createBiTree(& (*T)->lchild); createBiTree(& (*T)->rchild); } }
int main() { BiTree T; //printf("input former sequence traversal, like:\nab#d##c##\n"); createBiTree(&T); inOrderTraverse(T); printf("\n"); return 0; }
void main(){ BiTree tree; int root = 1;//根节点的位置 printf("请按先序次序输入二叉树各节点以#号结束,空树用点号代替:\n"); int pos = 1;//控制加入静态数组的位置 createBiTree(tree, root, pos); printf("先序遍历打印二叉树(递归算法):\n"); preOrderPrint(tree, root); printf("\n"); printf("先序遍历打印二叉树(非递归算法):\n"); preOrderPrint2(tree, root); printf("\n"); printf("中序遍历打印二叉树(递归算法):\n"); inOrderPrint(tree, root); printf("\n"); printf("中序遍历打印二叉树(非递归算法):\n"); inOrderPrint2(tree, root); printf("\n"); printf("后序遍历打印二叉树(递归算法):\n"); postOrderPrint(tree, root); printf("\n"); printf("后序遍历打印二叉树(非递归算法):\n"); postOrderPrint2(tree, root); printf("\n"); printf("按层次遍历打印二叉树(非递归算法):\n"); hierarchicalTraversePrint(tree, root); printf("\n"); int depth = getBiTreeDepth(tree, root); printf("该二叉树的深度为:%d\n", depth); int size = getBiTreeSize(tree, root); printf("该二叉树的结点数为:%d\n", size); int leafNodesNum = getBiTreeLeafNodesNum(tree, root); printf("该二叉树的叶子结点数为:%d\n", leafNodesNum); }
void main(){ int i; BiTreeLink T,p,c; TElemType e1,e2; initBiTree(&T); //判断数是否为空,求树的深度 printf("\nInit a Binary Tree!\nThe tree is empty or not?%d(1:yes 0:no);The tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T)); //寻找根结点 e1 = root(T); if(e1!=Nil) printf("\nThe root of the Binary Tree is:'%c'\n",e1); else printf("\nThe Binary Tres has no root\n"); //请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树) printf("\nBuild a Binary Tree!like abc@@de@g@@f@@hi@j@@k@@,@stand for space\n"); createBiTree(&T); //判断数是否为空,求树的深度 printf("\nThe tree is empty or not?%d(1:yes 0:no);The tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T)); //寻找根结点 e1 = root(T); if(e1!=Nil) printf("\nThe root of the Binary Tree is:'%c'\n",e1); else printf("\nThe Binary Tres has no root\n"); //层序递归遍历(Of sequence to traverse the binary tree) printf("\nlevel order Traverse the Binary Tree:\n"); levelOrderTraverse(T,visitT); //先序遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); //中序递归遍历 printf("\n\ninOrder recursion Traverse the Binary Tree:\n"); inOrderTraverse(T,visitT); //中序非递归遍历1 printf("\ninOrder non-recursion Traverse the Binary Tree 1:\n"); inOrderTraverse1(T,visitT); //中序非递归遍历2 printf("inOrder non-recursion Traverse the Binary Tree 2:\n"); inOrderTraverse2(T,visitT); //后序递归遍历 printf("\npostOrder recursion Traverse the Binary Tree:\n"); postOrderTraverse(T,visitT); //修改结点 e1 = 'd';//原值 //scanf("%c",&e1); p = point(T,e1);//获得e1对应的指针 //获得对应结点的值 printf("\n\nKnow the previous vaule is:'%c'\nEnter the new vlaue:\n",value(p)); e2 = 'r';//新结点的值 //scanf("%c",&e2); assign(p,e2);//赋新值 //先序遍历 printf("preOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); //寻找双亲 e1 = parent(T,e2); if(e1!=Nil) printf("\n\nthe parent of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no parent\n",e2); //寻找左孩子 e1 = leftChild(T,e2); if(e1!=Nil) printf("\nthe left child of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no left child\n",e2); //寻找右孩子 e1 = rightChild(T,e2); if(e1!=Nil) printf("\nthe right child of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no right child\n",e2); //寻找左兄弟 e1 = leftSibling(T,e2); if(e1!=Nil) printf("\nthe left sibling of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no left sibling\n",e2); //寻找右兄弟 e1 = rightSibling(T,e2); if(e1!=Nil) printf("\nthe right sibiling of '%c' is : '%c'\n",e2,e1); else printf("'%c' has no right sibiling\n",e2); //初始化需要插入的树 initBiTree(&c);//s=jk //这里有三个空格 printf("\nBuild the Binary Tree c which has no right child:\n"); c = (BiTreeLink)malloc(sizeof(BiTNode)); p = (BiTreeLink)malloc(sizeof(BiTNode)); c->lchild = p; c->rchild = NULL; c->data = 'm'; p->lchild = p->rchild = NULL; p->data = 'n'; //createBiTree(&c); //先序递归遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(c,visitT); //树s插到树T中,请输入树T中树s的双亲结点 s为左(0)或右(1)子树: printf("\n\nInsert the Tree s to the Tree T,enter the parent of the Tree c in the Tree T,left Tree(0) and right Tree(1):\n"); e1= 'b';i = 0;//将子树c作为结点'b'的左子树 //scanf("%c%d",&e1,&i); p = point(T,e1);//p是T中树c的双亲结点指针 insertChild(p,i,c); //先序递归遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); // 删除子树,请输入待删除子树根结点 左(0)或右(1)子树 printf("\n\nDelete the Tree s,enter the root of the deleting Child Tree, left Tree(0) and right Tree(1):\n"); e1= 'b';i = 1;//删除父结点为'b'的右子树 p = point(T,e1);//p是T中树c的双亲结点指针 deleteChild(p,i); //先序递归遍历 printf("\npreOrder Traverse the Binary Tree:\n"); preOrderTraverse(T,visitT); //清空子树 clearBiTree(&T); printf("\n\nEmpty the Binary Tree?%d(1:yes 0:no)\nThe tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T)); //寻找根结点 e1 = root(T); if(e1!=Nil) printf("\nThe root of the Binary Tree is:'%c'\n",e1); else printf("\nThe Binary Tres has no root\n"); }