二叉搜索树 C++ 展示

Binary Search Tree C++ at Display

如何在插入时获取代码:

添加每个节点时的正下方。

我的显示器有问题。我无法将其发送到 运行。代码 如下link。我需要显示所有插入结果的树结构。

http://goo.gl/NXAwrE

void BinarySearchTree::display(tree_node *ptr, int level)
{
    int i;
    if (ptr != NULL)
    {
        display(ptr->right, level+1);
        cout<<endl;
        if (ptr == root)
            cout<<"Root->:  ";
        else
        {
            for (i = 0;i < level;i++)
                cout<<"       ";
    }
        cout<<ptr->data;
        display(ptr->left, level+1);
    }
}

case 5:  cout<<"Display BST:"<<endl;
         b.display(tmp,1);
         cout<<endl;
         break;

void BinarySearchTree::display(tree_node *ptr, int level) 设为私有并创建另一个 public 函数:

void BinarySearchTree::display() {
    display(root, 1);
}

注意:不要忘记更新 class 定义。

然后,使用 b.display(); 而不是 b.display(tmp,1);