Gilad Naaman
Gilad Naaman

Reputation: 6550

Binary tree in C#

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 (read == "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
        / \

Upvotes: 1

Views: 1516

Answers (3)

Rune FS
Rune FS

Reputation: 21742

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)

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500365

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.)

Upvotes: 7

Related Questions