开发者

binary tree in C# [closed]

开发者 https://www.devze.com 2023-03-10 01:52 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How would you do a binary tree in C# that is simple, straight forward, and does not use any predefined classes? I am talking about something simple like you would do in C++

Nothing like NGenerics Objects that represent trees

I mean something that starts with something simple, like this:

struct
{
  Node * left
  Node * right
  int value;
}

Follow up question:

OK, so if I have this:

public class binarytreeNode
{
    public binarytreeNode Left;
    public binarytreeNode Right;
    public int data;

}

Would I have to put the methods that act upon the node, inside this class? Doesn't this make this no longer a Node?

If I create a method for adding a node inside the P开发者_如何学JAVArogram class:

class Program
{       
     public binarytreeNode AddNode(int value)
    {
        binarytreeNode newnode = new binarytreeNode();
        newnode.Left = null;
        newnode.Right = null;
        newnode.data = value;
        return newnode;
    }
     static void Main(string[] args)
    {
        binarytreeNode head = AddNode(4);

    }
}

The compiler says that an object reference is required for my call to AddNode. Why?


class Node<T>
{
    public Node<T> Left, Right;
    public T Value;
}


class Node
{
  public Node left, right;
  public int value;
}


namespace ConsoleApplication1
{
    public class binarytreeNode
    {
        public binarytreeNode Left;
        public binarytreeNode Right;
        public int data;

    }
    public class binarytree
    {
        public binarytreeNode AddNode(int value)
            {
                binarytreeNode newnode = new binarytreeNode();
                newnode.Left = null;
                newnode.Right = null;
                newnode.data = value;
                return newnode;
            }
    }
    class Program
    {       

         static void Main(string[] args)
        {
            binarytree mybtree = new binarytree();

            binarytreeNode head = mybtree.AddNode(4);

        }
    }
}
0

精彩评论

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