示例#1
0
        public void Insert(T item)
        {
            T currentNodeValue = this.NodeData;

            if ((currentNodeValue.CompareTo(item)) > 0)
            {
                if (this.leftTree == null)
                {
                    this.leftTree = new BinarySearchTreeDemo <T>(item);
                }
                else
                {
                    this.leftTree.Insert(item);
                }
            }
            else
            {
                if (this.rightTree == null)
                {
                    this.rightTree = new BinarySearchTreeDemo <T>(item);
                }
                else
                {
                    this.rightTree.Insert(item);
                }
            }
        }
示例#2
0
 public bool IfExists(T element, BinarySearchTreeDemo <T> node)
 {
     if (node == null)
     {
         return(false);
     }
     if (node.NodeData.Equals(element))
     {
         Console.WriteLine("Found the element in BST" + " " + node.NodeData);
         result = true;
     }
     else
     {
         Console.WriteLine("Current element is {0} in BST", node.NodeData);
     }
     if (element.CompareTo(node.NodeData) < 0)
     {
         IfExists(element, node.leftTree);
     }
     if (element.CompareTo(node.NodeData) > 0)
     {
         IfExists(element, node.rightTree);
     }
     return(result);
 }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Binary Search Tree Demo!");
            BinarySearchTreeDemo <int> binarSearchTree = new BinarySearchTreeDemo <int>(56);

            binarSearchTree.Insert(30);
            binarSearchTree.Insert(70);
            binarSearchTree.Insert(22);
            binarSearchTree.Insert(40);
            binarSearchTree.Insert(60);
            binarSearchTree.Insert(95);
            binarSearchTree.Insert(11);
            binarSearchTree.Insert(65);
            binarSearchTree.Insert(3);
            binarSearchTree.Insert(16);
            binarSearchTree.Insert(63);
            binarSearchTree.Insert(67);
            binarSearchTree.Display();
            binarSearchTree.GetSize();
            bool result = binarSearchTree.IfExists(63, binarSearchTree);

            Console.WriteLine(result);
            Console.ReadKey();
        }
示例#4
0
 public BinarySearchTreeDemo(T nodeData)
 {
     this.NodeData  = nodeData;
     this.rightTree = null;
     this.leftTree  = null;
 }