【问题标题】:Recursive function implementation for Binary search tree二叉搜索树的递归函数实现
【发布时间】:2013-11-15 07:20:55
【问题描述】:

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

我能够创建Node,但AddNode() 函数存在问题。它应该向现有树添加一个新节点,但它replaces 它。

知道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;有什么意义?
  • @PaulDraper:是的,没有特殊情况。谢谢你提出来。但是,你知道我应该如何为AddNode 编写函数吗?

标签: c# .net data-structures tree binary-search-tree


【解决方案1】:

这些行替换了根:

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

【讨论】:

  • @nowhewhomustnotbenamed。你是怎么测试的?显然,这里没有“AddRoot”函数,应该是“AddNode”。不是吗?
  • @RajeshMishra 已修复。
【解决方案2】:

此语句仅在您第一次运行代码时为真。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2014-01-02
    • 2019-05-03
    • 2019-04-24
    • 2012-07-11
    • 2021-07-03
    相关资源
    最近更新 更多