Пример #1
0
 /// <summary>
 /// Printing inorder traversal
 /// <param name="n">Node to be the root
 /// </summary>
 internal void InorderPrint(BstNode n)
 {
     if (n != null)
     {
         InorderPrint(n.Left);
         Console.WriteLine("{0}->", n.Data);
         InorderPrint(n.Right);
     }
 }
Пример #2
0
        /// <summary>
        /// Inserting node in BST
        /// <param name="d"> Data element in node </param>
        /// </summary>
        public void InsertNode(int d)
        {
            BstNode newBstNode = new BstNode(d);

            if (root == null)
            {
                root = newBstNode;
            }
            else
            {
                BstNode temp = root;

                while (temp != null)
                {
                    if (d < (int)temp.Data)
                    {
                        if (temp.Left == null)
                        {
                            temp.Left = newBstNode;
                            break;
                        }
                        else
                        {
                            temp = temp.Left;
                        }
                    }
                    else
                    {
                        if (temp.Right == null)
                        {
                            temp.Right = newBstNode;
                            break;
                        }
                        else
                        {
                            temp = temp.Right;
                        }
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Constructor initializing an empty binary search tree
 /// </summary>
 public BinarySearchTree()
 {
     root = null;
 }
Пример #4
0
 /// <summary>
 /// Constructor initializing a new node
 /// </summary>
 public BstNode(object d)
 {
     data  = d;
     right = null;
     left  = null;
 }
Пример #5
0
 /// <summary>
 /// Constructor initializing root to null
 /// </summary>
 public HeightBalancedBst(int s)
 {
     root        = null;
     size        = s;
     sortedArray = new int[size];
 }