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);
}
}
}
精彩评论