Home > Back-end >  Have bosses know why this is, the tree generation
Have bosses know why this is, the tree generation

Time:04-15

# include & lt; Stdio. H>
# include & lt; Stdlib. H>

Struct binode {
The int data;
Struct binode * lchild;
Struct binode * rchild;

};
Typedef struct binode * bitree;
Typedef struct binode binode;
Void creatbitree bitree * (T) {
int ch;
- scanf_s (" % d ", & amp; Ch);
(* T)=(bitree) malloc (sizeof (binode));
If (ch==1)
* (T)=NULL;
If ((* T)==NULL)
return;
The else
{
(* T) - & gt; Data=https://bbs.csdn.net/topics/ch;
Creatbitree (& amp; ((* T) - & gt; Lchild));
Creatbitree ((& amp; (* T) - & gt; Rchild));
}
}

Int main () {
Int * T;
T=(int *) malloc (sizeof (int));
Creatbitree (T);
return;
}

CodePudding user response:

Fyi:
 # include & lt; The locale. H> 
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; The malloc. H>
Typedef struct BiTNode {//binary tree node
char data;//data
Struct lchild BiTNode * and * rchild;//the child pointer
} BiTNode, * BiTree;
Int nn=0;
Int CreateBiTree (BiTree * T) {//create a binary tree according to the first order sequence
char data;
scanf("%c",& The data);//in accordance with the first sequence order input the value of the node in the binary tree (a character), said the '#' empty tree
If (data='#') https://bbs.csdn.net/topics/={
* T=NULL;
} else {
* T=(BiTree) malloc (sizeof (BiTNode)); Nn++;
(* T) - & gt; Data=https://bbs.csdn.net/topics/data;//generate the root node
CreateBiTree (& amp; (* T) - & gt; Lchild);//structure left subtree
CreateBiTree (& amp; (* T) - & gt; Rchild);//structure right subtree
}
return 0;
}
Void Visit (BiTree T) {//output
If (T - & gt; The data!='#') {
Printf (" % c ", T - & gt; The data);
}
}
Void PreOrder (BiTree T) {//first sequence traversal
If (T!=NULL) {
Visit (T);//access to the root node
PreOrder (T - & gt; Lchild);//access left child node
PreOrder (T - & gt; Rchild);//access right child node
}
}
Void InOrder (BiTree T) {//sequence traversal
If (T!=NULL) {
InOrder (T - & gt; Lchild);//access left child node
Visit (T);//access to the root node
InOrder (T - & gt; Rchild);//access right child node
}
}
After the void PostOrder (BiTree T) {//sequence traversal
If (T!=NULL) {
PostOrder (T - & gt; Lchild);//access left child node
PostOrder (T - & gt; Rchild);//access right child node
Visit (T);//access to the root node
}
}
Void PreOrder2 (BiTree T) {//first sequence traversal (non-recursive)
//access T - & gt; After the data, T into stack, traverse the left subtree; Traverse the left subtree returns, the stack elements should be T, out of the stack, and the first sequence traversal T right subtree,
BiTree * stack=(BiTree *) malloc (nn * sizeof (BiTree));
Int sp=0;
BiTree p=T;//p is traversing the pointer
While (p | | sp) {//not empty stack is not empty or p cycle
If (p!=NULL) {
Stack [sp]=p; Sp++;//in the stack
Printf (" % c ", p - & gt; The data);//access to the root node
P=p - & gt; Lchild;//traverse left subtree
} else {
Sp -; P=stack [sp];//return stack
P=p - & gt; Rchild;//access right subtree
}
}
Free (stack);
}
Void InOrder2 (BiTree T) {//sequence traversal (non-recursive)
//T is to traverse the tree root is a pointer, sequence traversal requirements in after traversing the left subtree, access to the root, and traverse the right subtree,
//T into the stack, first traverse left subtree; Traverse the left subtree returns, the stack elements should be T, out of the stack and visit T - & gt; Data, and then in sequence traversal T right subtree,
BiTree * stack=(BiTree *) malloc (nn * sizeof (BiTree));
Int sp=0;
BiTree p=T;//p is traversing the pointer
While (p | | sp) {//not empty stack is not empty or p cycle
If (p!=NULL) {
Stack [sp]=p; Sp++;//in the stack
P=p - & gt; Lchild;//traverse left subtree
} else {
Sp -; P=stack [sp];//return stack
Printf (" % c ", p - & gt; The data);
P=p - & gt; Rchild;//access right subtree
}
}
Free (stack);
}

Typedef struct BiTNodePost {
BiTree BiTree;
Char tag;
} BiTNodePost, * BiTreePost;
After the void PostOrder2 (BiTree T) {//sequence traversal (non-recursive)
BiTreePost * stack=(BiTreePost *) malloc (nn * sizeof (BiTreePost));
Int sp=0;
BiTree p=T;//p is traversing the pointer
BiTreePost BT;
while (p !=NULL | | sp) {//not empty stack is not empty or p cycle
while (p !=NULL) {//traverse left subtree
BT=(BiTreePost) malloc (sizeof (BiTNodePost));
BT - & gt; BiTree=p;
BT - & gt; Tag='L';//visited left subtree
Stack [sp]=BT; Sp++;//in the stack
P=p - & gt; Lchild;
}
While (sp & amp; & (stack [sp - 1]) - & gt; Tag=='R') {//subtree access to complete access to the root node
Sp -; BT=stack [sp];//return stack
Printf (" % c ", BT - & gt; BiTree - & gt; The data);
Free (BT);
}
If (sp) {//traverse right subtree
BT=stack [sp - 1);
BT - & gt; Tag='R'.//visited right subtree
P=BT - & gt; BiTree;
P=p - & gt; Rchild;
}
}
Free (stack);
}
Void LevelOrder (BiTree T) {//level traversal
BiTree p;
BiTree * queue;
Int h=0, t=0, n=0;

If (T==NULL) return;
P=T;
The queue=(BiTree *) malloc (nn * sizeof (BiTree));
The queue [t]=p; T=(t + 1) % 10; n++;//root node team
While (n) {//not empty queue loop
P=queue [h];//right
element out of the teamPrintf (" % c ", p - & gt; The data);//access point to the node p
H=(h + 1) % 10; n--;//exit queue
If (p - & gt; Lchild!=NULL) {//left subtree is not empty, will be left subtree team
The queue [t]=p - & gt; Lchild; T=(t + 1) % 10; n++;
}
If (p - & gt; Rchild!=NULL) {//right subtree is not empty, the right subtree team
The queue [t]=p - & gt; Rchild; T=(t + 1) % 10; n++;
}
}
Free (queue);
}
Int main () {
BiTree T;

The setlocale (LC_ALL, "CHS");
CreateBiTree (& amp; T);

Printf (" sequence traversal first: "); PreOrder (T); printf("\n");
Printf (" the first sequence traversal (non-recursive) : "); PreOrder2 (T); printf("\n");
printf("\n");
Printf (" sequence traversal in: "); InOrder (T); printf("\n");
Printf (" sequence traversal (non-recursive) : "); InOrder2 (T); printf("\n");
printf("\n");
Printf (" sequence traversal: after "); PostOrder (T); printf("\n");
Printf (" after the sequence traversal (non-recursive) : "); PostOrder2 (T); printf("\n");
printf("\n"); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related