Home > Back-end >  Binary tree
Binary tree

Time:09-21

Title:
According to the order in the binary tree traversal non-recursive algorithm, the binary tree t find value for x element, if found and its left subtree is empty, then insert the value of y element will be the left child, otherwise if the right child is empty, then insert the y will be the right child, insert fails, the return value is 0, otherwise, the return value is 1
It's his writing:
The Status InsertTree (BiTree & amp; T, TElemType TElemType x, y) {
SqStack S;
InitStack (S);
BiTree p=T;
BiTree q=new BiTNode;
While (p | |! (StackEmpty (S))) {
If (p) {
Push (S, p);
P=p - & gt; Lchild;
}
The else {
Pop (S, q);
If ((q - & gt; data=https://bbs.csdn.net/topics/=x) && (q -> rchild==NULL)) {
BiTree s=new BiTNode;
S - & gt; data=https://bbs.csdn.net/topics/y;
S - & gt; Lchild=NULL;
S - & gt; Rchild=NULL;
Q - & gt; Rchild=s;
return OK;
}
If ((q - & gt; data=https://bbs.csdn.net/topics/=x) && (q -> lchild=NULL) {
BiTree s=new BiTNode;
S - & gt; data=https://bbs.csdn.net/topics/y;
S - & gt; Lchild=NULL;
S - & gt; Rchild=NULL;
Q - & gt; Lchild=s;
return OK;
}
P=q - & gt; Rchild;
}
return 0;
}
return 0;
}
Why every time insert failure? For big solutions
  • Related