Using random function to generate 80 (not more than 200 and different) random integers, using these integers to generate a two-tree, respectively, the first sequence of two fork tree, the middle sequence traversal and the sequence of the output tree to traverse the node element sequence. Note: The first order traversal output requires a non-recursive implementation.
(2) Basic idea of program realization
1. Establish a suitable two-fork tree
Program is to
Figure 1.1
In the form of a building.
2. The pre-order traversal is implemented using stack stacks and pointers to children.
3. The pre-sequence traversal, the middle sequence traversal, the post-order traversal respectively uses the recursive realization.
4. If a two-fork tree is created, the randomly generated value is ( because the number is more, so the #define max_size, to 80 number, can be #define Max_size to #define Max_size):
106 199 95 144 102 164 26 96 87 168
The following tree can be established by the above values:
Figure 1.2
From figure 1.2 can be drawn their preamble, middle order and post sequence.
Pre-order sequence: 106 95 26 87 102 96 199 144 164 168
Sequencing sequence: 26 87 95 96 102 106 144 164 168 199
Post Order sequence: 87 26 96 102 95 168 164 144 199 106
(3) System flowchart
Program steps:
#include "stdio.h"
#include "conio.h"
#define MAX_SIZE 10/* Declare the different random numbers to generate */
struct Tree/* Declares the structure of the tree */
{
struct tree *left;
int data;
struct tree *right;
};
typedef struct Tree TreeNode;
typedef TreeNode *b_tree; /* Declare a binary tree list */
/* Insert a binary tree node */
B_tree Insert_node (b_tree root,int node)
{
B_tree NewNode;
B_tree CurrentNode;
B_tree parentnode;
Newnode= (b_tree) malloc (sizeof (TreeNode)); /* Create a new node's memory space */
newnode->data=node;
newnode->right=null;
newnode->left=null;
if (root==null)
return newnode;
Else
{
Currentnode=root;
while (Currentnode!=null)
{Parentnode=currentnode;
if (currentnode->data>node)/* value is less than the previous node, insert to left child */
currentnode=currentnode->left;
else currentnode=currentnode->right;
}
if (parentnode->data>node)/* value is greater than the previous node, insert to right child */
parentnode->left=newnode;
else parentnode->right=newnode;
}
return root;
}
/* Build two fork tree */
B_tree create_btree (int *data,int len)
{
B_tree Root=null;
int i;
for (i=0;i<len;i++)
Root=insert_node (Root,data[i]);
return root;
}
void preorder_nonrecursive (B_tree root)
{
B_tree Stack[max_size];
B_tree p;
int top=-1;
int i;
if (root = NULL)
{
top++; /* root node in stack */
Stack[top] = root;
while (Top >-1)/* stack is not empty loop */
{
p = stack[top]; /* Out of the stack and access the node */
top--;
printf ("%d\t", p->data);
if (p->right! = NULL)/* Right child into the stack */
{
top++;
Stack[top] = p->right;
}
if (p->left! = NULL)/*/left child into stack */
{
top++;
Stack[top] = p->left;
}
}
}
}
/* Two fork tree pre-sequence traversal--recursive */
void preorder (B_tree point)
{
if (point!=null)
{printf ("%d\t", point->data);
Preorder (Point->left);
Preorder (point->right);
}
}
/* Two fork tree in sequence traversal--recursive */
void Inorder (B_tree point)
{if (point!=null)
{
Inorder (Point->left);
printf ("%d\t", point->data);
Inorder (Point->right);
}
}
/* Two fork Tree post-traversal--recursive */
void Postorder (B_tree point)
{
if (point!=null)
{
Postorder (Point->left);
Postorder (Point->right);
printf ("%d\t", point->data);
}
Return
}
/* Main function */
Main ()
{
B_tree Root=null;
int i,j;
int value;
int nodelist[max_size];
printf ("\nthe elements of Binary tree:\n");
/* Read values to the array */
Srand ((unsigned) time (NULL));
for (i = 0; I <max_size;i++)
{
Nodelist[i]=rand ()%200+1;
for (j=0;j<i;j++)
if (Nodelist[i]==nodelist[j])
{
Nodelist[i]=rand ()%200+1;
j=0;
}
printf ("%d\t", Nodelist[i]);
}
/* Build two fork tree */
Root=create_btree (nodelist,max_size);
/* Pre-sequence Traversal binary tree-non-recursive */
printf ("\n\nthe non-inorder traversal result is: \ n");
Preorder_nonrecursive (root);
/* Pre-order Traversal binary Tree--recursion */
printf ("\nthe inorder traversal result is: \ n");
Preorder (root);
/* Middle sequence traversal binary tree-recursive */
printf ("\nthe inorder traversal result is: \ n");
Inorder (root);
/* post-traverse binary Tree-recursive */
printf ("\nthe postorder traversal result is: \ n");
Postorder (root);
Getch ();
}
(4) System operation
From:http://www.cnblogs.com/yongfeng/archive/2010/02/10/1666927.html
Tree traversal and non-recursive