Example #1
0
 public override string ToString()
 {
     string result = "";
     int height = GetHeight (this);
     Console.WriteLine ("height = " + height);
     BTNode dummy = new BTNode (-1);
     List<BTNode> curLevel = new List<BTNode>();
     curLevel.Add (this);
     for (int i = 0; i < height; ++i) {
         string indent = new string ('\t', (int)Math.Pow (2, height - i - 1));
         string sep = new string ('\t', (int)Math.Pow (2, height - i));
         result += indent;
         List<BTNode> nextLevel = new List<BTNode> ();
         foreach (BTNode n in curLevel) {
             if (n == dummy)
                 result += sep;
             else
                 result += n.data.ToString() + sep;
             nextLevel.Add (n.left ?? dummy);
             nextLevel.Add (n.right ?? dummy);
         }
         result += '\n';
         curLevel = nextLevel;
     }
     return result;
 }
Example #2
0
 public static BTNode CreateMinimalBST(int[] arr, int left, int right)
 {
     if (left > right)
         return null;
     int mid = left + (right - left) / 2;
     BTNode n = new BTNode (arr [mid]);
     n.left = CreateMinimalBST (arr, left, mid - 1);
     n.right = CreateMinimalBST (arr, mid + 1, right);
     return n;
 }
Example #3
0
 private int GetHeight(BTNode node)
 {
     if (node == null)
         return 0;
     int leftHeight = GetHeight (node.left);
     int rightHeight = GetHeight (node.right);
     return 1 + Math.Max (leftHeight, rightHeight);
 }
Example #4
0
 public BTNode(int data)
 {
     this.data = data;
     this.parent = null;
 }
Example #5
0
 public BTNode(int data)
 {
     this.data   = data;
     this.parent = null;
 }