二叉搜索树的递归函数实现

3

我正在尝试实现一个基本的二叉搜索树。

我已经创建了一个Node,但在AddNode()函数中遇到了问题。它应该将新节点添加到现有树中,但是它会替换掉原来的节点。

您有什么关于AddNode()函数该怎么做的想法吗?

class Node
{
    public int value { get; set; }

    public Node left { get; set; }

    public Node right { get; set; }

    public Node(int i)
    {
        this.value = i;
        this.left = null;
        this.right = null;
    }

}

class Tree
{
    public Node root { get; set; }

    public Tree(Node n)
    {
        root = n;
    }

    public void AddNode(int valueToBeInserted)
    {
        if (this.root == null)
        {
            this.root = new Node(valueToBeInserted); 
            // problem here : existing tree is destroyed. 
            // a new one is created. 
            // instead it should add a new node to the end of the tree if its null
        }

        if (valueToBeInserted < this.root.value)
        {
            this.root = this.root.left;
            this.AddNode(valueToBeInserted);
        }

        if (valueToBeInserted > this.root.value)
        {
            this.root = this.root.right;
            this.AddNode(valueToBeInserted);
        }
    }

    public void printTree()
    {
        // print recursively the values here.
    }
}

class TreeTest
{
    public void Test()
    {
        var tree = new Tree(new Node(100));
        tree.AddNode(20);
        tree.AddNode(100);
    }
}

感谢您的选择。

public int value { get; set; }而不是public int value;的意义是什么? - Paul Draper
@PaulDraper:是的,没有特殊情况。感谢你提出这个问题。但是,你知道我应该如何编写“AddNode”函数吗? - now he who must not be named.
2个回答

4
这些行替换了根目录:
this.root = this.root.left;
this.root = this.root.right;

您应该给递归函数传入一个参数。

您也可以移除 this 限定符 - 它们只有在您有一个同名局部变量或其他一些特殊情况时才是必需的。

添加一个辅助函数很有用 / 必要,因为根必须单独处理。

更新后的代码:

public void AddNode(int valueToBeInserted)
{
    if (root == null)
    {
        root = new Node(valueToBeInserted);
    }
    else
    {
        AddNode(valueToBeInserted, root);
    }
}

private void AddNode(int valueToBeInserted, Node current)
{
    if (valueToBeInserted < current.value)
    {
        if (current.left == null)
            current.left = new Node(valueToBeInserted);
        else
            AddNode(valueToBeInserted, current.left);
    }

    if (valueToBeInserted > current.value)
    {
        if (current.right == null)
            current.right = new Node(valueToBeInserted);
        else
            AddNode(valueToBeInserted, current.right);
    }
}

@现在不该被提及的人。你是怎么测试它的?很明显,这里没有"AddRoot"函数,应该是"AddNode"。不是吗? - Rajesh Mishra
@RajeshMishra 已修复。 - Bernhard Barker

1
这个语句只有在你第一次运行代码时才会成立。
if (this.root == null)
{
    this.root = new Node(valueToBeInserted);
}

没有任何地方将 this.root 再次设置为 null... 通常,您会这样编写 add:


public void AddNode(int valueToBeInserted)
{
    if (this.root == null)
    {
        this.root = new Node(valueToBeInserted); 
    }

    if (valueToBeInserted < this.root.value)
    {
        this.root.left = this.AddNode(valueToBeInserted);
        this.root = this.root.left;
    }

    if (valueToBeInserted > this.root.value)
    {
        this.root.right = this.AddNode(valueToBeInserted);
        this.root = this.root.right;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接