예제 #1
0
 /// <summary>
 /// Constructs an RBTNode with Data of a given type. Children default to null.
 /// </summary>
 /// <param name="data">Data of type TData held by this node.</param>
 /// <param name="leftChild">Left child of this Node.
 /// The left child should return 1 when compared to this node.</param>
 /// <param name="rightChild">Right child of this Node.
 /// The right child should return -1 when compared to this node</param>
 public RBTNode(TData data, RBTNode <TData> leftChild = null, RBTNode <TData> rightChild = null, NodeColor color = NodeColor.RED)
 {
     this.data       = data;
     this.leftChild  = leftChild;
     this.rightChild = rightChild;
     this.color      = color;
 }
예제 #2
0
 public void Free()
 {
     Console.WriteLine("Freeing " + Data.ToString());
     Data       = default(TData);
     leftChild  = null;
     rightChild = null;
     parent     = null;
 }
예제 #3
0
        static void Main(string[] args)
        {
            RedBlackTree <int> myRBT    = new RedBlackTree <int>();
            String             input    = "";
            RBTNode <int>      currNode = null;

            while (input != "done")
            {
                Console.WriteLine();
                Console.WriteLine("Enter a command");
                input = Console.ReadLine();

                switch (input)
                {
                case "add":
                    Console.WriteLine("Enter data to be added to tree");
                    myRBT.Add(new RBTNode <int>(int.Parse(Console.ReadLine())));
                    break;

                case "print":
                    myRBT.Print();
                    break;

                case "remove":
                    Console.WriteLine("Enter data to be removed from tree");
                    myRBT.Remove(new RBTNode <int>(int.Parse(Console.ReadLine())));
                    break;

                case "root":
                    currNode = (RBTNode <int>)myRBT.Root;
                    Console.WriteLine(currNode.Data);
                    break;

                case "right":
                    try
                    {
                        currNode = (RBTNode <int>)currNode.RightChild;
                    }
                    catch (NullReferenceException e) { Console.WriteLine(currNode.Data + " does not have a right child!"); }
                    Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    break;

                case "left":
                    try {
                        currNode = (RBTNode <int>)currNode.LeftChild;
                        Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    }
                    catch (NullReferenceException e) { Console.WriteLine(currNode.Data + " does not have a left child!"); }
                    break;

                case "parent":
                    try
                    {
                        currNode = (RBTNode <int>)currNode.Parent;
                        Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    }
                    catch (NullReferenceException e) { Console.WriteLine(currNode.Data + " does not have a parent!"); }
                    break;

                case "curr":
                    Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    break;
                }
            }
        }