Requirements: Two fork tree is stored as a Lson-rson link, which is designed and completed in a menu way: Establish and store tree, output pre-order traversal results, output sequence traversal results, output post-order traversal results, exchange left and right subtree, statistical height, which for the middle sequence, post-order traversal operation requires non-recursive method. The code uses the C language description.
Hsy of computer Department of 13 grade of Lanzhou University
Date: 2015.6.1
Programming Environment: WIN7-64,CODEBLOCKS,GUN-GCC compiler
#include <stdio.h>
#include <stdlib.h>
struct Node *t;//declares global variable T
struct node//establishing nodes
{
struct Node*lson;
char data;
struct Node*rson;
};
struct Node * creattree (struct node*t)//two fork Tree establishment
{
Char ch;
scanf ("%c", &ch);
if (ch== ' # ')
{
T=null;
}
Else
{
t= (struct node*) malloc (sizeof (struct Node));
if (! T
Exit (1);//Overflow error
t->data=ch;
T->lson=creattree (T->lson);
T->rson=creattree (T->rson);
}
return T;
}
void Preordertraverse (struct Node *t)//recursive pre-sequence traversal
{
if (t==null)
Return
Else
{
printf ("%c", t->data);
Preordertraverse (T->lson);
Preordertraverse (T->rson);
}
}
void Inorder (struct node* T)//non-recursive middle sequence traversal
{
struct Node *stack[10], *p;
int top =-1;
if (t!= NULL)
{
p = T;
while (Top >-1 | | P! = NULL)
{
Scan all left nodes of P to merge stacks
while (P! = NULL)
{
top++;
Stack[top] = p;
p = p->lson;
}
if (Top >-1)
{
Out of the stack and access the node
p = stack[top];
top--;
printf ("%c", p->data);
Scan P's right child */
p = p->rson;
}
}
printf ("\ n");
}
}
Non-recursive sequential traversal of binary tree
void Postorder (struct node* T)
{
struct Node *stack[10], *p;
int sign, top =-1;
if (T! = NULL)
{
Do
{
T all left nodes into the stack
while (T! = NULL)
{
top++;
Stack[top] = T;
T = t->lson;
}
P point to the top of the stack at the previous visited node
p = NULL;
Set T as visited
sign = 1;
while (top! =-1 && sign)
{
Remove the top node of the stack
T = Stack[top];
Right child does not exist or right child has visited then access T
if (T->rson = = p)
{
printf ("%c", t->data);
top--;
P point to the node being accessed
p = T;
}
Else
{
T point to right child node
T = t->rson;
No access tag
sign = 0;
}
}
}while (Top! =-1);
printf ("\ n");
}
}
void Exchange (struct node*t)//swap left and right subtrees
{
if (t==null)
Return
struct Node *x;
x=t->lson;
t->lson=t->rson;
t->rson=x;//Completing the Exchange process
Exchange (T->lson);
Exchange (T->rson);
}
int high (struct Node *t)
{
int LH, RH;
if (t==null)
return 0;
Else
{
Lh=high (T->lson);
Rh=high (T->rson);
if (LH>RH)
return lh+1;
Else
return rh+1;
}
}
void UI ()//user interface
{
printf ("\n\n\n\n");
printf ("**************************************************************\n");
printf ("* *\n");
printf ("* Please select the menu function option before the ordinal *\n");
printf ("* *\n");
printf ("**************************************************************\n");
printf ("1. Build and store tree \n2. Output pre-order traversal result \n3. Output sequence traversal result \ n");
printf ("4. Output post-sequential traversal results \n5. Exchange left and right subtree \n6. Statistical height \n7. Exit procedure \n\n\n");
printf ("Please select Input 1-7\n");
}
void menu ()//Menus
{
UI ();
int C;
Char b;//is used to read the carriage return character during input
while (1)
{
scanf ("%d", &c);
if (c==7)
Break
Switch (c)
{
Case 1:
printf ("You have chosen to be 1. Build and store the tree \ n order to enter in the sequence of previous sequential traversal: \ n");
scanf ("%c", &b);
T=creattree (T);
printf ("\ n two fork tree has been established and storage completed \ n");
UI ();
Break
Case 2:
printf ("You chose to be 2. Output pre-order traversal result \ n");
scanf ("%c", &b);
Preordertraverse (T);
UI ();
Break
Case 3:
printf ("You have selected 3. Output sequence traversal result \ n");
Inorder (T);
UI ();
Break
Case 4:
printf ("You chose to be 4. Output post-routing results \ n");
Postorder (T);
UI ();
Break
Case 5:
printf ("You have selected 5. Swap left and right subtrees \ n");
scanf ("%c", &b);
Exchange (T);
printf ("\ n has completed the exchange of the subtree of the two-fork tree \ n");
UI ();
Break
Case 6:
printf ("Your choice is 6. Find the height of the binary tree \ n");
int H=high (T);
printf ("The height of the binary tree is%d\n", h);
UI ();
Break
}
}
}
int main ()
{
menu ();
}
Source: The heart of the NetEase blog
Welcome to share this article, reproduced please keep the source!
The storage, traversal, commutative subtree and the depth of the binary tree in the data structure