void test_RightSibling(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, *right_sibling;

	node = get_node(tree, 2);
	right_sibling = RightSibling(tree, node);
	assert_node_equal(right_sibling, 3);

	node = get_node(tree, 51);
	right_sibling = RightSibling(tree, node);
	assert_node_equal(right_sibling, 52);

	node = get_node(tree, 1);
	right_sibling = RightSibling(tree, node);
	CU_ASSERT_PTR_NULL(right_sibling);

	node = get_node(tree, 3);
	right_sibling = RightSibling(tree, node);
	CU_ASSERT_PTR_NULL(right_sibling);

	node = get_node(tree, 52);
	right_sibling = RightSibling(tree, node);
	CU_ASSERT_PTR_NULL(right_sibling);
}
Ejemplo n.º 2
0
int SearchableHeap<T, Cmp>::Search(int i, const T& t)
{
    w_assert3(HeapProperty(0));
    DBGTHRD(<<"Search starting at " << i
    << ", numElements=" << numElements
    );
    if(i > numElements-1) return -1;

    int parent = i; // root
    // First, check sibling of parent if parent != root
    if (parent >0 && (RightSibling(parent) < numElements))  {
        DBGTHRD(<<"check right sibling: " << RightSibling(parent));
        if (cmp.ge(elements[RightSibling(parent)], t)) {
            return RightSibling(parent);
        }
    }
Ejemplo n.º 3
0
 void main()
 {
   int i;
   PTree T,p;
   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");
   TraverseTree(T,vi);
   printf("请输入待修改的结点的值 新值: ");
   scanf("%c%*c%c%*c",&e,&e1);
   Assign(T,e,e1);
   printf("层序遍历修改后的树T:\n");
   TraverseTree(T,vi);
   printf("%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");
   TraverseTree(p,vi);
   printf("将树p插到树T中,请输入T中p的双亲结点 子树序号: ");
   scanf("%c%d%*c",&e,&i);
   InsertChild(T,e,i,p);
   Print(T);
   printf("删除树T中结点e的第i棵子树,请输入e i: ");
   scanf("%c%d",&e,&i);
   DeleteChild(T,e,i);
   Print(T);
 }
Ejemplo n.º 4
0
int main(int argc, char **argv)
{
	CSTree T;
	TElemType e;
	TElemType e1;
	InitTree(&T);
	printf("构造空树后,树空否? %d(1:是 0:否) 树根为%c 树的深度为%d\n",\
			TreeEmpty(T), Root(T), TreeDepth(T));
	CreateTree(&T);
	printf("构造空树后,树空否? %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_recurssion1(T, vi);
	printf("\n");
	printf("PostOrderTraverse_recurssion1 complete!\n");
	PostOrderTraverse_recurssion2(T, vi);
	printf("\n");
	printf("PostOrderTraverse_recurssion2 complete!\n");
	printf("\n%c的双亲是%c, 长子是%c,下一个兄弟是%c\n", e1, Parent(T, e1),\
			LeftChild(T, e1), RightSibling(T, e1));
	printf("层序遍历树:\n");
	LevelOrderTraverse(T, vi);
	DestroyTree(&T);
	return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
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");
}
//从第n个元素开始先根遍历树n,
void PreOrder(TREE_ARRAY T, int n)
{
	int c;
	cout<<T[n].data<<'\t';
	c=LeftMostChild(T, n);
	while(c!=0)
	{
		PreOrder(T, c);
		c=RightSibling(T, c);
	}
}
Ejemplo n.º 7
0
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));
}
Ejemplo n.º 8
0
 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;
}
Ejemplo n.º 10
0
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);
}
Ejemplo n.º 11
0
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;
}
Ejemplo n.º 12
0
static PNODE TreeGetLeftmost(PNODE pThisNode,
	unsigned nCurrentLevel,
	unsigned nSearchDepth)
{
	/*------------------------------------------------------
	* Determine the leftmost descendant of a node at a
	* given depth. This is implemented using a post-order
	* walk of the subtree under pThisNode, down to the
	* level of nSearchDepth. If we've searched to the
	* proper distance, return the currently leftmost node.
	* Otherwise, recursively look at the progressively
	* lower levels.
	*----------------------------------------------------*/

	PNODE pLeftmost;    /* leftmost descendant at level   */
	PNODE pRightmost;   /* rightmost offspring in search  */

	if (nCurrentLevel == nSearchDepth)
		pLeftmost = pThisNode; /*  searched far enough.    */
	else if (IsLeaf(pThisNode))
		pLeftmost = 0;  /* This node has no descendants    */
	else {  /* Do a post-order walk of the subtree.        */
		for (pLeftmost = TreeGetLeftmost(pRightmost =
			FirstChild(pThisNode),
			nCurrentLevel + 1,
			nSearchDepth)

			;
			(pLeftmost == 0) && (HasRightSibling(pRightmost))
			;
			pLeftmost = TreeGetLeftmost(pRightmost =
				RightSibling(pRightmost),
				nCurrentLevel + 1,
				nSearchDepth)
			) { /* Nothing inside this for-loop. */
		}
	}
	return (pLeftmost);
}
Ejemplo n.º 13
0
 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);
 }
