public void Add(int x)
        {
            NodeBT n = new NodeBT(x);

            if (Root == null)
            {
                Root = n;
                Count++;
                return;
            }
            NodeBT cur = Root;

            while (true)
            {
                if ((cur.Data < n.Data) && (cur.Right != null))
                {
                    cur = cur.Right;
                }
                else if ((cur.Data >= n.Data) && (cur.Left != null))
                {
                    cur = cur.Left;
                }
                else if ((cur.Data < n.Data) && (cur.Right == null))
                {
                    cur.Right = n;
                    return;
                }
                else
                {
                    cur.Left = n;
                    return;
                }
            }
        }
 private void InOrderPrint(NodeBT root)
 {
     if (root == null)
     {
         return;
     }
     InOrderPrint(root.Left);
     Console.WriteLine(root.Data);
     InOrderPrint(root.Right);
 }
コード例 #3
0
        //Returns the data in the farthest right node
        public int GetMax(NodeBT root)
        {
            NodeBT cur = root;

            while (cur.Right != null)
            {
                cur = cur.Right;
            }
            return(cur.Data);
        }
コード例 #4
0
        //Returns the data in the farthest left node
        public int GetMin(NodeBT root)
        {
            NodeBT cur = root;

            while (cur.Left != null)
            {
                cur = cur.Left;
            }
            return(cur.Data);
        }
 public void CallInOrderPrint(NodeBT root)
 {
     InOrderPrint(this.Root);
 }