Home > Back-end >  Data structure of binary sort tree to create lookup inserted, program in creating a tree process is
Data structure of binary sort tree to create lookup inserted, program in creating a tree process is

Time:12-29

 # include & lt; stdio.h> 
#include
# define OK 1
# define False - 1
Typedef struct BSTNode {
int data;
Struct lchild BSTNode * and * rchild;
} BSTNode, * BSTree;

Int InsertBST BSTree * T, int (e)//insert
{
If (* T==NULL) return False.
BSTree * p * q;
Q=T;
(* p)=(BSTNode *) malloc (sizeof (BSTNode));
(* p) - & gt; data=https://bbs.csdn.net/topics/e;
(* p) - & gt; Lchild=(* p) - & gt; Rchild=NULL;
If (e==(* q) - & gt; Data) return 0;
If (e<(* q) - & gt; Data) InsertBST (& amp; (q) (*) - & gt; Lchild, e);
If (e> (* q) - & gt; Data) InsertBST (& amp; (q) (*) - & gt; Rchild, e);
Return OK;
}

Int CreatBST (BSTree * T, int num [], int n)//create
{
Int I=1;
(* T)=(BSTNode *) malloc (sizeof (BSTNode));
(* T) - & gt; data=https://bbs.csdn.net/topics/num [I];
(* T) - & gt; Lchild=(* T) - & gt; Rchild=NULL;
For (I=2; i<=n; I++)
InsertBST (T, num [I]);//create a process that insert node
Return OK;
}

Int SearchBST (BSTree T, int key)//recursive search
{
BSTree p=T;
If (p)
{
If (key==p - & gt; Data)
{
Printf (" look for success! \n");
Return OK;
}
Else if (key

SearchBST (p - & gt; Lchild, key);
Else if (key> P - & gt; Data& & P - & gt; Rchild)
SearchBST (p - & gt; Rchild, key);
The else printf (" keyword does not exist! \n");
}
return;
}
Int main ()
{
BSTree T;
Int num [50], n, a, I=1, j, key;
Printf (" please input as required below to create a binary sort tree \ n ");
Do {
Printf (" please enter a number (1 out of circulation) : \ n ");
The scanf (" % d ", & amp; Num [I]);
A=num [I];
i++;
N++;
} while (a!=(1));
N -;
If (CreatBST (& amp; T, num, n)); Printf (" create success! \n");//create

Printf (" please enter the insert number: \ n ");//insert
The scanf (" % d ", & amp; A);
J=InsertBST (& amp; T, a);
If (j==1) printf (" \ n inserted successfully! \n");
Else if (j==0) printf (" data already exists! Don't need to insert! \n");
The else printf (" tree is empty! Insert failed! \n");

Printf (" please input search key figures: \ n ");//find
The scanf (" % d ", & amp; Key);
SearchBST (T, key);

}




This is the problem screenshot, carefully check feel hands as if there is no problem?
Hope to help to have a look at, explain!!!!! Thank you very much!!!!!
  • Related