开发者

Binary tree in C#

开发者 https://www.devze.com 2023-01-31 05:57 出处:网络
I\'m trying to build a binary tree, but from some reason my code doesn\'t work. Can somebody please help me?

I'm trying to build a binary tree, but from some reason my code doesn't work. Can somebody please help me? I enter random numbers, and it... I can't really explain it, it best to run it yourself and see the result in debug;

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            leaf root = new leaf();
            while (true)
            {
                string read = Console.ReadLine();
                if (r开发者_如何学JAVAead == "print")
                {
                    root.print();
                }
                else
                {
                    root.grow(Convert.ToInt32(Console.ReadLine()));
                }
            }
        }
    }
    class leaf
    {
        public void print()
        {

        }
        public void grow(int value)
        {
            if (isNull)
            {
                isNull = false;
                this.value = value;
                smaller = bigger = new leaf();
            }
            else
            {
                if (value > this.value)
                {
                    bigger.grow(value);
                }
                else
                {
                    smaller.grow(value);
                }
            }
        }
        public bool isNull = true;
        public int value;
        leaf smaller;
        leaf bigger;
    }
}

The problem: For the input: 1 2 3 4 13 6 4 7 8

It generates the following tree(Skips numbers, and I don't know why):

   2
  / \
     4
    / \
       6
      / \
         7
        / \


This is your problem, I suspect:

smaller = bigger = new leaf();

You've created one object, and assigned a reference to it for both smaller and bigger. Therefore calling smaller.grow() is equivalent to calling bigger.grow().

Try this:

smaller = new leaf();
bigger = new leaf();

(As a side note, I'd strongly recommend that you start following the .NET naming conventions to make your code easier to read for other developers, and more consistent with other code.)


My guess is that if you wrote precisely what you're doing the answer would be more clear.

You write that the input is: 1 2 3 4 13 6 4 7 8 but that would result in nothing the line:

string read = Console.ReadLine();

would consume it and loop to the same line waiting for input. My guess is that your actual input is:

1

2

3

13

6

4

7

8

but swince every other of those would be consumed by the above line only 2,4,6,7 would be consumed by the line:

root.grow(Convert.ToInt32(Console.ReadLine()));

which corresponds to your result. changing that last line (after making the change Jon suggests) to:

root.grow(Convert.ToInt32(read));

will do the trick (if my assumptions on your actual input is correct)


You should look this

http://www.codeproject.com/KB/recipes/BinarySearchTree.aspx

http://www.codeproject.com/KB/recipes/BinarySearchTree/Demo_and_Source_BinarySearchTree.zip

0

精彩评论

暂无评论...
验证码 换一张
取 消