Ejemplo n.º 14
0
CSTree RightSibling(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->rightsibling==NULL?NULL:p->rightsibling;
			else if(p->firstchild)
			{
				temp=RightSibling(p->firstchild,cur_e);
				if(temp!=NULL)
					return temp;
			}
			p=p->rightsibling;
		}
		return NULL;
	}
}
Ejemplo n.º 15
0
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);
}
Ejemplo n.º 16
0
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;
}
Ejemplo n.º 17
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);
 }
Ejemplo n.º 18
0
static BOOL TreeSecondWalk(PNODE pThisNode,
	unsigned nCurrentLevel)
{
	/*------------------------------------------------------
	* During a second pre-order walk, each node is given a
	* final x-coordinate by summing its preliminary
	* x-coordinate and the modifiers of all the node's
	* ancestors.  The y-coordinate depends on the height of
	* the tree.  (The roles of x and y are reversed for
	* RootOrientations of EAST or WEST.)
	* Returns: TRUE if no errors, otherwise returns FALSE.
	*----------------------------------------- ----------*/

	BOOL bResult = TRUE;        /* assume innocent        */
	long lxTemp, lyTemp;        /* hold calculations here */
	float flNewModsum;          /* local modifier value   */
	static float flModsum = (float)0.0;

	if (nCurrentLevel <= MAXIMUM_DEPTH) {
		flNewModsum = flModsum;  /* Save the current value  */
		switch (RootOrientation) {
		case NORTH:
			lxTemp = (long)xTopAdjustment +
				(long)(pThisNode->flPrelim + flModsum);
			lyTemp = (long)yTopAdjustment +
				(long)(nCurrentLevel * LEVEL_SEPARATION);
			break;
		case SOUTH:
			lxTemp = (long)xTopAdjustment +
				(long)(pThisNode->flPrelim + flModsum);
			lyTemp = (long)yTopAdjustment -
				(long)(nCurrentLevel * LEVEL_SEPARATION);
			break;
		case EAST:
			lxTemp = (long)xTopAdjustment +
				(long)(nCurrentLevel * LEVEL_SEPARATION);
			lyTemp = (long)yTopAdjustment -
				(long)(pThisNode->flPrelim + flModsum);
			break;
		case WEST:
			lxTemp = (long)xTopAdjustment -
				(long)(nCurrentLevel * LEVEL_SEPARATION);
			lyTemp = (long)yTopAdjustment -
				(long)(pThisNode->flPrelim + flModsum);
			break;
		}

		if (CheckExtentsRange(lxTemp, lyTemp)) {
			/* The values are within the allowable range */

			pThisNode->xCoordinate = (COORD)lxTemp;
			pThisNode->yCoordinate = (COORD)lyTemp;

			if (HasChild(pThisNode)) {
				/* Apply the flModifier value for this    */
				/* node to all its offspring.             */
				flModsum = flNewModsum =
					flNewModsum + pThisNode->flModifier;
				bResult = TreeSecondWalk(
					FirstChild(pThisNode), nCurrentLevel + 1);
				flNewModsum = flNewModsum -
					pThisNode->flModifier;
			}

			if ((HasRightSibling(pThisNode)) && (bResult)) {
				flModsum = flNewModsum;
				bResult = TreeSecondWalk(
					RightSibling(pThisNode), nCurrentLevel);
			}
		}
		else bResult = FALSE;   /* outside of extents   */
	}
	return (bResult);
}
Ejemplo n.º 19
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;
}