回 帖 发 新 帖 刷新版面

主题:谁能帮我改下顺序二叉树的题

#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;
}

无法编译,应该是参数设置的问题

回复列表 (共1个回复)

沙发

你可以检查修改下程序.我习惯把数组的第一个元素不用,下标从1开始

我来回复

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