简体   繁体   English

线程二叉树的插入或顺序遍历有什么问题

[英]what's wrong in insertion or inorder traversal of in-threaded binary tree

somebody please help me with this code I've written it for an in-threaded binary tree and made two functions for insertion and inorder traversal. 有人请帮我提供这段代码,我已经为线程内的二叉树编写了该代码,并为插入和有序遍历提供了两个功能。 but the program is not going in the way I thought it to go. 但是该程序并没有按照我认为的方式进行。 after inserting 50,40,30,45 inorder traversal is 30->40->45->50 that is correct. 插入50,40,30,45后,顺序遍历为30-> 40-> 45-> 50是正确的。 but after this sequence when I tried to add 42 in it inorder traversal is 42->45->50 which is not correct. 但是在此序列之后,当我尝试在其中添加42时,遍历顺序为42-> 45-> 50,这是不正确的。 can somebody tell me where am I going wrong in this code. 有人可以告诉我这段代码在哪里出问题。 as I've tried a lot to debug it and according to my logic there shouldn't be any bug in it. 因为我已经尝试了很多调试它,并且根据我的逻辑,它应该没有任何错误。

#include <iostream>
#include <cstdlib>

using namespace std;

struct node{
int data;
bool lthread,rthread;
struct node *lchild,*rchild;
};

typedef struct node node;

node *in_succ(node *ptr){
if(ptr->rthread==true)
    return ptr->rchild;
ptr=ptr->rchild;
while(ptr->lthread==false)
    ptr=ptr->lchild;
return ptr;
}

node *in_pred(node *ptr){
if(ptr->lthread==true)
    return ptr->lchild;
ptr=ptr->rchild;
while(ptr->rthread==false)
    ptr=ptr->rchild;
return ptr;
}

node *ins(node *root,int n){
node *tmp;
tmp=(node*)malloc(sizeof(node));
tmp->data=n;
tmp->rchild=tmp->lchild=NULL;
tmp->rthread=tmp->lthread=true;
if(root==NULL)
    return tmp;
node *ptr=root;
node *par=NULL;
while(1){
    par=ptr;
    if(ptr->data==n){
        return root;
    }else if(ptr->data > n){
        ptr=ptr->lchild;
        if(par->lthread==true)
            break;
    }else{
        ptr=ptr->rchild;
        if(par->rthread==true)
            break;
    }
}
if(par->data>n){
    par->lthread=false;
    tmp->lchild=ptr;
    tmp->rchild=par;
    par->lchild=tmp;
}else{
    par->rthread=false;
    tmp->rchild=ptr;
    tmp->lchild=par;
    par->rchild=tmp;
}
}

void inorder(node *ptr){
if(ptr==NULL)
    return;
while(ptr->lthread==false)
    ptr=ptr->lchild;
while(ptr!=NULL){
    cout<<ptr->data<<" ";
    ptr=in_succ(ptr);
}
}

int main(){
int c,n;
node *root=NULL;
while(1){
    cout<<"1. Insert\n2. Delete\n3. Inorder\n4. Preorder\n5. Postorder\n6. Height\n0. Exit\n";
    cin>>c;
    switch(c){
        case 1:
            cin>>n;
            root=ins(root,n);
            break;
        case 3:
            inorder(root);
            cout<<endl;
            break;
        case 0:
            return 0;
    }
}
}

First I found one copy-past mistake in function in_pred 首先,我在函数in_pred发现了一个复制in_pred错误

ptr=ptr->rchild; -> ptr=ptr->lchild;

Second you forgot return root in function ins 其次,您忘记在函数ins return root

#include <iostream>
#include <cstdlib>

using namespace std;

struct node{
    int data;
    bool lthread,rthread;
    struct node *lchild,*rchild;
};

typedef struct node node;

node *in_succ(node *ptr){
    if(ptr->rthread==true)
        return ptr->rchild;
    ptr=ptr->rchild;
    while(ptr->lthread==false)
        ptr=ptr->lchild;
    return ptr;
}

node *in_pred(node *ptr){
    if(ptr->lthread==true)
        return ptr->lchild;
    ptr=ptr->lchild;
    while(ptr->rthread==false)
        ptr=ptr->rchild;
    return ptr;
}

node *ins(node *root,int n){
    node *tmp;
    tmp=(node*)malloc(sizeof(node));
    tmp->data=n;
    tmp->rchild=tmp->lchild=NULL;
    tmp->rthread=tmp->lthread=true;
    if(root==NULL)
        return tmp;
    node *ptr=root;
    node *par=NULL;
    while(1){
        par=ptr;
        if(ptr->data==n){
            return root;
        }else if(ptr->data > n){
            ptr=ptr->lchild;
            if(par->lthread==true)
                break;
        }else{
            ptr=ptr->rchild;
            if(par->rthread==true)
                break;
        }
    }
    if(par->data>n){
        par->lthread=false;
        tmp->lchild=ptr;
        tmp->rchild=par;
        par->lchild=tmp;
    }else{
        par->rthread=false;
        tmp->rchild=ptr;
        tmp->lchild=par;
        par->rchild=tmp;
    }
    return root;
}

void inorder(node *ptr){
    if(ptr==NULL)
        return;
    while(ptr->lthread==false)
        ptr=ptr->lchild;
    while(ptr!=NULL){
        cout<<ptr->data<<" ";
        ptr=in_succ(ptr);
    }
}

int main()
{
    int c,n;
    node *root=NULL;
    while(1){
        cout<<"1. Insert\n2. Delete\n3. Inorder\n4. Preorder\n5. Postorder\n6. Height\n0. Exit\n";
        cin>>c;
        switch(c){
            case 1:
                cin>>n;
                root=ins(root,n);
                break;
            case 3:
                inorder(root);
                cout<<endl;
                break;
            case 0:
                return 0;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM