Home > Back-end >  Binary tree operation result is wrong
Binary tree operation result is wrong

Time:10-05

#include
#include
Typedef struct note {
The int data;
Struct left note * and * right;
} BTnode;
BTnode * CreateTree (int Data [], int n)
{
Current BTnode * root, * and * parent, * insert;

//create a root node
Root=(BTnode *) malloc (sizeof (BTnode));
Root - & gt; data=https://bbs.csdn.net/topics/Data [0].
Root - & gt; Left=root - & gt; Right=NULL;
The current=root;
For (int loop=1; Loop{insert=(BTnode *) malloc (sizeof (BTnode));
The insert - & gt; data=https://bbs.csdn.net/topics/Data (loop),
The insert - & gt; Left=insert - & gt; Right=NULL;
The current=root;

While (current) {

The parent=current;
If (current - & gt; Data> The insert - & gt; Data)
The current=current - & gt; The left;
The else current=current - & gt; Right;
If (the parent - & gt; Data> The insert - & gt; Data)
The parent - & gt; Left=insert;
The else
The parent - & gt; Right=insert;
}
}
Return the root;
}
Void Forder (BTnode * root)
{if (root)
{printf (" % 5 d, "root - & gt; The data);
Forder (root - & gt; Left);
Forder (root - & gt; right);

}
}
Void Inorder (BTnode * root)
{if (root) {
Forder (root - & gt; Left);
Printf (" % 3 d ", root - & gt; The data);
Forder (root - & gt; right);

}
}
Void FDorder (BTnode * root)
{if (root) {

Forder (root - & gt; Left);
Forder (root - & gt; right);
Printf (" % 2 d ", root - & gt; The data);
}
}

Int main (void)
{int Data [8]=6,4,3,8,2,9,5,7 {};
BTnode * root;
//create a binary tree
Root=CreateTree (Data, 8);
Inorder (root);
Printf (" \ n \ n ");
Forder (root);
return 0;


}
  • Related