Esempio n. 1
0
        static void Main(string[] args)
        {
            //UC2- Inset all the element in the specified node depending upon the value passed by user as item
            // Creating a instance object of MyBinaryTree.
            MyBinaryTree <int> binaryTree = new MyBinaryTree <int>(56);

            Console.WriteLine("Inserting item");
            binaryTree.Insert(30);
            binaryTree.Insert(70);
            binaryTree.Insert(22);
            binaryTree.Insert(40);
            binaryTree.Insert(3);
            binaryTree.Insert(16);
            binaryTree.Insert(11);
            binaryTree.Insert(60);
            binaryTree.Insert(95);
            binaryTree.Insert(65);
            binaryTree.Insert(63);
            binaryTree.Insert(67);
            Console.WriteLine("Displaying Binary Tree");
            binaryTree.Display();
            binaryTree.GetSize();
            Console.WriteLine("===============================");
            //UC3 - Ability to search an element  whether the element is present in the binary tree or not
            Console.WriteLine("Searching element");
            binaryTree.Search(63, binaryTree);
            binaryTree.Search(55, binaryTree);
            Console.ReadLine();
        }
Esempio n. 2
0
 /// <summary>
 ///  UC3- Ability to search an element  whether the element is present in the binary tree or not
 /// </summary>
 /// <param name="element"></param>
 /// <param name="node"></param>
 /// <returns></returns>
 public bool Search(T element, MyBinaryTree <T> node)
 {
     //checking for the node first if not null
     if (node == null)
     {
         Console.WriteLine("Element is not present in the Binary tree is {0}", element);
         return(false);
     }
     // checking for the node data if not equal to element
     if (node.nodeData.Equals(element))
     {
         Console.WriteLine("Element is present in the Binary tree is {0}", element);
         return(true);
     }
     else
     {
         Console.WriteLine("we are currently on node {0}", node.nodeData);
     }
     //then comparing the nodedata with element to search in either left tree or right tree
     if (node.nodeData.CompareTo(element) > 0)
     {
         return(Search(element, node.leftTree));
     }
     else
     {
         return(Search(element, node.rightTree));
     }
 }
Esempio n. 3
0
        /// <summary>
        ///UC1- Inset the element in the specified node depending upon the value passed by user as item
        /// </summary>
        /// <param name="item"></param>
        public void Insert(T item)
        {
            // Creating a currentNodeValue variable copying the value of nodeData into currentNodeValue.
            T currentNodeValue = this.nodeData;

            //comparing if the value entered is greater than current node or less than.
            if (currentNodeValue.CompareTo(item) > 0)
            {
                //if the item value less than currentNodeValue then adding into left tree
                if (this.leftTree == null)
                {
                    this.leftTree = new MyBinaryTree <T>(item);
                    Console.WriteLine("Inserting item on left is : " + item);
                }
                // Recursion remember first it will reach the end node and also traverse back to upper nodes
                else
                {
                    this.leftCount++;
                    this.leftTree.Insert(item);
                }
            }
            else
            {
                //if the item value greater than currentNodeValue then adding into left tree
                if (this.rightTree == null)
                {
                    this.rightTree = new MyBinaryTree <T>(item);
                    Console.WriteLine("Inserting item on right is : " + item);
                }
                // Recursion remember first it will reach the end node and also traverse back to upper nodes
                else
                {
                    this.rightCount++;
                    this.rightTree.Insert(item);
                }
            }
        }
Esempio n. 4
0
 // Creating a parameterized constructor.
 public MyBinaryTree(T nodeData)
 {
     this.nodeData  = nodeData;
     this.leftTree  = null;
     this.rightTree = null;
 }