void MaxHeapify(int *pnArr, int nLen, int i) { int LChild = LeftChild(i); int RChild = RightChild(i); int nMaxPos; if (LChild < nLen && pnArr[LChild] > pnArr[i]) { nMaxPos = LChild; } else { nMaxPos = i; } if (RChild < nLen && pnArr[RChild] > pnArr[nMaxPos]) { nMaxPos = RChild; } if (nMaxPos != i) { Swap(&pnArr[nMaxPos], &pnArr[i]); MaxHeapify(pnArr, nLen,nMaxPos); } }
int main() { Status i; int j; position p; TElemType e; TElemType s; InitBiTree( T ); CreateBiTree( T ); printf("After initializing the Tree, is the Tree empty? Yes:1, No:0, the depth is: %d\n", BiTreeEmpty( T ), BiTreeDepth(T)); i = Root( T, &e ); if( i ) printf("The root of the tree is: %d\n", e); else printf("The tree is empty!\n"); printf("Traverse_1:\n"); LevelOrderTraverse( T, visit ); printf("Traverse_2:\n"); InOrderTraverse( T, visit ); printf("Traverse_3:\n"); PostOrderTraverse( T, visit ); printf("input the level number to be modified \n"); scanf(" %d%d", &p.level, &p.order); e = Value( T, p); printf("The old value is %d, input new value: ", e); scanf(" %d", &e); Assign( T, p, e); printf("Traverse_1:\n"); PreOrderTraverse( T, visit ); printf("The parent of node %d is %d, left and right children are: ", e, Parent(T, e)); printf("%d, %d, left and rignt brothers are:", LeftChild(T, e), RightChild(T, e)); printf("%d, %d\n", LeftSibling(T, e), RightSibling(T, e)); InitBiTree( s ); printf("Initializing a Tree that has empty right subtree:\n"); CreateBiTree( s ); printf("The tree s insert to the tree T, input the parent node of s, s is left subtree or right subtree."); scanf(" %d%d%d", &p.level, &p.order, &j); DeleteChild( T, p, j); Print( T ); clearBiTRee( T ); printf("After clearing the tree, is the tree empty? Yes:1, No:0 %d\n", BiTreeEmpty( T ), BiTreeDepth(T)); i = Root( T, &e ); if( i ) printf("The root of the bitree is %d\n", e); else printf("The tree is empty, no root!\n"); }
void PercDown(ElementType A[], int i, int N) { int Child; ElementType Tmp; for (Tmp = A[i]; LeftChild(i) < N; i = Child) { Child = LeftChild(i); if (Child != N - 1 && A[Child+1] > A[Child]) { Child++; } if (Tmp < A[Child]) { A[i] = A[Child]; } else { break; } } }
Node * LeftSibling(BinaryTree * binaryTree,Node * e){ if(Parent(binaryTree,e)){ return LeftChild(binaryTree,Parent(binaryTree,e)); } return NULL; }
void PercDown( ElementType A[ ], int i, int N ) { int Child; ElementType Tmp; /* 1*/ for( Tmp = A[ i ]; LeftChild( i ) < N; i = Child ) { /* 2*/ Child = LeftChild( i ); /* 3*/ if( Child != N - 1 && A[ Child + 1 ] > A[ Child ] ) /* 4*/ Child++; /* 5*/ if( Tmp < A[ Child ] ) /* 6*/ A[ i ] = A[ Child ]; else /* 7*/ break; } /* 8*/ A[ i ] =Tmp; }
void test_LeftChild(void) { BINARY_TREE_TYPE tree = get_test_tree( "1, 2, 3, , 5, 6, , , , 51, 52, 61, 62, , "); if (tree == NULL) return; BINARY_TREE_NODE *node, *left_child; node = get_node(tree, 1); left_child = LeftChild(tree, node); assert_node_equal(left_child, 2); node = get_node(tree, 3); left_child = LeftChild(tree, node); assert_node_equal(left_child, 6); node = get_node(tree, 2); left_child = LeftChild(tree, node); CU_ASSERT_PTR_NULL(left_child); }
void main() { TElemType e; SqBiTree T; InitBiTree(T); CreateBiTree(T); printf("请输入待查询结点的值:"); scanf("%d",&e); printf("结点%d的双亲为%d,左右孩子分别为",e,Parent(T,e)); printf("%d,%d,左右兄弟分别为",LeftChild(T,e),RightChild(T,e)); printf("%d,%d\n",LeftSibling(T,e),RightSibling(T,e)); }
__checkReturn const TYPE* GetLowerBound( __in_opt const TYPE* root = NULL ) { if (!root) root = GetRoot(); for (const TYPE* node = root; node; node = LeftChild(node)) root = node; return root; }
void main() { Status i; int j; position p; TElemType e; SqBiTree T,s; InitBiTree(T); CreateBiTree(T); printf("建立二叉树后,树空否?%d(1:是 0:否) 树的深度=%d\n",BiTreeEmpty(T),BiTreeDepth(T)); i=Root(T,&e); if(i) printf("二叉树的根为:%d\n",e); else printf("树空,无根\n"); printf("层序遍历二叉树:\n"); LevelOrderTraverse(T,visit); printf("中序遍历二叉树:\n"); InOrderTraverse(T,visit); printf("后序遍历二叉树:\n"); PostOrderTraverse(T,visit); printf("请输入待修改结点的层号 本层序号: "); scanf("%d%d",&p.level,&p.order); e=Value(T,p); printf("待修改结点的原值为%d请输入新值: ",e); scanf("%d",&e); Assign(T,p,e); printf("先序遍历二叉树:\n"); PreOrderTraverse(T,visit); printf("结点%d的双亲为%d,左右孩子分别为",e,Parent(T,e)); printf("%d,%d,左右兄弟分别为",LeftChild(T,e),RightChild(T,e)); printf("%d,%d\n",LeftSibling(T,e),RightSibling(T,e)); InitBiTree(s); printf("建立右子树为空的树s:\n"); CreateBiTree(s); printf("树s插到树T中,请输入树T中树s的双亲结点 s为左(0)或右(1)子树: "); scanf("%d%d",&e,&j); InsertChild(T,e,j,s); Print(T); printf("删除子树,请输入待删除子树根结点的层号 本层序号 左(0)或右(1)子树: "); scanf("%d%d%d",&p.level,&p.order,&j); DeleteChild(T,p,j); Print(T); ClearBiTree(T); printf("清除二叉树后,树空否?%d(1:是 0:否) 树的深度=%d\n",BiTreeEmpty(T),BiTreeDepth(T)); i=Root(T,&e); if(i) printf("二叉树的根为:%d\n",e); else printf("树空,无根\n"); }
void PercDown(ElementTyp A[], int i, int N) { int Child; ElementTyp Tmp; for(Tmp = A[i]; LeftChild(i) < N; i = Child) { Child = LeftChild(i); if (Child != N - 1 && A[Child] < A[Child+1]) { // 找到最小儿子 Child++; } if (Tmp < A[Child]) { // 逐层下滤 A[i] = A[Child]; } else { break; } } A[i] = Tmp; }
/** * @param T 二叉树 * @param node 要操作的节点 * @return 返回node的右兄弟 */ BiTreeNode *RightSubling(BiTree *T, BiTreeNode *node) { BiTreeNode *parent = (BiTreeNode *)malloc(sizeof(BiTreeNode)); BiTreeNode *right = (BiTreeNode *)malloc(sizeof(BiTreeNode)); if (T == NULL || node == NULL || node == T->root) return NULL; parent = node->parent; if (LeftChild(parent) == node) { right = RightChild(parent); return right; } return NULL; }
/** * @param T 二叉树 * @param node 要操作的节点 * @return 返回node的左兄弟节点 */ BiTreeNode *LeftSubling(BiTree *T, BiTreeNode *node) { BiTreeNode *parent = (BiTreeNode *)malloc(sizeof(BiTreeNode)); BiTreeNode *left = (BiTreeNode *)malloc(sizeof(BiTreeNode)); if (T == NULL || node == NULL || node == T->root) return NULL; parent = node->parent; if (RightChild(parent) == node) { left = LeftChild(parent); return left; } return NULL; }
//m$ implementation ==> MiGetPreviousNode __checkReturn bool GetPrev( __inout const TYPE** node ) { if (node && *node) { const TYPE* next; if (next = LeftChild(*node)) return GetUpperBound(next, node); next = Parent(*node); for (const void* child = *node; next && next != child; next = Parent(child)) { if (LeftChild(next) != child) { *node = next; return true; } child = next; } } return false; }
int main() { Status i; Position p; TElemType e; SqBiTree T; InitBiTree(T); CreateBiTree(T); printf("建立二叉树后,树空否?%d(1:是 0:否) 树的深度=%d\n",BiTreeEmpty(T),BiTreeDepth(T)); i=Root(T,&e); if(i) printf("二叉树的根为:%d\n",e); else printf("树空,无根\n"); printf("层序遍历二叉树:\n"); LevelOrderTraverse(T); printf("前序遍历二叉树:\n"); PreOrderTraverse(T); printf("中序遍历二叉树:\n"); InOrderTraverse(T); printf("后序遍历二叉树:\n"); PostOrderTraverse(T); printf("修改结点的层号3本层序号2。"); p.level=3; p.order=2; e=Value(T,p); printf("待修改结点的原值为%d请输入新值:50 ",e); e=50; Assign(T,p,e); printf("前序遍历二叉树:\n"); PreOrderTraverse(T); printf("结点%d的双亲为%d,左右孩子分别为",e,Parent(T,e)); printf("%d,%d,左右兄弟分别为",LeftChild(T,e),RightChild(T,e)); printf("%d,%d\n",LeftSibling(T,e),RightSibling(T,e)); ClearBiTree(T); printf("清除二叉树后,树空否?%d(1:是 0:否) 树的深度=%d\n",BiTreeEmpty(T),BiTreeDepth(T)); i=Root(T,&e); if(i) printf("二叉树的根为:%d\n",e); else printf("树空,无根\n"); return 0; }
void SiftDown(size_t point) { while ((LeftChild(point) && comparator(heap_[LeftChild(point)], heap_[point])) || (RightChild(point) && comparator(heap_[RightChild(point)], heap_[point]))) { if (!RightChild(point)) { Swap(point, LeftChild(point)); point = LeftChild(point); continue; } if (comparator(heap_[LeftChild(point)], heap_[RightChild(point)])) { Swap(point, LeftChild(point)); point = LeftChild(point); } else { Swap(point, RightChild(point)); point = RightChild(point); } } }
void main() { int i; CSTree T, p, q; TElemType e, e1; InitTree(T); printf("构造空树后,树空否?%d(1:是 0:否)。树根为%c,树的深度为%d。\n", TreeEmpty(T), Root(T), TreeDepth(T)); CreateTree(T); printf("构造树T后,树空否?%d(1:是 0:否)。树根为%c,树的深度为%d。\n", TreeEmpty(T), Root(T), TreeDepth(T)); printf("层序遍历树T:\n"); LevelOrderTraverse(T, visit); printf("请输入待修改的结点的值 新值:"); scanf("%c%*c%c%*c", &e, &e1); Assign(T, e, e1); printf("层序遍历修改后的树T:\n"); LevelOrderTraverse(T, visit); printf("%c的双亲是%c,长子是%c,下一个兄弟是%c。\n", e1, Parent(T, e1), LeftChild(T, e1), RightSibling(T, e1)); printf("建立树p:\n"); CreateTree(p); printf("层序遍历树p:\n"); LevelOrderTraverse(p, visit); printf("将树p插到树T中,请输入T中p的双亲结点 子树序号:"); scanf("%c%d%*c", &e, &i); q = Point(T, e); InsertChild(T, q, i, p); printf("层序遍历修改后的树T:\n"); LevelOrderTraverse(T, visit); printf("先根遍历树T:\n"); PreOrderTraverse(T, visit); printf("\n后根遍历树T:\n"); PostOrderTraverse(T, visit); printf("\n删除树T中结点e的第i棵子树,请输入e i:"); scanf("%c%d", &e, &i); q = Point(T, e); DeleteChild(T, q, i); printf("层序遍历修改后的树T:\n"); LevelOrderTraverse(T, visit); DestroyTree(T); }
int main(int argc,char *argv[]) { int i; TElemType e,tempe; PTree T,C; InitTree(T); InitTree(C); printf("������:"); CreateTree(T); CreateTree(C); printf("\n������:"); TraverseTree(T,Visit); printf("\n"); TraverseTree(C,Visit); printf("\n����:%c",Root(T)); printf("\n����i:"); scanf("%d%*c",&i); printf("\n��ֵ:%c",(tempe=Value(T,i))); printf("\n��ֵ:"); scanf("%c%*c",&e); Assign(T,tempe,e); printf("\n���ڵ㡢���ӡ����ֵܣ��������:%c %c %c %d %d", Value(T,Parent(T,e)), Value(T,LeftChild(T,e)),Value(T,RightSibling(T,e)), TreeDepth(T),TreeEmpty(T)); printf("\n��������:"); InsertChild(T,e,1,C); TraverseTree(T,Visit); printf("\nɾ�������:"); DeleteChild(T,e,1); TraverseTree(T,Visit); DestroyTree(T); DestroyTree(C); return 0; }
void MaxHeap<T>::SiftDown(unsigned n, unsigned size) { unsigned l = LeftChild(n); unsigned r = RightChild(n); unsigned max_index; if(r>=size) { if(l>=size) return; else max_index = l; } else { arr[l]>=arr[r] ? max_index = l : max_index = r; } if(arr[n]<arr[max_index]) { std::swap(arr[n],arr[max_index]); SiftDown(max_index,size); } }
void main() { int i; CSTree T,p,q; TElemType e,e1; InitTree(T); printf("构造空树后,树空否? %d(1:是 0:否) 树根为%c 树的深度为%d\n",TreeEmpty(T),Root(T),TreeDepth(T)); CreateTree(T); printf("构造树T后,树空否? %d(1:是 0:否) 树根为%c 树的深度为%d\n",TreeEmpty(T),Root(T),TreeDepth(T)); printf("先根遍历树T:\n"); PreOrderTraverse(T,vi); printf("\n请输入待修改的结点的值 新值: "); scanf("%c%*c%c%*c",&e,&e1); Assign(T,e,e1); printf("后根遍历修改后的树T:\n"); PostOrderTraverse(T,vi); printf("\n%c的双亲是%c,长子是%c,下一个兄弟是%c\n",e1,Parent(T,e1),LeftChild(T,e1),RightSibling(T,e1)); printf("建立树p:\n"); InitTree(p); CreateTree(p); printf("层序遍历树p:\n"); LevelOrderTraverse(p,vi); printf("\n将树p插到树T中,请输入T中p的双亲结点 子树序号: "); scanf("%c%d%*c",&e,&i); q=Point(T,e); InsertChild(T,q,i,p); printf("层序遍历树T:\n"); LevelOrderTraverse(T,vi); printf("\n删除树T中结点e的第i棵子树,请输入e i: "); scanf("%c%d",&e,&i); q=Point(T,e); DeleteChild(T,q,i); printf("层序遍历树T:\n",e,i); LevelOrderTraverse(T,vi); printf("\n"); DestroyTree(T); }
void PriorityQueue::PercolateDown(int i) { int max = -1; int left = LeftChild(i); int right = RightChild(i); if(left != -1 && heap_type == MAX_HEAP && A[left] > A[i] || left != -1 && heap_type == MIN_HEAP && A[left] < A[i]) max = left; else max = i; if(right != -1 && heap_type == MAX_HEAP && A[right] > A[max] || right != -1 && heap_type == MIN_HEAP && A[right] < A[max]) max = right; if(max != i) //Swap with max index { int tmp = A[i]; A[i] = A[max]; A[max] = tmp; PercolateDown(max); } }
CSTree LeftChild(CSTree T,TElemType cur_e) { CSTree p; CSTree temp; if(T==NULL) return NULL; else { p=T; while(p) { if(p->e==cur_e) return p->firstchild==NULL?NULL:p->firstchild; else if(p->firstchild) { temp=LeftChild(p->firstchild,cur_e); if(temp!=NULL) return temp; } p=p->rightsibling; } return NULL; } }
int main(int argc, char **argv) { printf("*************************************************\n"); printf("程序名称: Parent Tree\t作者:jasonleakey\n"); printf("*************************************************\n"); int menu_sel_num; int i; char cur_ch; //current node value char newValue; PPTree TT; PTree T = &TT; PRINT_MENU; while (scanf("%d", &menu_sel_num) == 1 && menu_sel_num != EOF) { switch(menu_sel_num) { case 1: InitPTree(&T); printf("Init parent tree complete!\n"); break; case 2: CLEAR_STDIN; CreatePTree(&T); printf("Create parent tree complete!\n"); break; case 3: printf("T is %s\n",\ (PTreeEmpty(T)) ? ("Empty") : ("Not Empty")); break; case 4: printf("Parent Tree depth:%d\n", PTreeDepth(T)); break; case 5: printf("Root data:%c\n", Root(T)); break; case 6: CLEAR_STDIN; printf("Input i(0-?):"); while (scanf("%d", &i) != 1) { CLEAR_STDIN; } printf("the i-th node data:%c\n", Value(T, i)); break; case 7: CLEAR_STDIN; printf("Input data you want to modify:"); scanf("%c", &cur_ch); CLEAR_STDIN; printf("Input new data after modification:"); scanf("%c", &newValue); Assign(&T, cur_ch, newValue); printf("Assign complete!\n"); break; case 8: CLEAR_STDIN; printf("Input data:"); scanf("%c", &cur_ch); printf("Its parent's data:%c\n", Parent(T, cur_ch)); printf("Parent complete!\n"); break; case 9: CLEAR_STDIN; printf("Input data:"); scanf("%c", &cur_ch); printf("Its left child's data:%c\n", LeftChild(T, cur_ch)); printf("LeftChlid complete!\n"); break; case 10: CLEAR_STDIN; printf("Input data:"); scanf("%c", &cur_ch); printf("Its right sibling's data:%c\n", RightSibling(T, cur_ch)); printf("RightSibling complete!\n"); break; case 11: Print(T); printf("Print complete!\n"); break; case 12: Traverse(T, vi); break; case 0: DestroyPTree(&T); exit(EXIT_SUCCESS); default: printf("Input error!\nInput again:"); } CLEAR_STDIN; printf("press Return back to menu:"); getchar(); PRINT_MENU; } DestroyPTree(&T); return EXIT_SUCCESS; }
//SqBiTree的测试程序 int main(int argc,char *argv[]) { int i=0; char buf[1024]; char e,newe; char *temp; bool k; position p; BiTree T,C; BiTree q,q2; InitBiTree(T); InitBiTree(C); printf("输入数据创建二叉树(#表示空):"); scanf("%s%*c",buf); temp=buf; CreateBiTree(T,temp,i); printf("\n遍历(前、中、后、层):"); PreOrderTraverse(T,Visit); printf("\n"); InOrderTraverse(T,Visit); printf("\n"); PostOrderTraverse(T,Visit); printf("\n"); LevelOrderTraverse(T,Visit); printf("\n二叉树是否为空:%s",BiTreeEmpty(T)==1?"空":"非空"); printf("\n树的深度:%d",BiTreeDepth(T)); printf("\n根节点的左孩子、双亲、左右孩子、左右兄弟节点"); q=LeftChild(T,*T); printf("\n%c %c %c %c %c %c",q==NULL?Nil:q->e,(q2=Parent(T,*q))==NULL?Nil:q2->e,(q2=LeftChild(T,*q))==NULL?Nil:q2->e, (q2=RightChild(T,*q))==NULL?Nil:q2->e,(q2=LeftSibling(T,*q))==NULL?Nil:q2->e, (q2=RightSibling(T,*q))==NULL?Nil:q2->e); printf("\n节点的新值:"); scanf("%c%*c",&newe); Assign(T,*q,newe); printf("\n替换后遍历(前、中、后、层):"); PreOrderTraverse(T,Visit); printf("\n"); InOrderTraverse(T,Visit); printf("\n"); PostOrderTraverse(T,Visit); printf("\n"); LevelOrderTraverse(T,Visit); printf("\n输入数据创建二叉树(#表示空):"); scanf("%s%*c",buf); temp=buf; i=0; CreateBiTree(C,temp,i); printf("\n输入要插入数据的层、序号和左右子树(0左1右):"); scanf("%d%d%d%*c",&p.level,&p.order,&k); InsertChild(T,p,k,C); printf("\n遍历(前、中、后、层):"); PreOrderTraverse(T,Visit); printf("\n"); InOrderTraverse(T,Visit); printf("\n"); PostOrderTraverse(T,Visit); printf("\n"); LevelOrderTraverse(T,Visit); printf("\n删除%d层%d个节点的右子树后遍历:",p.level,p.order); DeleteChild(T,p,1); printf("\n遍历(前、中、后、层):"); PreOrderTraverse(T,Visit); printf("\n"); InOrderTraverse(T,Visit); printf("\n"); PostOrderTraverse(T,Visit); printf("\n"); LevelOrderTraverse(T,Visit); DestroyBiTree(T); system("pause"); return 0; }
void main() { int i; BiPTree T,c,q; TElemType e1,e2; InitBiTree(&T); printf("构造空二叉树后,树空否?%d(1:是 0:否)树的深度=%d\n",BiTreeEmpty(T),BiTreeDepth(T)); e1=Root(T); if(e1!=Nil) printf("二叉树的根为: "form"\n",e1); else printf("树空,无根\n"); #ifdef CHAR printf("请按先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n"); #endif #ifdef INT printf("请按先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n"); #endif CreateBiTree(&T); printf("建立二叉树后,树空否?%d(1:是 0:否) 树的深度=%d\n",BiTreeEmpty(T),BiTreeDepth(T)); e1=Root(T); if(e1!=Nil) printf("二叉树的根为: "form"\n",e1); else printf("树空,无根\n"); printf("中序递归遍历二叉树:\n"); InOrderTraverse(T,visitT); printf("\n后序递归遍历二叉树:\n"); PostOrderTraverse(T,visitT); printf("\n层序遍历二叉树:\n"); LevelOrderTraverse(T,visitT); printf("\n请输入一个结点的值: "); scanf("%*c"form,&e1); c=Point(T,e1); /* c为e1的指针 */ printf("结点的值为"form"\n",Value(c)); printf("欲改变此结点的值,请输入新值: "); scanf("%*c"form"%*c",&e2); Assign(c,e2); printf("层序遍历二叉树:\n"); LevelOrderTraverse(T,visitT); e1=Parent(T,e2); if(e1!=Nil) printf("\n"form"的双亲是"form"\n",e2,e1); else printf(form"没有双亲\n",e2); e1=LeftChild(T,e2); if(e1!=Nil) printf(form"的左孩子是"form"\n",e2,e1); else printf(form"没有左孩子\n",e2); e1=RightChild(T,e2); if(e1!=Nil) printf(form"的右孩子是"form"\n",e2,e1); else printf(form"没有右孩子\n",e2); e1=LeftSibling(T,e2); if(e1!=Nil) printf(form"的左兄弟是"form"\n",e2,e1); else printf(form"没有左兄弟\n",e2); e1=RightSibling(T,e2); if(e1!=Nil) printf(form"的右兄弟是"form"\n",e2,e1); else printf(form"没有右兄弟\n",e2); InitBiTree(&c); printf("构造一个右子树为空的二叉树c:\n"); #ifdef CHAR printf("请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n"); #endif #ifdef INT printf("请先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n"); #endif CreateBiTree(&c); printf("先序递归遍历二叉树c:\n"); PreOrderTraverse(c,visitT); printf("\n树c插到树T中,请输入树T中树c的双亲结点 c为左(0)或右(1)子树: "); scanf("%*c"form"%d",&e1,&i); q=Point(T,e1); InsertChild(q,i,c); printf("先序递归遍历二叉树:\n"); PreOrderTraverse(T,visitT); printf("\n删除子树,请输入待删除子树的双亲结点 左(0)或右(1)子树: "); scanf("%*c"form"%d",&e1,&i); q=Point(T,e1); DeleteChild(q,i); printf("先序递归遍历二叉树:\n"); PreOrderTraverse(T,visitT); printf("\n"); DestroyBiTree(&T); }
int main() { /** * 建立一个含三个元素的二叉树进行测试 */ BiTreeNode *leftNode = (BiTreeNode *)malloc(sizeof(BiTreeNode)); BiTreeNode *rightNode = (BiTreeNode *)malloc(sizeof(BiTreeNode)); BiTreeNode *leftLeftNode = (BiTreeNode *)malloc(sizeof(BiTreeNode)); int newData; Queue *queue = (Queue *)malloc(sizeof(Queue)); //定义一个根,然后动态分配一个内存单元 BiTree *T = (BiTree *)malloc(sizeof(BiTree)); // 测试 Initbitree() InitBiTree(T); printf("T 树是否为空 :"); if (BiTreeEmpty(T)) printf("为空\n"); else { printf("不为空\n"); } // 生成根的数据 测试方法 CreateBiTree() printf("请输入根的数据"); scanf("%d", &newData); CreateBiTree(T, newData); printf("T 树是否为空 : "); if (BiTreeEmpty(T)) printf("为空\n"); else { printf("不为空\n"); } // 生成根的左孩子 printf("请输入根的左孩子的数据"); scanf("%d", &newData); T->root->left = leftNode; leftNode->data = newData; leftNode->parent = T->root; // 生成根的左孩子的左孩子 printf("添加根的左孩子的左孩子用于测试"); scanf("%d", &newData); T->root->left->left = leftLeftNode; leftLeftNode->data = newData; leftLeftNode->parent = T->root->left; // 生成根的右孩子 printf("请输入根的右子树"); scanf("%d", &newData); T->root->right = rightNode; rightNode->data = newData; rightNode->parent = T->root; /** * 打印出根的数据 * 测试的方法有 Root() 和 Value() */ printf("根的数据是 :%d\n", Visit(Root(T))); /** * 测试BiTreeDepth() 这个方法 */ printf("树的深度是 %d \n", BiTreeDepth(T->root)); /** * 测试Leftchild() 和 Rightchild() */ printf("根的左孩子的数据是: %d \n", Visit(LeftChild(Root(T)))); printf("根的右孩子的数据是: %d \n", Visit(RightChild(Root(T)))); // 测试 Parent() printf("根的左孩子的父亲节点数据 : %d ", Visit(Parent(T, LeftChild(Root(T))))); // 测试Leftsubling() printf("测试LeftSubling的方法,输出是:%d \n", Visit(LeftSubling(T, T->root->right))); // 测试先序遍历 中序遍历 和 后序遍历 printf("先序测试结果:"); PreOrderTraverse(T->root); printf("\n"); printf("中序测试结果"); InOrderTraverse(T->root); printf("\n"); printf("后序测试结果"); PostOrderTraverse(T->root); printf("\n"); /** * 测试Deletechild()和Destroybitree() 这两个方法 * 在Deletechild() 中调用了Destroybitree() 这个方法 */ printf("删除根的左子树,然后通过先序遍历遍历树\n"); DeleteChild(T, T->root, 0); T->root->left = NULL; PreOrderTraverse(T->root); return 0; }