├── .gitattributes └── BinarySearchTree.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /BinarySearchTree.h: -------------------------------------------------------------------------------- 1 | #define BST_H 2 | 3 | #include 4 | #include 5 | #include"linked_list.h" 6 | #include 7 | using namespace std; 8 | 9 | class BST{ 10 | private: 11 | /***********************************/ 12 | //DATA MEMBER 13 | /***********************************/ 14 | 15 | struct Node { 16 | int data; 17 | Node * leftChild = NULL; 18 | Node * rightChild = NULL; 19 | }; 20 | Node * root; 21 | 22 | /***********************************/ 23 | //RECURSIVE FUNCTIONS 24 | /***********************************/ 25 | 26 | void insert(Node*& parent, int data) {//insert child in tree by recursively 27 | if (parent == NULL) {// if parent become NULL 28 | parent = new Node; 29 | parent->data = data; 30 | return; 31 | } 32 | else { 33 | if (data > parent->data) { 34 | insert(parent->rightChild, data); //check right child 35 | } 36 | else if (data < parent->data) { 37 | insert(parent->leftChild, data);//check left child 38 | } 39 | else { 40 | cout << "\nData already present"; 41 | return; 42 | } 43 | } 44 | } 45 | int height(Node* parent) { //find height of tree by recursively 46 | if (parent == NULL) { 47 | return 0; 48 | } 49 | else if(parent!=NULL) { 50 | int rightDepth = height(parent->rightChild); 51 | int leftDepth = height(parent->leftChild); 52 | 53 | //return the greates height 54 | int Height; 55 | if (leftDepth > rightDepth) { 56 | Height = leftDepth + 1; 57 | } 58 | else { 59 | Height = rightDepth + 1; 60 | } 61 | return Height; 62 | } 63 | 64 | } 65 | void printLeftToRight(Node* root, int currentLevel, int level) {// print left to right child data of same level 66 | if (root != NULL) { // check is parent NULL 67 | if (currentLevel == level) {// is level came or not 68 | cout << root->data << " "; 69 | return; 70 | } 71 | } 72 | else { 73 | return; 74 | } 75 | 76 | printLeftToRight(root->leftChild, currentLevel + 1, level); 77 | printLeftToRight(root->rightChild, currentLevel + 1, level); 78 | } 79 | void printRightToLeft(Node* root, int currentLevel, int level) { // print right to left child data of same level 80 | if (root != NULL) { // check is parent NULL 81 | if (currentLevel == level) {// is level came or not 82 | cout << root->data << " "; 83 | return; 84 | } 85 | } 86 | else { 87 | return; 88 | } 89 | printRightToLeft(root->rightChild, currentLevel + 1, level); 90 | printRightToLeft(root->leftChild, currentLevel + 1, level); 91 | } 92 | void BinaryMirrorTree(Node* MirrorParent) { 93 | if (MirrorParent != NULL) { 94 | //swap left child with right child of the poarent 95 | Node*temp = MirrorParent->rightChild; 96 | MirrorParent->rightChild = MirrorParent->leftChild; 97 | MirrorParent->leftChild = temp; 98 | 99 | BinaryMirrorTree(MirrorParent->leftChild); 100 | BinaryMirrorTree(MirrorParent->rightChild); 101 | } 102 | else { 103 | return; 104 | } 105 | } 106 | void deleteTree(Node * parent) { 107 | if (parent == NULL) { 108 | return; 109 | } 110 | else { // post order 111 | deleteTree(parent->leftChild); // delte left leef nodes 112 | deleteTree(parent->rightChild);//delete right leef Nodes 113 | delete parent; 114 | } 115 | } 116 | void flattenedTree(Node *parent, linkedList& flattened) { // receive Node and linked List for flattened tree 117 | if (parent == NULL) { // if parent Null then return 118 | return; 119 | } 120 | flattened.insertAtEnd(parent->data); 121 | flattenedTree(parent->leftChild, flattened); // check left sub tree 122 | flattenedTree(parent->rightChild, flattened);// check right sub tree 123 | } 124 | 125 | void printNodesAtLevel(Node* root, int currentLevel, int level, int tempspaces) { 126 | int spaces = tempspaces + tempspaces - 1; 127 | 128 | if (root == NULL) { 129 | cout << " "; 130 | for (int k = 0; k < spaces; k++) { 131 | cout << " "; 132 | } 133 | return; 134 | } 135 | if (currentLevel == level) { 136 | cout << root->data; 137 | for (int k = 0; k < spaces; k++) { 138 | cout << " "; 139 | } 140 | return; 141 | } 142 | 143 | printNodesAtLevel(root->leftChild, currentLevel + 1, level, tempspaces); 144 | printNodesAtLevel(root->rightChild, currentLevel + 1, level, tempspaces); 145 | } 146 | void print(Node*parent, int height) { 147 | Node * temp = parent; 148 | for (int i = 0; i < height; i++) { 149 | int element = pow(2, i); 150 | int tempspaces = pow(2, height - (i + 1)); 151 | 152 | for (int j = 0; j < tempspaces; j++) { // print first spaces 153 | cout << " "; 154 | } 155 | printNodesAtLevel(parent, 0, i, tempspaces); 156 | cout << endl; 157 | } 158 | } 159 | 160 | void copyData(Node *parent, BST& copy) { // receive parents and BST tree for copy data 161 | if (parent == NULL) { // if parent Null then return 162 | return; 163 | } 164 | copy.insert(parent->data); 165 | copyData(parent->leftChild, copy); // check left sub tree 166 | copyData(parent->rightChild, copy);// check right sub tree 167 | } 168 | 169 | void copyData(BST & tree) {// copy data of caller tree 170 | tree.root = NULL; 171 | this->copyData(this->root, tree); 172 | } 173 | void insertinMirror(Node*& parent, int data) {//insert child in tree by recursively with reverse property 174 | //reverse property means small number enter at left side and larger at right side 175 | if (parent == NULL) {// if parent become NULL 176 | parent = new Node; 177 | parent->data = data; 178 | return; 179 | } 180 | else { 181 | if (data < parent->data) { 182 | insertinMirror(parent->rightChild, data); //check left child 183 | } 184 | if (data > parent->data) { 185 | insertinMirror(parent->leftChild, data);//check right child 186 | } 187 | } 188 | } 189 | void printLevelWise(Node* root, int currentLevel, int level) {// print left to right child data of same level 190 | if (root != NULL) { // check is parent NULL 191 | if (currentLevel == level) {// is level came or not 192 | cout << "\nThis node Address " << root; 193 | cout << "\nLeft Child Address : " << root->leftChild; 194 | cout << "\nRight Child Address : " << root->rightChild; 195 | cout << "\nData : " << root->data; 196 | cout << "\n.........................................\n"; 197 | return; 198 | } 199 | } 200 | else { 201 | return; 202 | } 203 | printLevelWise(root->leftChild, currentLevel + 1, level); 204 | printLevelWise(root->rightChild, currentLevel + 1, level); 205 | } 206 | public: 207 | BST() { 208 | root = NULL; 209 | } 210 | 211 | /***********************************/ 212 | //Helper functions 213 | /***********************************/ 214 | Node* getRoot() { 215 | return this->root; 216 | } 217 | void insert(int data) { 218 | if (root == NULL) { //if root Null put Node on root and put data in it; 219 | root = new Node; 220 | root->data = data; 221 | } 222 | else { 223 | insert(root, data); 224 | } 225 | } 226 | int height() { 227 | return height(root); 228 | } 229 | void SpiralOrderTraversal() { // print tree in spiral Form 230 | cout << "\nData printing in spiral order : "; 231 | int Height = this->height(); 232 | for (int i = 0; i < Height; i++) {// change level of tree 233 | if (i % 2 == 0) { 234 | printRightToLeft(root, 0, i); 235 | } 236 | else { 237 | printLeftToRight(root, 0, i); 238 | } 239 | } 240 | } 241 | 242 | int DiameterOfBinaryTree(Node*Parent) { 243 | if (Parent == NULL) { 244 | return 0; 245 | } 246 | //check height 247 | int leftHeight = this->height(Parent->leftChild);// height of left sub tree 248 | int rightHeight = this->height(Parent->rightChild);// height of right sub tree 249 | //Now check Diameter 250 | int rightDiameter = this->DiameterOfBinaryTree(Parent->rightChild);// check the diameter of left child 251 | int leftDiameter = this->DiameterOfBinaryTree(Parent->leftChild);// check the diameter of left child 252 | 253 | if (leftDiameter > rightDiameter) { // check which diameter greater 254 | if ((leftHeight + rightHeight) > leftDiameter) {// compare height and diamter and return dreater value 255 | return (leftHeight + rightHeight); 256 | } 257 | else { 258 | return leftDiameter; 259 | } 260 | } 261 | else { 262 | if ((leftHeight + rightHeight) > rightDiameter) {// compare height and diamter and return dreater value 263 | return (leftHeight + rightHeight); 264 | } 265 | else { 266 | return rightDiameter; 267 | } 268 | } 269 | } 270 | 271 | void BinaryMirrorTree(BST& mirror) { 272 | this->ItrativePreorder(mirror); 273 | } 274 | void ItrativePreorder(BST & mirror) { 275 | //operation = root , right child, left child push into stack 276 | mirror.root = NULL; 277 | if (this->root == NULL) 278 | return; 279 | Node*tempRoot = this->root; 280 | stack s; 281 | s.push(tempRoot); 282 | while (!s.empty()) { 283 | tempRoot = s.top(); //point last root enter in stak 284 | s.pop(); 285 | mirror.insertinMirror(mirror.root,tempRoot->data);//enter data in mirror tree 286 | 287 | if (tempRoot->rightChild != NULL) { 288 | s.push(tempRoot->rightChild); 289 | } 290 | if (tempRoot->leftChild != NULL) { 291 | s.push(tempRoot->leftChild); 292 | } 293 | } 294 | } 295 | 296 | void flattenedTree(linkedList& flattened) { 297 | flattened.deleteList(); // if list already consist of any data 298 | flattenedTree(root, flattened); // call recursive function 299 | } 300 | void print() {//print data of tree in simple form; 301 | int Height = height(this->root); 302 | for (int i = 0; i < Height; i++) { 303 | cout << "\n________________________________________\n"; 304 | cout << " LEVEL " << i << endl; 305 | printLevelWise(this->root, 0, i); 306 | } 307 | } 308 | 309 | void Printintreeform() { 310 | if (root != NULL) { 311 | cout << "It's not good way but you can observe many thisngs in this way :(\n"; 312 | print(root, height(root)); 313 | cout << "\n\n\t\t for more clerefication : "; 314 | this->print(); 315 | } 316 | else { 317 | cout << "\nEmpty :( "; 318 | } 319 | } 320 | 321 | ~BST() { 322 | this->deleteTree(this->root); 323 | } 324 | }; --------------------------------------------------------------------------------