回 帖 发 新 帖 刷新版面

主题:谁能帮我看一下关于顺序二叉树的程序,拜托啦~~

#include<iostream.h>

typedef struct tree                               //定义二叉树结点
{
    int data;
    struct tree *lchild,*rchild;
}node;

node create_tree(node*,int&);        //建立顺序二叉树
void middle_look(node*);                    //中序遍历
node delete_treenode(node*,int&);        //将二叉树删除某一结点

void main()
{
    node *p=NULL;
    int array[8]={10,18,3,8,12,2,7,4};
    p=create_tree(p,array[]);
    middle_look(p);
    p=delete_treenode(p,10);
    middle_look(p);
}

node create_tree(node root,int& array[])
{
    node *point=root;
    for (int a=0;point==NULL;a++)
    {
        if(point==NULL)
        {
            point=new node;
            point->data=array[a];
            point->lchild=NULL;
            point->rchild=NULL;
        }
        else
        {
            node *newnode=new node;
            newnode->data=array[a];
            
            switch(array[a]>point->data)
            {
            case 1:
                {
                    point->rchild=newnode;
                    point->lchild=NULL;
                    point=point->rchild;
                }
                break;
            case 0:
                {
                    point->lchild=newnode;
                    point->rchild=NULL;
                    point=point->lchild;
                }
                break;
            default:
                break;
            }
        }
    }
    return root;
}

void middle_look(node& root)
{
    cout<<root->data<<' ';
    middle_look(root->lchild);
    middle_look(root->rchild);
}

node delete_treenode(node& root,int& val)
{
    node *p=NULL;
    int array[];
    void middle_evaluate(node *root)
    {
        int accumulate=0;
        if(root->data!=val)
        {
            array[accumulate]=root->data;
        }
        accumulate++;
        middle_evaluate(root->lchild);
        middle_evaluate(root->rchild);
    }
    middle_evaluate(*root);
    *p=create_tree(p,array[]);
    return p;
}

回复列表 (共4个回复)

沙发


程序无法执行,好像是函数形参写的有点问题...谁能帮忙改一下啊~~

板凳

显著的问题是实参传错了p=create_tree(p,array[]);这里参数2直接传递array就可以了

3 楼

另外建立树用的函数看着很别扭,直觉上讲逻辑不对劲

4 楼

谢谢了,我再试试

我来回复

您尚未登录,请登录后再回复。点此登录或注册