void test_LeftSibling(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_sibling; node = get_node(tree, 3); left_sibling = LeftSibling(tree, node); assert_node_equal(left_sibling, 2); node = get_node(tree, 52); left_sibling = LeftSibling(tree, node); assert_node_equal(left_sibling, 51); node = get_node(tree, 1); left_sibling = LeftSibling(tree, node); CU_ASSERT_PTR_NULL(left_sibling); node = get_node(tree, 2); left_sibling = LeftSibling(tree, node); CU_ASSERT_PTR_NULL(left_sibling); node = get_node(tree, 61); left_sibling = LeftSibling(tree, node); CU_ASSERT_PTR_NULL(left_sibling); }
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 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)); }
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"); }
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; }
//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); }
static BOOL TreeFirstWalk(PNODE pThisNode, unsigned nCurrentLevel) { /*------------------------------------------------------ * In a first post-order walk, every node of the tree is * assigned a preliminary x-coordinate (held in field * node->flPrelim). In addition, internal nodes are * given modifiers, which will be used to move their * children to the right (held in field * node->flModifier). * Returns: TRUE if no errors, otherwise returns FALSE. *----------------------------------------------------*/ PNODE pLeftmost; /* left- & rightmost */ PNODE pRightmost; /* children of a node. */ float flMidpoint; /* midpoint between left- */ /* & rightmost children */ /* Set up the pointer to previous node at this level */ pThisNode->prev = GetPrevNodeAtLevel(nCurrentLevel); /* Now we're it--the previous node at this level */ if (!(SetPrevNodeAtLevel(nCurrentLevel, pThisNode))) return (FALSE); /* Can't allocate element */ /* Clean up old values in a node's flModifier */ pThisNode->flModifier = (float)0.0; if ((IsLeaf(pThisNode)) || (nCurrentLevel == MAXIMUM_DEPTH)) { if (HasLeftSibling(pThisNode)) { /*-------------------------------------------- * Determine the preliminary x-coordinate * based on: * - preliminary x-coordinate of left sibling, * - the separation between sibling nodes, and * - mean width of left sibling & current node. *--------------------------------------------*/ /* Set the mean width of these two nodes */ TreeMeanNodeSize(LeftSibling(pThisNode), pThisNode); pThisNode->flPrelim = (pThisNode->Leftsibling->flPrelim) + (float)SIBLING_SEPARATION + flMeanWidth; } else /* no sibling on the left to worry about */ pThisNode->flPrelim = (float)0.0; } else { /* Position the leftmost of the children */ if (TreeFirstWalk(pLeftmost = pRightmost = FirstChild(pThisNode), nCurrentLevel + 1)) { /* Position each of its siblings to its right */ while (HasRightSibling(pRightmost)) { if (TreeFirstWalk(pRightmost = RightSibling(pRightmost), nCurrentLevel + 1)) { } else return (FALSE); /* malloc() failed */ } /* Calculate the preliminary value between */ /* the children at the far left and right */ flMidpoint = (pLeftmost->flPrelim + pRightmost->flPrelim) / (2.0); /* Set global mean width of these two nodes */ TreeMeanNodeSize(LeftSibling(pThisNode), pThisNode); if (HasLeftSibling(pThisNode)) { pThisNode->flPrelim = (pThisNode->leftsibling->flPreLim) + (float)SIBLING_SEPARATION + flMeanWidth; pThisNode->flModifier = pThisNode->flPrelim - flMidpoint; TreeApportion(pThisNode, nCurrentLevel); } else pThisNode->flPrelim = flMidpoint; } else return (FALSE); /* Couldn't get an element */ } return (TRUE); }
static void TreeApportion(PNODE pThisNode, unsigned nCurrentLevel) { /*------------------------------------------------------ * Clean up the positioning of small sibling subtrees. * Subtrees of a node are formed independently and * placed as close together as possible. By requiring * that the subtrees be rigid at the time they are put * together, we avoid the undesirable effects that can * accrue from positioning nodes rather than subtrees. *----------------------------------------------------*/ PNODE pLeftmost; /* leftmost at given level*/ PNODE pNeighbor; /* node left of pLeftmost */ PNODE pAncestorLeftmost; /* ancestor of pLeftmost */ PNODE pAncestorNeighbor; /* ancestor of pNeighbor */ PNODE pTempPtr; /* loop control pointer */ unsigned i; /* loop control */ unsigned nCompareDepth; /* depth of comparison */ /* within this proc */ unsigned nDepthToStop; /* depth to halt */ unsigned nLeftSiblings; /* nbr of siblings to the */ /* left of pThisNode, including pThisNode, */ /* til the ancestor of pNeighbor */ float flLeftModsum; /* sum of ancestral mods */ float flRightModsum; /* sum of ancestral mods */ float flDistance; /* difference between */ /* where pNeighbor thinks pLeftmost should be */ /* and where pLeftmost actually is */ float flPortion; /* proportion of */ /* flDistance to be added to each sibling */ pLeftmost = FirstChild(pThisNode); pNeighbor = LeftNeighbor(pLeftmost); nCompareDepth = 1; nDepthToStop = MAXIMUM_DEPTH - nCurrentLevel; while ((pLeftmost) && (pNeighbor) && (nCompareDepth <= nDepthToStop)) { /* Compute the location of pLeftmost and where it */ /* should be with respect to pNeighbor. */ flRightModsum = flLeftModsum = (float)0.0; pAncestorLeftmost = pLeftmost; pAncestorNeighbor = pNeighbor; for (i = 0; (i < nCompareDepth); i++) { pAncestorLeftmost = Parent(pAncestorLeftmost); pAncestorNeighbor = Parent(pAncestorNeighbor); flRightModsum = flRightModsum + pAncestorLeftmost->flModifier; flLeftModsum = flLeftModsum + pAncestorNeighbor->flModifier; } /* Determine the flDistance to be moved, and apply*/ /* it to "pThisNode's" subtree. Apply appropriate*/ /* portions to smaller interior subtrees */ /* Set the global mean width of these two nodes */ TreeMeanNodeSize(pLeftmost, pNeighbor); flDistance = (pNeighbor->flPrelim + flLeftModsum + (float)SUBTREE_SEPARATION + (float)flMeanWidth) - (pLeftmost->flPrelim + flRightModsum); if (flDistance > (float)0.0) { /* Count the interior sibling subtrees */ nLeftSiblings = 0; for (pTempPtr = pThisNode; (pTempPtr) && (pTempPtr != pAncestorNeighbor); pTempPtr = Leftsibling(pTempPtr)) { nLeftSiblings++; } if (pTempPtr) { /* Apply portions to appropriate */ /* leftsibling subtrees. */ flPortion = flDistance / (float)nLeftSiblings; for (pTempPtr = pThisNode; (pTempPtr != pAncestorNeighbor); pTempPtr = LeftSibling(pTempPtr)) { pTempPtr->flPrelim = pTempPtr->flPrelim + flDistance; pTempPtr->flModifier = pTempPtr->flModifier + flDistance; flDistance = flDistance - flPortion; } } else { /* Don't need to move anything--it needs */ /* to be done by an ancestor because */ /* pAncestorNeighbor and */ /* pAncestorLeftmost are not siblings of */ /* each other. */ return; } } /* end of the while */ /* Determine the leftmost descendant of pThisNode */ /* at the next lower level to compare its */ /* positioning against that of its pNeighbor. */ nCompareDepth++; if (IsLeaf(pLeftmost)) pLeftmost = TreeGetLeftmost(pThisNode, 0, nCompareDepth); else pLeftmost = FirstChild(pLeftmost); pNeighbor = LeftNeighbor(pLeftmost); } }