0

我想用 C++ 创建树。我可以在没有错误或警告的情况下编译代码,但我没有得到输出。

我认为错误是 inorder fn,但不知道如何删除它。

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct tree
{
    int data;
    struct tree * left;
    struct tree * right;
};
typedef struct tree * TREE;

TREE maketree(int x)
{
    TREE tree = (TREE)malloc(sizeof(tree));
    if(tree == NULL)
    {
        return NULL;
    }
    tree->left = tree->right = NULL;
    tree->data = x;
return tree;
}

void setleft(TREE tree,int x)
{
    if(tree == NULL || tree->left != NULL)
    {
        cout<<"\t Error !! Inserting New Node At Left Side  !\n";
    }
    else
    {
        tree->left = maketree(x);
    }
}

void setright(TREE tree,int x)
{
    if(tree == NULL || tree->right != NULL)
    {
        cout<<"\t Error !! Inserting New Node At Right Side !\n";
    }
    else
    {
        tree->right = maketree(x);
    }
}

void inorder(TREE root)
{
    if(root != NULL)
    {
        TREE left=root->left;
        TREE right=root->right;
        inorder(left);
        cout<<root->data;
        inorder(right);
    }
}

void main()
{
clrscr();
    TREE root = NULL,child,parent;
    int i,j = 1;
    cout<<"Root Of Binary Search Tree :- ";
    cin>>i;
    root = maketree(i);
    cout<<"\n\n";
    while(i)
    {
        cout<<j<<" Node Value:- ";
        cin>>i;
        if(i < 0)
        {
            break;
        }
        parent = child = root;
        while((i != parent->data) && (child != NULL))
        {
            parent = child;
            if(i < parent->data)
            {
                child = parent->left;
            }
            else
            {
                child = parent->right;
            }
        }
        if(i == parent->data)
        {
            cout<<"\t Value "<<i<<" Already Present In BST !!\n";
        }
        else if(i < parent->data)
        {
            setleft(parent,i);
        }
        else
        {
            setright(parent,i);
        }
        j++;
    }
    inorder(root);
getch();
}
4

1 回答 1

3

如果你想用 C++ 编写,那就用 C++ 编写。使用构造函数和析构函数以及类方法。可能将您的数据成员设为私有。而使用new而不是malloc你的析构函数可能会想要删除(树的子节点)。

您所写的内容是用 C 语言编写的,而不是您合并了 C++ 最糟糕的特性 iostream 以及旧的已弃用的非标准版本。

这看起来像一些学校练习。

我也无法在任何地方看到free您分配的数据malloc

您的排序逻辑应该在基于树的函数中,而不是 main。

您的“错误”可能是输出中缺少空格,但我不知道。

tree将数据类型(它是一个结构,在 C++ 中不需要使用结构限定)和变量(经常使用它)用作数据类型是合法的,但不是好的做法。

好的,现在有一些代码,主要是基于你的。

class tree
{
    tree * left;
    tree * right;
    int value;

public:
    explicit tree( int v );
    ~tree();

    bool insert( int v );
    void print( std::ostream& ) const;

private:
    tree( const tree& );
    tree& operator=( const tree& );

};

tree::tree( int v ) : 
   left( NULL ), 
   right( NULL ),
   value( v )
{
}

tree::~tree()
{
    delete right;
    delete left;
}

bool tree::insert( int v )
{
   // inserts v in the correct place in the tree, returns true
   // if it inserted or false if it already exists

   // I want you to fill in the detail for this function
}

void tree::print( std::ostream& os ) const
{
   // prints the tree
    if( left )
    {
       left->print( os );
    }
    os << value << '\n';
    if( right )
    {
       right->print( os );
    }
}

在那里,我留下了一个功能供您实现。您不需要实现私有复制构造函数或赋值运算符。

同时实施main()。请注意,不需要在堆上的 main 中实现树(使用 new )。在堆栈上实现它。

main()将读入数字,将它们插入到调用其insert()方法的树中,然后在最后打印树,std::cout作为参数传递。

您需要#include <iostream>(不是 iostream.h),它会起作用。

于 2012-10-16T09:31:28.367 回答