Home > Back-end >  The binary tree based in not up, don't know where is wrong, please everyone a great god give ad
The binary tree based in not up, don't know where is wrong, please everyone a great god give ad

Time:09-21

/* establish binary tree, output the results various traversal, */
#include
#include
#include
using namespace std;
# define NULL 0
/* binary tree binary linked list node structure definition */
Typedef struct node {
Char value;//node data
Struct node * lChild;//the left child pointer
Struct node * rChild;//right child pointer
} BiTree, * BT;


/* * generate a binary tree/
Void InsertBST (BT * t, int key)
{
BiTree * s;
Int is=1;
If (* t==NULL) {
S=(BT) malloc (sizeof (BT));
S - & gt; value=https://bbs.csdn.net/topics/key;
S - & gt; LChild=NULL;
S - & gt; RChild=NULL;
* t=s;
} else {

If (is==1) {
(* t) - & gt; value=https://bbs.csdn.net/topics/key;//generate the root node
Is=1;
InsertBST (& amp; ((* t) - & gt; LChild), key);//recursive structure left subtree
} else {
(* t) - & gt; value=https://bbs.csdn.net/topics/key;//generate the root node
Is=1;
InsertBST (& amp; ((* t) - & gt; RChild), key);//recursive, construct right subtree
}
}
}

Void CreateBST (BT * t) creates a binary sort tree//
{//enter a node sequence, set up a binary sort tree, the root node pointer returned
Int the key;
* t=NULL;
The scanf (" % d ", & amp; Key);
While (key)
{
InsertBST (t, key);
The scanf (" % d ", & amp; Key);
}
}

/* binary tree before sequence traversal */
Int PreOrder (BT t) {
If (t!=NULL) {
Printf (" % c ", t - & gt; Value);//print data node
PreOrder (t - & gt; LChild);//to traverse the left subtree
PreOrder (t - & gt; RChild);//to traverse the right subtree
}
return 1;
}

/* binary tree of sequence traversal */
Int InOrder (BT t) {
If (t!=NULL) {
InOrder (t - & gt; LChild);//to traverse the left subtree
Printf (" % c ", t - & gt; Value);//print data node
InOrder (t - & gt; RChild);//to traverse the right subtree
}
return 1;
}

/* after binary tree sequence traversal */
Int PostOrder (BT t) {
If (t!=NULL) {
PostOrder (t - & gt; LChild);//to traverse the left subtree
PostOrder (t - & gt; RChild);//to traverse the right subtree
Printf (" % c ", t - & gt; Value);//print data node
}
return 1;
}

Void main () {
BiTree * t=(BT) malloc (sizeof (BiTree));

Printf (" please input binary tree: ");
CreateBST (& amp; t);
Printf (" preorder traversal of binary tree result is: ");
PreOrder (t);
printf("\n");
Printf (" to the result of the sequence traversal of binary tree: ");
InOrder (t);
printf("\n");
Printf (" sequence traversal of binary tree after the results as follows: ");
PostOrder (t);
printf("\n");

}
  • Related