Home > Back-end >  Clues about binary tree of sequence of functions
Clues about binary tree of sequence of functions

Time:10-20

Typedef enum {Link, Thread} PointerTag;
//Link==0 means pointer to around children
//Thread==1 said toward the precursor or subsequent clues

Struct TreeNode {
int data;
Left struct TreeNode * and * right;
PointerTag LTag RTag;
};

Struct TreeNode * pre;//global variable

Void InThreading (struct TreeNode * p) {
If (p) {
InThreading (p - & gt; Left);//recursive clues left subtree,
if (! P - & gt; Left) {
P - & gt; LTag=Thread;
P - & gt; Left=pre;
}
If (pre & amp; & ! The pre - & gt; Right) {
The pre - & gt; RTag=Thread;
The pre - & gt; Right=p;
}
// my understanding of this function should be no problem, but I see on the words "data structure", the author put a part of the function to the following red font, not with the pre is true this decision condition, the results make me confused about, I want to ask is, change the below red font that way is right? Why is that? In determining the pre - & gt; Right before the false must first determine the pre is true?
/*
if (! The pre - & gt; Right) {
The pre - & gt; RTag=Thread;
The pre - & gt; Right=p;
}
*/

The pre=p;
InThreading (p - & gt; Right);//recursive right subtree clues,
}
}

CodePudding user response:

To determine, I guess,

CodePudding user response:

Fyi:
 # include & lt; The locale. H> 
#include
#include
#include
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;
The scanf (" % c ", & amp; 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");
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related