|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +class Node |
| 6 | +{ |
| 7 | + public int date; |
| 8 | + public Node left; |
| 9 | + public Node right; |
| 10 | + |
| 11 | + public Node(int value) |
| 12 | + { |
| 13 | + this.date = value; |
| 14 | + this.left = null; |
| 15 | + this.right = null; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class BST |
| 20 | +{ |
| 21 | + Node root = null; // empty BST |
| 22 | + |
| 23 | + private void AddHelper(Node temp, int value) // Private method with recursion |
| 24 | + { |
| 25 | + if (value <= temp.date) |
| 26 | + { |
| 27 | + if (temp.left == null) |
| 28 | + { |
| 29 | + temp.left = new Node(value); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + AddHelper(temp.left, value); |
| 34 | + } |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + if (temp.right == null) |
| 39 | + { |
| 40 | + temp.right = new Node(value); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + AddHelper(temp.right, value); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + private int GetMaxHelper(Node temp) // Private method with recursion |
| 49 | + { |
| 50 | + if (temp.right == null) |
| 51 | + { |
| 52 | + return temp.date; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + return GetMaxHelper(temp.right); |
| 57 | + } |
| 58 | + } |
| 59 | + private int GetMinHelper(Node temp) // Private method with recursion |
| 60 | + { |
| 61 | + if (temp.left == null) |
| 62 | + { |
| 63 | + return temp.date; |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + return GetMaxHelper(temp.left); |
| 68 | + } |
| 69 | + } |
| 70 | + private int GetHeightHelper(Node temp) // Private method with recursion |
| 71 | + { |
| 72 | + if (temp == null) |
| 73 | + { |
| 74 | + return -1; // height of empty subtree , after the leaf node |
| 75 | + } |
| 76 | + |
| 77 | + int Left_SubTree = GetHeightHelper(temp.left); |
| 78 | + int Right_SubTree = GetHeightHelper(temp.right); |
| 79 | + |
| 80 | + return 1 + Math.Max(Left_SubTree, Right_SubTree); // apply returning recursive on every node from empty node ==> leaf ==> parent tree ==> ....etc head |
| 81 | + } |
| 82 | + //-------------------------------------------------------------------------------------------------------------------------- |
| 83 | + public void Add(int value) |
| 84 | + { |
| 85 | + if (root == null) |
| 86 | + { |
| 87 | + root = new Node(value); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + AddHelper(root, value); // method to search which place on BTS conditions to put the newNode with recursion |
| 92 | + } |
| 93 | + } |
| 94 | + public int GetMax() |
| 95 | + { |
| 96 | + return GetMaxHelper(root); |
| 97 | + } |
| 98 | + public int GetMin() |
| 99 | + { |
| 100 | + return GetMinHelper(root); |
| 101 | + } |
| 102 | + public int GetHeight() |
| 103 | + { |
| 104 | + if (root == null) |
| 105 | + { |
| 106 | + return -1; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + return GetHeightHelper(root); |
| 111 | + } |
| 112 | + } |
| 113 | + public void Display_LevelOrder() |
| 114 | + { |
| 115 | + if (root == null) { return; } |
| 116 | + |
| 117 | + Queue<Node> q = new Queue<Node>(); |
| 118 | + q.Enqueue(root); |
| 119 | + |
| 120 | + while (q.Count != 0) |
| 121 | + { |
| 122 | + Node current = q.Peek(); |
| 123 | + q.Dequeue(); |
| 124 | + |
| 125 | + Console.WriteLine(current.date); |
| 126 | + |
| 127 | + if (current.left != null) { q.Enqueue(current.left); } |
| 128 | + if (current.right != null) { q.Enqueue(current.right); } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +class Solution |
| 135 | +{ |
| 136 | + static void Main(string[] args) |
| 137 | + { |
| 138 | + // Testing |
| 139 | + BST sT = new BST(); |
| 140 | + sT.Add(8); |
| 141 | + sT.Add(5); |
| 142 | + sT.Add(13); |
| 143 | + sT.Add(3); |
| 144 | + sT.Add(7); |
| 145 | + sT.Add(10); |
| 146 | + sT.Add(15); |
| 147 | + |
| 148 | + sT.Display_LevelOrder(); |
| 149 | + } |
| 150 | +} |
| 151 | +/* |
| 152 | + ====================================> Add Method with loop <======================== |
| 153 | + public void add(int value) |
| 154 | + { |
| 155 | + Node newNode = new Node(); |
| 156 | + newNode.date = value; |
| 157 | + newNode.left = null; |
| 158 | + newNode.right = null; |
| 159 | +
|
| 160 | + if (root == null) |
| 161 | + { |
| 162 | + root = newNode; |
| 163 | + return; |
| 164 | + } |
| 165 | +
|
| 166 | + Node temp = root; // equal Root ,to search for suitable place to Put newNode in BST ... in BST built conditions |
| 167 | + Node parent = null; |
| 168 | +
|
| 169 | + while (temp != null) |
| 170 | + { |
| 171 | + parent = temp; |
| 172 | + if (value <= temp.date) // based on BST condition the Node will go to Left |
| 173 | + { |
| 174 | + temp = temp.left; |
| 175 | + } |
| 176 | + else // based on BST condition the Node will go to Right |
| 177 | + { |
| 178 | + temp = temp.right; |
| 179 | + } |
| 180 | + } |
| 181 | +
|
| 182 | + if (value <= parent.date) |
| 183 | + { |
| 184 | + parent.left = newNode; |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + parent.right = newNode; |
| 189 | + } |
| 190 | + } |
| 191 | + ================================================> Min/Max with Loop <================================== |
| 192 | + public int getMax() |
| 193 | + { |
| 194 | + Node temp = root; |
| 195 | + while (temp.right != null) |
| 196 | + { |
| 197 | + temp = temp.right; |
| 198 | + } |
| 199 | + return temp.date; |
| 200 | + } |
| 201 | + public int getMin() |
| 202 | + { |
| 203 | + Node temp = root; |
| 204 | + while (temp.left != null) |
| 205 | + { |
| 206 | + temp = temp.left; |
| 207 | + } |
| 208 | + return temp.date; |
| 209 | + } |
| 210 | + */ |
0 commit comments