【问题标题】:Logical error BST ... wrong results逻辑错误 BST ... 错误结果
【发布时间】:2009-01-02 18:04:39
【问题描述】:

嗨,我在 Marc Gravell 的帮助下完成了这段代码

Why can't I find _left and _right in BinarySearchTree?
&
How do I correct an implicit conversion error in BST C# Code?

,它的二进制搜索树,但现在我遇到了逻辑错误,结果是错误的,我的代码输出如下:

2
3
5
6
10
17
------------------------------------------------
17
2
------------------------------------------------
3                 
6
Press any key to continue . . .

最后两个数字必须给出插入元素的总数 6 但显示为 9

但是我怎样才能得到树的高度?!


using System;
using System.Collections.Generic;
using System.Text;

namespace errors
{
    class Program
    {
        static void Main(string[] args)
        {
            BinarySearchTree t = new BinarySearchTree();

            t.insert(ref t.root, 10);
            t.insert(ref t.root, 5);
            t.insert(ref t.root, 6);
            t.insert(ref t.root, 17);
            t.insert(ref t.root, 2);
            t.insert(ref t.root, 3);

            BinarySearchTree.print(t.root);

            Console.WriteLine("------------------------------------------------");
            Console.WriteLine(t.FindMax());
            Console.WriteLine(t.FindMin());
            Console.WriteLine("------------------------------------------------");

            Console.WriteLine(t.CountLeaves(t.root));
            Console.WriteLine(t.CountNodes(t.root));

        }

        public class TreeNode
        {
            public int n;
            public TreeNode _left;
            public TreeNode _right;


            public TreeNode(int n, TreeNode _left, TreeNode _right)
            {
                this.n = n;
                this._left = _left;
                this._right = _right;
            }


            public void DisplayNode()
            {
                Console.Write(n);
            }


        }


        public class BinarySearchTree
        {
            public TreeNode root;


            public BinarySearchTree()
            {
                root = null;
            }


            public void insert(ref TreeNode root, int x)
            {
                if (root == null)
                {
                    root = new TreeNode(x, null, null);
                }
                else
                    if (x < root.n)
                        insert(ref root._left, x);
                    else
                        insert(ref root._right, x);
            }

            public int FindMin()
            {
                TreeNode current = root;

                while (current._left != null)
                    current = current._left;

                return current.n;
            }

            public int FindMax()
            {
                TreeNode current = root;

                while (current._right != null)
                    current = current._right;

                return current.n;
            }



            public TreeNode Find(int key)
            {
                TreeNode current = root;

                while (current.n != key)
                {
                    if (key < current.n)
                        current = current._left;
                    else
                        current = current._right;
                    if (current == null)
                        return null;
                }
                return current;
            }



            public void InOrder(ref TreeNode root)
            {
                if (root != null)
                {
                    InOrder(ref root._left);
                    root.DisplayNode();
                    InOrder(ref root._right);
                }
            }

            public int CountNodes(TreeNode root)
            {
                int count=1;
                if (root._left != null)
                    count += CountNodes(root._left);
                if (root._right != null)
                    count += CountNodes(root._right);
                return count;
            }

            public int CountLeaves(TreeNode root)
            {
                int count = (root._left == null && root._right == null) ? 1 : 0;
                if (root._left != null)
                    count += CountLeaves(root._left);
                if (root._right != null)
                    count += CountLeaves(root._right);
                return count;
            }


            public static void print(TreeNode root)
            {
                if (root != null)
                {
                    print(root._left);
                    Console.WriteLine(root.n.ToString());
                    print(root._right);
                }

            }



        }

    }
}

提前致谢并特别感谢 Marc Gravell。

【问题讨论】:

  • 已关闭 - 重复。如果它与同一问题相关(原样),您需要修改原始问题并将其添加到该问题中。
  • 家庭作业,顺便说一句。叹。 stackoverflow.com/users/50840/bugs-bunny
  • 我不否认,但我忘了添加链接
  • 重点是针对特定主题的一个问题。所有这些都将通过编辑转移到一个问题。

标签: c# binary-tree


【解决方案1】:

如果您在CountNodes 中的意思是计算所有非叶节点,则必须更改此行:

int count=1;

阅读本文:

int count = (root._left == null && root._right == null) ? 0 : 1;

(与 CountLeaves 中的相反)。

这将为您提供树的高度:

public int Height(TreeNode root)
{
    int height = 1;
    if (root._left != null)
        height = Math.Max(height, Height(root._left));
    if (root._right != null)
        height = Math.Max(height, Height(root._right));
    return height;   
}

【讨论】:

  • 是的,它计算所有节点,而不仅仅是所有内部节点。
  • 我忘记了参数。现在修好了。
【解决方案2】:

关于获取树高,使用如下递归伪代码,调用nodeHeight(root)

nodeHeight(node)
    left=0
    right=0

    if (node == null) return 0

    left = 1 + nodeHeight(getLeft(node))
    right = 1 + nodeHeight(getRight(node))

    return max(left,right)

【讨论】:

    【解决方案3】:

    您似乎没有平衡您的树,因此您只获得了一个简单的链表,这可能会导致您的值不正确。一个适当平衡的树将具有 log2(n) 的最大高度。

    【讨论】:

    • 好评论,但不是问题的根源。
    猜你喜欢
    • 2017-05-08
    • 1970-01-01
    • 2013-03-09
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多