The initialization function of the tree (both parental and child node methods),
The function of achievement,
Output tree function,
The pre-sequence traversal function of a tree (both recursive and non-recursive),
The sequential traversal function of a tree (both recursive and non-recursive),
The hierarchical traversal function of the tree,
A conversion function for a general tree and a two-fork tree.
Main Menu and sub-menu.
The main function.
The specific code is as follows:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
Set constants:
#define MAX_TREE_SIZE 100
The storage structure of the general tree is as follows: Parents knot, child knot, child brother knot. In this experiment, we applied the parents ' knot and the children's brothers ' knot point. The specific storage structure is as follows:
/* Parent of tree indicates node structure definition */
typedef struct
{
int data;
int parent; Parent location Domain
}ptnode;
/* Parent NOTATION tree structure */
typedef struct
{
Ptnode Node[max_tree_size];
int count; Root position and number of nodes
}ptree;
/* The child brother of the tree indicates the node structure definition */
typedef struct node{
int data;
struct node *firstchild;
struct node *rightsib;
}btnode,*btree;
Initialization tree (parent notation)
void Init_ptree (Ptree *tree)
{
tree->count=-1;
}
Initializing tree nodes (child sibling notation)
Btnode gettreenode (int x)
{
Btnode T;
T.data=x;
T.firstchild=t.rightsib=null;
return t;
}
Pre-sequence traversal of a tree (recursive)
void preorder (Btnode *t)
{
if (t!=null)
{
printf ("%d", t->data);
Preorder (T->firstchild);
Preorder (T->RIGHTSIB);
}
}
Pre-sequence traversal of a tree (non-recursive)
void Preorder2 (PTree T)
{
int i;
for (i=0;i<t.count;i++)
{
printf ("%d", t.node[i]);
}
}
Post-tree traversal (recursive)
void Inoeder (Btnode *t)
{
if (t!=null)
{
Inoeder (T->firstchild);
printf ("%d", t->data);
Inoeder (T->RIGHTSIB);
}
}
Post-tree traversal (non-recursive)
void Inoeder2 (PTree T)
{
int i;
for (i=t.count-1;i>=0;i--)
{
printf ("%d", t.node[i]);
}
}
Hierarchical traversal
void level (PTree T)
{
int i;
for (i=0;i<t.count;i++)
{
printf ("%d", t.node[i]);
}
}
Horizontal Output binary Tree
void Printbtree (Btnode *root,int level)
{
int i;
if (root!=null)
{
Printbtree (root->rightsib,level+1);
for (i=1;i<=8*level;i++)
printf ("");
printf ("-------%d\n", root->data);
Printbtree (root->firstchild,level+1);
}
}
Output tree
void Print_ptree (Ptree tree)
{
int i;
printf ("Serial number knot parent \ n");
for (i=0;i<=tree.count;i++)
{
printf ("%8d%8d%8d", i,tree.node[i].data,tree.node[i].parent);
printf ("\ n");
}
}
/* Create a tree with parent notation */
PTree Creattree (PTree T)
{
int i=1;
int fa,ch;
Ptnode p;
for (i=1;ch!=-1;i++)
{
printf ("Input%d node: \ n", i);
scanf ("%d,%d", &fa,&ch);
printf ("\ n");
P.data=ch;
P.PARENT=FA;
t.count++;
T.node[t.count].data = P.data;
T.node[t.count].parent = p.parent;
}
printf ("\ n");
printf ("The tree created is as follows: \ n");
Print_ptree (T);
return T;
}
/* General tree converted to binary tree */
Btnode *change (PTree T)
{
int i,j=0;
Btnode P[max_tree_size];
Btnode *ip,*is,*ir,*tree;
ip= (Btnode *) malloc (sizeof (Btnode));
is= (Btnode *) malloc (sizeof (Btnode));
Ir= (Btnode *) malloc (sizeof (Btnode));
Tree= (Btnode *) malloc (sizeof (Btnode));
for (i=0;i<t.count;i++)
{
P[i]=gettreenode (T.node[i].data);
}
for (i=1;i<t.count;i++)
{
ip=&p[i];
is=&p[j];
while (T.node[i].parent!=is->data)
{
j + +;
is=&p[j];
}
if (! ( Is->firstchild))
{
is->firstchild=ip;
Ir=ip;
}
Else
{
ir->rightsib=ip;
Ir=ip;
}
}
tree=&p[0];
return Tree;
}
/* Main Menu */
void Menu ()
{
printf ("================= main Menu =======================\n");
printf ("* * * Input 1-------------Create a general tree ***\n with parental law");
printf ("* * * Input 2-------------the pre-sequence traversal (recursion) *******\n of the Tree");
printf ("* * * Input 3-------------The sequential traversal of the tree (recursive) *******\n");
printf ("* * * Input 4-------------The pre-sequence traversal (non-recursive) *****\n of the Tree");
printf ("* * * Input 5-------------The sequential traversal of the tree (non-recursive) *****\n");
printf ("* * * Input 6-------------non-recursive traversal *******\n of hierarchy order");
printf ("* * * Input 0-------------Exit program *****************\n");
printf ("==============================================\n");
printf ("Please enter instructions for execution:");
}
/* Sub Menu */
void Menu2 ()
{
printf ("***************** Sub-menu *******************\n");
printf ("***9-------------return to main menu to continue Operation *******\n");
printf ("***0-------------Exit program *****************\n");
}
/* Main function */
void Main ()
{
int i=0,c1,c2;
PTree T;
Btnode *tree;
Init_ptree (&t);
Loop
Menu ();
scanf ("%d", &C1);
Switch (C1)
{
Case 1:
printf ("Establish a general tree, enter each node in turn: \ n");
printf ("Input node mode: Parent data, Integer data (the first node has a parent data of-1, end with -1,-1) \ n Example: -1,1 1,3\n");
T=creattree (T);
Tree=change (T);
printf ("General tree converted to binary tree case: \ n");
Printbtree (Tree,i);
GetChar ();
Break
Case 2:
printf ("Pre-order traversal of the Tree (recursive): \ n");
Preorder (Tree);
printf ("\ n");
Break
Case 3:
printf ("post-sequential traversal of the tree (recursion): \ n");
Inoeder (Tree);
printf ("\ n");
Break
Case 4:
printf ("Pre-order traversal of the tree (non-recursive): \ n");
Preorder2 (T);
printf ("\ n");
Break
Case 5:
printf ("post-sequential traversal of the tree (non-recursive): \ n");
Inoeder2 (T);
printf ("\ n");
Break
Case 6:
printf ("Hierarchy Traversal of the tree: \ n");
Level (T);
printf ("\ n");
Break
Case 0:
Exit (1);
Break
}
MENU2 ();
scanf ("%d", &C2);
if (c2==9)
Goto Loop;
else if (c2==0)
Exit (1);
}
Transformation and traversal of tree and two-fork tree