Пример #1
0
 public void Search(T data)
 {
     if (Parent.data.CompareTo(data) == 0)
     {
         Console.WriteLine("Found");
         return;
     }
     if (Parent.data.CompareTo(data) < 0)
     {
         Parent = Parent.RightChild;
         Search(data);
     }
     else
     {
         Parent = Parent.leftChild;
         Search(data);
     }
 }
Пример #2
0
 public void Display(MyBinaryNode <T> parent)
 {
     Console.WriteLine("Parent : {0}", parent.data);
     if (parent.leftChild != null)
     {
         Console.WriteLine("Left :" + parent.leftChild.data);
     }
     if (parent.RightChild != null)
     {
         Console.WriteLine("Right :" + parent.RightChild.data);
     }
     if (parent.leftChild != null)
     {
         Display(parent.leftChild);
     }
     if (parent.RightChild != null)
     {
         Display(parent.RightChild);
     }
 }
        /// <summary>
        /// UC 1 Adds the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        public void Add(T data)
        {
            // If the tree is empty
            if (rootNode == null)
            {
                rootNode = new MyBinaryNode <T>(data);
                return;
            }
            MyBinaryNode <T> temp = rootNode;

            // If the given element is less than compared then traverse left
            if (data.CompareTo(temp.data) < 0)
            {
                if (temp.leftTree == null)
                {
                    temp.leftTree          = new BinarySearchTree <T>();
                    temp.leftTree.rootNode = new MyBinaryNode <T>(data);
                    return;
                }
                else
                {
                    temp.leftTree.Add(data);
                }
            }

            // If the given element is greater than compared then traverse right
            else if (data.CompareTo(temp.data) > 0)
            {
                if (temp.rightTree == null)
                {
                    temp.rightTree          = new BinarySearchTree <T>();
                    temp.rightTree.rootNode = new MyBinaryNode <T>(data);
                    return;
                }
                else
                {
                    temp.rightTree.Add(data);
                }
            }
        }
Пример #4
0
 public void Add(T data)
 {
     if (root == null)
     {
         root = new MyBinaryNode <T>(data);
     }
     else
     {
         var node = new MyBinaryNode <T>(data);
         var temp = Parent;
         if (data.CompareTo(Parent.data) < 0)
         {
             if (temp.leftChild == null)
             {
                 temp.leftChild = node;
             }
             else
             {
                 Parent = temp.leftChild;
                 Add(data);
             }
         }
         else
         {
             if (temp.RightChild == null)
             {
                 temp.RightChild = node;
             }
             else
             {
                 Parent = temp.RightChild;
                 Add(data);
             }
         }
     }
     Parent = root;
 }
Пример #5
0
 public MyBinaryNode(T data)
 {
     this.data  = data;
     leftChild  = null;
     RightChild = null;
 }
Пример #6
0
 public BinarySearchTree()
 {
     root   = null;
     Parent = null;
 }