コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Binary Search Tree Program");
            BinarySTree <int> binarySearch = new BinarySTree <int>(56);

            binarySearch.Insert(30);
            binarySearch.Insert(70);
            binarySearch.Insert(22);
            binarySearch.Insert(40);
            binarySearch.Insert(60);
            binarySearch.Insert(95);
            binarySearch.Insert(11);
            binarySearch.Insert(65);
            binarySearch.Insert(3);
            binarySearch.Insert(16);
            binarySearch.Insert(63);
            binarySearch.Insert(67);

            binarySearch.GetSize();
            binarySearch.Display();
            bool result = binarySearch.Search(63, binarySearch);

            Console.WriteLine();
            Console.WriteLine("The element 63 exists in the BST: " + binarySearch.Search(63, binarySearch));
            Console.Read();
        }
コード例 #2
0
        public void Insert(T item)//create method and pass parameter item
        {
            T currentNodeValue = NodeData;

            if ((currentNodeValue.CompareTo(item)) > 0)
            {
                if (LeftTree == null)
                {
                    LeftTree = new BinarySTree <T>(item);
                }
                else
                {
                    LeftTree.Insert(item);
                }
            }
            else
            {
                if (RightTree == null)
                {
                    RightTree = new BinarySTree <T>(item);
                }
                else
                {
                    RightTree.Insert(item);
                }
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            BinarySTree bt = new BinarySTree();

            bt.Add("QWE 213", 12, 13);
            bt.Add("ASD 837", 9, 11);
            bt.Add("GFY 963", 16, 20);
            bt.Add("FDS 631", 8, 9);
            bt.Add("BRV 074", 11, 13);
            bt.Add("OSJ 984", 14, 21);
            bt.Add("BRV 647", 18, 22);
        }
コード例 #4
0
        //search method to search a node in the binary search tree
        public bool Search(T element, BinarySTree <T> node)
        {
            if (node == null)
            {
                return(false);
            }
            if (node.NodeData.Equals(element))
            {
                result = true;
            }

            else if (node.NodeData.CompareTo(element) < 0)
            {
                Search(element, node.RightTree);
            }
            else
            {
                Search(element, node.LeftTree);
            }
            return(result);
        }
コード例 #5
0
        /// constructor and pass parameter

        public BinarySTree(T nodeData)
        {
            NodeData  = nodeData;
            LeftTree  = null;
            RightTree = null;
        }