#include <iostream>
using namespace std;
struct Btree{
int num;
struct Btree *next;
struct Btree *prev;`enter code here`
};
struct Btree * head=NULL;
struct Btree* create(){
struct Btree* temp;
temp =(struct Btree*)malloc(sizeof(struct Btree));
return temp;
}
void insert(int x){
struct Btree* node,*t;
node=create();
node->num=x;
node->next=NULL;
node->prev=NULL;
if(head==NULL){
head=node;
}
else
{
t=head;
while(t!=NULL){
if(x>t->num){
if(t->next==NULL){
t->next=node;
}
t=t->next;
}
else if(x<t->num){
if(t->prev==NULL){
t->prev=node;
}
t=t->prev;
}
}
}
}
void printInorder(struct Btree* node)
{
if (node == NULL) {
return;
}
else
{
printInorder(node->prev);
cout << node->num << " ";
printInorder(node->next);
}
}
void display(){
struct Btree *t;
t=head;
printInorder(t);
}
int main() {
insert(10);
insert(20);
insert(4);
insert(140);
insert(5);
insert(6);
insert(70);
display();
return 0;
}
///我需要在二叉树中插入节点,所以我创建了两个函数。我需要递归地使用preOrder Traversal方法遍历节点,但是它循环遍历它,并且不抛出任何输出。请帮我用这段代码。我认为值没有插入。但是我不知道这是怎么回事..任何人都可以解决我这个问题。它将对我有很大的帮助。我无法发现找出错误。