bhupixb

All about binary trees

Jan 30th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 83.10 KB | None | 0 0
  1. // Trees.cpp : Defines the entry point for the console application.
  2. //
  3. #include <iostream>
  4. #include <map>
  5. #include <vector>
  6. #include <string>
  7. #include <sstream>
  8. using namespace std;
  9.  
  10. #define MAXSTACKSIZE 1000
  11. #define INF 99999999
  12. #define MINV -987654321
  13. #define MAXV 987654321
  14.  
  15. inline int MAX(int a, int b){
  16. return a > b ? a : b;
  17. }
  18.  
  19. struct lnode{
  20. int data;
  21. struct lnode* next;
  22. struct lnode* previous;
  23. lnode(int mdata){
  24. data = mdata;
  25. next = NULL;
  26. previous = NULL;
  27. }
  28. };
  29.  
  30. lnode* createLNode(int val){
  31. return new lnode(val);
  32. }
  33.  
  34. struct node{
  35. int data;
  36. struct node* left;
  37. struct node* right;
  38. struct node* parent;
  39. struct node* sibiling;
  40. int leftChilds;
  41. int rightChilds;
  42. /*used only for dll*/
  43. struct node* next;
  44. struct node* previous;
  45. /* used only for Balancing */
  46. bool isBalanced;
  47. /* used only for max BST */
  48. bool isBST;
  49. /* used only for max BST */
  50. int leftMin;
  51. int rightMax;
  52. /* used for AVL */
  53. int height;
  54. node(int mdata){
  55. data = mdata;
  56. left = right = NULL;
  57. sibiling = NULL;
  58. next = previous = NULL;
  59. leftChilds = rightChilds = 0;
  60. isBalanced = false;
  61. isBST = false;
  62. leftMin = MINV;
  63. rightMax = MAXV;
  64. height = 1;
  65. }
  66. };
  67.  
  68. struct stack{
  69. int top;
  70. node* arr[MAXSTACKSIZE];
  71. stack(){
  72. top = -1;
  73. }
  74. bool isEmpty(){
  75. return (top == -1);
  76. }
  77. bool isFull(){
  78. return (top == MAXSTACKSIZE-1);
  79. }
  80. void push(node* data){
  81. if (isFull())
  82. return;
  83.  
  84. arr[++top] = data;
  85. }
  86. node* pop(){
  87. if(isEmpty())
  88. return NULL;
  89.  
  90. return arr[top--];
  91. }
  92. node* topD(){
  93. if(isEmpty())
  94. return NULL;
  95.  
  96. return arr[top];
  97. }
  98. };
  99.  
  100. struct queue{
  101. int front, rear;
  102. int msize;
  103. node* arr[MAXSTACKSIZE];
  104. queue(){
  105. rear = -1;
  106. front = 0;
  107. msize = 0;
  108. }
  109. int size(){
  110. return msize;
  111. }
  112. bool isEmpty(){
  113. if(rear == -1 && front == 0)
  114. return true;
  115. return false;
  116. }
  117. bool isFull(){
  118. if(rear == MAXSTACKSIZE-1)
  119. return true;
  120. return false;
  121. }
  122. void enQueue(node* data){
  123. if(isFull())
  124. return;
  125.  
  126. msize++;
  127. arr[++rear] = data;
  128. }
  129. node* deQueue(){
  130. if(isEmpty())
  131. return NULL;
  132.  
  133. node* temp;
  134. if(front == rear){
  135. temp = arr[front];
  136. rear = -1;
  137. front = 0;
  138. msize--;
  139. return temp;
  140. }
  141.  
  142. msize--;
  143. temp = arr[front++];
  144. return temp;
  145. }
  146. };
  147.  
  148. struct node* createNode(int val){
  149. return new node(val);
  150. }
  151.  
  152. void inOrderL(lnode* root){
  153. if(root == NULL)
  154. return;
  155.  
  156. inOrderL(root->previous);
  157. cout<<root->data<<" ";
  158. inOrderL(root->next);
  159. }
  160.  
  161. void printTreeUsingNext(node* root) {
  162. if (root == NULL)
  163. return;
  164.  
  165. while (root) {
  166. cout << " " << root->data;
  167. root = root->next;
  168. }
  169.  
  170. cout << endl;
  171. }
  172.  
  173. void printLMinAndRMax(node* rootm){
  174. if(rootm == NULL)
  175. return;
  176.  
  177. printLMinAndRMax(rootm->left);
  178. printLMinAndRMax(rootm->right);
  179. cout<<"LM:"<<rootm->leftMin<<" N:"<<rootm->data<<" RM:"<<rootm->rightMax<<endl;
  180. }
  181.  
  182. void inOrder(node* root){
  183. if(root == NULL)
  184. return;
  185.  
  186. inOrder(root->left);
  187. cout<<root->data<<" ";
  188. inOrder(root->right);
  189. }
  190.  
  191. void inOrderWithoutRecurr(node* root){
  192. if(root == NULL)
  193. return;
  194.  
  195. stack s1;
  196. node* current = root;
  197. if(current)
  198. s1.push(current);
  199.  
  200. while(!s1.isEmpty()){
  201. while(current){
  202. if(current->left)
  203. s1.push(current->left);
  204. current = current->left;
  205. }
  206.  
  207. if(current == NULL && !s1.isEmpty()){
  208. node* temp = s1.pop();
  209. if(temp){
  210. cout<<temp->data<<" ";
  211. current = temp->right;
  212. if(current)
  213. s1.push(current);
  214. }
  215. }
  216. }
  217. }
  218.  
  219. void preOrder(node* root){
  220. if(root == NULL)
  221. return;
  222.  
  223. cout<<root->data<<" ";
  224. preOrder(root->left);
  225. preOrder(root->right);
  226. }
  227.  
  228. void preOrderWithoutRecurr(node* root){
  229. if(root == NULL)
  230. return;
  231.  
  232. stack s;
  233. s.push(root);
  234. while(!s.isEmpty()){
  235. node* temp = s.pop();
  236. if(temp){
  237. cout<<temp->data<<" ";
  238. s.push(temp->right);
  239. s.push(temp->left);
  240. }
  241. }
  242. }
  243.  
  244. void postOrder(node* root){
  245. if(root == NULL)
  246. return;
  247.  
  248. postOrder(root->left);
  249. postOrder(root->right);
  250. cout<<root->data<<" ";
  251. }
  252.  
  253. void postOrderWithoutRecurr(node* root){
  254. if(root == NULL)
  255. return;
  256.  
  257. stack s1, s2;
  258. s1.push(root);
  259. while(!s1.isEmpty()){
  260. node* temp = s1.pop();
  261. s2.push(temp);
  262. if(temp){
  263. s1.push(temp->left);
  264. s1.push(temp->right);
  265. }
  266. }
  267.  
  268. while(!s2.isEmpty()){
  269. node* temp = s2.pop();
  270. if(temp)
  271. cout<<temp->data<<" ";
  272. }
  273. }
  274.  
  275. node* insert(node* root, int val){
  276. if(root == NULL)
  277. return createNode(val);
  278.  
  279. if(val < root->data) {
  280. root->left = insert(root->left, val);
  281. root->leftChilds++;
  282. }
  283.  
  284. if(val > root->data) {
  285. root->right = insert(root->right, val);
  286. root->rightChilds++;
  287. }
  288.  
  289. if(root->left)
  290. root->left->parent = root;
  291. if(root->right)
  292. root->right->parent = root;
  293.  
  294. return root;
  295. }
  296.  
  297.  
  298. node* insertByItr(node* root, int val){
  299. if(root == NULL)
  300. return createNode(val);
  301.  
  302. node* croot = root;
  303. while(root){
  304. if(val < root->data){
  305. if(root->left) {
  306. root = root->left;
  307. } else {
  308. root->left = createNode(val);
  309. break;
  310. }
  311. } else if( val > root->data){
  312. if(root->right){
  313. root = root->right;
  314. } else {
  315. root->right = createNode(val);
  316. break;
  317. }
  318. }
  319. }
  320.  
  321. return croot;
  322. }
  323.  
  324. node* getMinNode(node* root){
  325. if(root == NULL)
  326. return NULL;
  327.  
  328. while(root){
  329. if(root->left)
  330. root = root->left;
  331. else
  332. break;
  333. }
  334.  
  335. return root;
  336. }
  337.  
  338. node* getMaxNode(node* root){
  339. if(root == NULL)
  340. return NULL;
  341.  
  342. while(root){
  343. if(root->right)
  344. root = root->right;
  345. else
  346. break;
  347. }
  348.  
  349. return root;
  350. }
  351.  
  352. node* deleteNode(node* root, int val){
  353. if(root == NULL)
  354. return NULL;
  355.  
  356. if(val < root->data)
  357. root->left = deleteNode(root->left, val);
  358. else if(val > root->data)
  359. root->right = deleteNode(root->right, val);
  360. else {
  361. if(root->left == NULL && root->right == NULL){
  362. delete root;
  363. return NULL;
  364. } else if(root->left != NULL && root->right == NULL){
  365. node* temp = root->left;
  366. temp->parent = root->parent;
  367. delete root;
  368. return temp;
  369. } else if(root->left == NULL && root->right != NULL){
  370. node* temp = root->right;
  371. temp->parent = root->parent;
  372. delete root;
  373. return temp;
  374. } else {
  375. node* min = getMinNode(root->right);
  376. root->data = min->data;
  377. root->right = deleteNode(root->right, min->data);
  378. }
  379. }
  380. return root;
  381. }
  382.  
  383. void greaterSumTreeUtil(node* root, int& sum){
  384. if(root == NULL)
  385. return;
  386.  
  387. greaterSumTreeUtil(root->right, sum);
  388. int val = root->data;
  389. root->data = sum;
  390. sum += val;
  391. greaterSumTreeUtil(root->left, sum);
  392. }
  393.  
  394. void sumOfAllLeftLeavesBSTUtil(node* root, int& sum){
  395. if(root == NULL)
  396. return;
  397.  
  398. sumOfAllLeftLeavesBSTUtil(root->left, sum);
  399.  
  400. if(root->left && !root->right){
  401. sum += root->left->data;
  402. }
  403.  
  404. sumOfAllLeftLeavesBSTUtil(root->right, sum);
  405. }
  406.  
  407. int convertBinaryTreeToSumTreeUtil(node* root){
  408. if(root == NULL)
  409. return 0;
  410.  
  411. int mData = root->data;
  412. int lData = convertBinaryTreeToSumTreeUtil(root->left);
  413. int rData = convertBinaryTreeToSumTreeUtil(root->right);
  414. root->data = lData + rData;
  415. return mData+root->data;
  416. }
  417.  
  418. node* findInBSTUtilReccur(node* root, int val){
  419. if(root == NULL)
  420. return NULL;
  421.  
  422. if(root->data == val)
  423. return root;
  424.  
  425. if(val < root->data)
  426. return findInBSTUtilReccur(root->left, val);
  427.  
  428. return findInBSTUtilReccur(root->right, val);
  429. }
  430.  
  431. node* findInBSTUtilItr(node* root, int val){
  432. if(root == NULL)
  433. return NULL;
  434.  
  435. node* found = NULL;
  436.  
  437. while(root){
  438. if(root->data == val){
  439. found = root;
  440. break;
  441. }
  442.  
  443. if(val < root->data)
  444. root = root->left;
  445. else
  446. root = root->right;
  447. }
  448.  
  449. return found;
  450. }
  451.  
  452. void sizeOfTreeUtilByRecurr(node* root, int& size){
  453. if(root == NULL)
  454. return;
  455.  
  456. size++;
  457. sizeOfTreeUtilByRecurr(root->left, size);
  458. sizeOfTreeUtilByRecurr(root->right, size);
  459. }
  460.  
  461. void sizeOfTreeUtilWithoutRecurr(node* root, int& size){
  462. if(root == NULL)
  463. return;
  464.  
  465. queue q;
  466. q.enQueue(root);
  467. size++;
  468.  
  469. while(!q.isEmpty()){
  470. node* temp = q.deQueue();
  471. if(temp){
  472. if(temp->left){
  473. q.enQueue(temp->left);
  474. size++;
  475. }
  476. if(temp->right){
  477. q.enQueue(temp->right);
  478. size++;
  479. }
  480. }
  481. }
  482. }
  483.  
  484. void BFSOfTreeUtil(node* root){
  485. if(root == NULL)
  486. return;
  487.  
  488. queue q;
  489. q.enQueue(root);
  490. while(!q.isEmpty()){
  491. int size = q.size();
  492. while(size--){
  493. node* temp = q.deQueue();
  494. cout<<temp->data<<" ";
  495. if(temp->left)
  496. q.enQueue(temp->left);
  497. if(temp->right)
  498. q.enQueue(temp->right);
  499. }
  500. cout<<endl;
  501. }
  502. }
  503.  
  504. int getHeightOfTree(node* root){
  505. if(root == NULL)
  506. return 0;
  507.  
  508. return MAX(getHeightOfTree(root->left), getHeightOfTree(root->right)) + 1;
  509. }
  510.  
  511. int DiameterOfTreeUtil(node* root){
  512. if(root == NULL)
  513. return 0;
  514.  
  515. return MAX(getHeightOfTree(root->left)+getHeightOfTree(root->right)+1, MAX(DiameterOfTreeUtil(root->left), DiameterOfTreeUtil(root->right)));
  516. }
  517.  
  518. struct DiameterAndHeight{
  519. int diameter;
  520. int height;
  521. DiameterAndHeight(){
  522. diameter = 0;
  523. height = 0;
  524. }
  525. };
  526.  
  527. DiameterAndHeight DiameterOfTreeUtilOptimize(node* root){
  528. if(root == NULL)
  529. return DiameterAndHeight();
  530.  
  531. DiameterAndHeight middle;
  532. DiameterAndHeight left = DiameterOfTreeUtilOptimize(root->left);
  533. DiameterAndHeight right = DiameterOfTreeUtilOptimize(root->right);
  534.  
  535. middle.height = MAX(left.height, right.height) + 1;
  536. middle.diameter = MAX(left.height+right.height+1, MAX(left.diameter, right.diameter));
  537. return middle;
  538. }
  539.  
  540. int maxElementOfTreeUtil(node* root, int& max){
  541. if(root == NULL)
  542. return 0;
  543.  
  544. max = root->data > max ? root->data : max;
  545. maxElementOfTreeUtil(root->left, max);
  546. maxElementOfTreeUtil(root->right, max);
  547. }
  548.  
  549. int maxElementOfTreeUtilByRecurr(node* root){
  550. if(root == NULL)
  551. return MINV;
  552.  
  553. return MAX(root->data, MAX(maxElementOfTreeUtilByRecurr(root->left), maxElementOfTreeUtilByRecurr(root->right)));
  554. }
  555.  
  556. bool mirrorTreeUtil(node* root, node* rootm){
  557. if(root == NULL && rootm == NULL)
  558. return true;
  559.  
  560. if(root == NULL || rootm == NULL)
  561. return false;
  562.  
  563. return (root->data == rootm->data) && mirrorTreeUtil(root->left, rootm->right) && mirrorTreeUtil(root->right, rootm->left);
  564. }
  565.  
  566. void createMirrorTreeUtil(node* root){
  567. if(root == NULL)
  568. return;
  569.  
  570. node* temp = root->left;
  571. root->left = root->right;
  572. root->right = temp;
  573.  
  574. createMirrorTreeUtil(root->left);
  575. createMirrorTreeUtil(root->right);
  576. }
  577.  
  578. bool isTreesSame(node* roota, node* rootb){
  579. if(roota == NULL && rootb == NULL)
  580. return true;
  581.  
  582. if(roota == NULL || rootb == NULL)
  583. return false;
  584.  
  585. return (roota->data == rootb->data) && isTreesSame(roota->left, rootb->left) && isTreesSame(roota->right, rootb->right);
  586. }
  587.  
  588. void printAllPathsFromRootToLeafUtil(node* root, int& sum, int path[], int& pathIndex){
  589. if(root == NULL)
  590. return;
  591.  
  592. path[pathIndex++] = root->data;
  593. sum = sum - root->data;
  594.  
  595. if(sum == 0){
  596. for(int i = 0; i < pathIndex; i++)
  597. cout<<path[i]<<" ";
  598. cout<<endl;
  599. pathIndex--;
  600. sum = sum + root->data;
  601. return;
  602. }
  603.  
  604. printAllPathsFromRootToLeafUtil(root->left, sum, path, pathIndex);
  605. printAllPathsFromRootToLeafUtil(root->right, sum, path, pathIndex);
  606.  
  607. pathIndex--;
  608. sum = sum + root->data;
  609. }
  610.  
  611. void reverseLevelOrderTraversalUtil(queue q){
  612. if(q.isEmpty())
  613. return;
  614.  
  615. queue qel;
  616. while(!q.isEmpty()){
  617. node*temp = q.deQueue();
  618. if(temp){
  619. if(temp->left)
  620. qel.enQueue(temp->left);
  621. if(temp->right)
  622. qel.enQueue(temp->right);
  623. }
  624. }
  625.  
  626. reverseLevelOrderTraversalUtil(qel);
  627.  
  628. while(!qel.isEmpty()){
  629. node*temp = qel.deQueue();
  630. if(temp)
  631. cout<<temp->data<<" ";
  632. }
  633.  
  634. cout<<endl;
  635. }
  636.  
  637. int findHeightWithoutRecurrsionUtil(node* root){
  638. if(root == NULL)
  639. return 0;
  640.  
  641. queue q;
  642. if(root)
  643. q.enQueue(root);
  644. int height = 0;
  645. while(!q.isEmpty()){
  646. int size = q.size();
  647. height++;
  648. while(size--){
  649.  
  650. node* temp = q.deQueue();
  651. if(temp){
  652. if(temp->left)
  653. q.enQueue(temp->left);
  654. if(temp->right)
  655. q.enQueue(temp->right);
  656. }
  657. }
  658. }
  659.  
  660. return height;
  661. }
  662.  
  663. void printAllFullNodesInBTreeUtil(node* root){
  664. if(root == NULL)
  665. return;
  666.  
  667. if(root->left && root->right)
  668. cout<<root->data<<" ";
  669. printAllFullNodesInBTreeUtil(root->left);
  670. printAllFullNodesInBTreeUtil(root->right);
  671. }
  672.  
  673. void printBottomViewOfBSTUtil(node* root, int hd, map<int, node*>& bView){
  674. if(root == NULL)
  675. return;
  676.  
  677. bView[hd] = root;
  678. printBottomViewOfBSTUtil(root->left, hd-1, bView);
  679. printBottomViewOfBSTUtil(root->right, hd+1, bView);
  680. }
  681.  
  682. node* constructBSTFromPreOrderUtil(int arr[], int& preOrderIndex, int& preOrderVal, int min, int max, int size){
  683. if(preOrderIndex >= size)
  684. return NULL;
  685.  
  686. node* root = NULL;
  687.  
  688. if(preOrderVal > min && preOrderVal < max){
  689. root = createNode(preOrderVal);
  690. preOrderIndex++;
  691. root->left = constructBSTFromPreOrderUtil(arr, preOrderIndex, arr[preOrderIndex], min, preOrderVal, size);
  692. root->right = constructBSTFromPreOrderUtil(arr, preOrderIndex, arr[preOrderIndex], preOrderVal, max, size);
  693. }
  694.  
  695. return root;
  696. }
  697.  
  698. void printTopViewOfBSTUtil(node* root, int hd, map<int, node*>& tView){
  699. if(root == NULL)
  700. return;
  701.  
  702. if(tView.find(hd) == tView.end()){
  703. tView[hd] = root;
  704. }
  705.  
  706. printTopViewOfBSTUtil(root->left, hd - 1, tView);
  707. printTopViewOfBSTUtil(root->right, hd + 1, tView);
  708. }
  709.  
  710. node* inOrderSuccessor(node* root){
  711. if(root == NULL)
  712. return NULL;
  713.  
  714. if(root->right)
  715. return getMinNode(root->right);
  716.  
  717. node* par = root->parent;
  718. while(par && (par->right == root)){
  719. root = par;
  720. par = par->parent;
  721. }
  722.  
  723. return par;
  724. }
  725.  
  726. node* inOrderPredecessor(node* root){
  727. if(root == NULL)
  728. return NULL;
  729.  
  730. if (root->left)
  731. return getMaxNode(root->left);
  732.  
  733. node* par = root->parent;
  734. while(par && (par->left == root)){
  735. root = par;
  736. par = par->parent;
  737. }
  738.  
  739. return par;
  740. }
  741.  
  742. int printHeightOfNodeInTreeUtil(node* root, node* find, int height){
  743. if(root == NULL && find == NULL)
  744. return 0;
  745.  
  746. if(root == NULL || find == NULL)
  747. return 0;
  748.  
  749. if(root->data == find->data){
  750. return height;
  751. }
  752.  
  753. if(find->data < root->data)
  754. return printHeightOfNodeInTreeUtil(root->left, find, height+1);
  755.  
  756. return printHeightOfNodeInTreeUtil(root->right, find, height+1);
  757. }
  758.  
  759. bool printIfTreeBalancedOrNotUtil(node* root){
  760. if(root == NULL)
  761. return true;
  762.  
  763. int lHeight = getHeightOfTree(root->left);
  764. int rHeight = getHeightOfTree(root->right);
  765.  
  766. if(((lHeight > rHeight) ? (lHeight-rHeight) : (rHeight-lHeight)) >= 2)
  767. return false;
  768.  
  769. return printIfTreeBalancedOrNotUtil(root->left) && printIfTreeBalancedOrNotUtil(root->right);
  770. }
  771.  
  772. int printIfTreeBalancedOrNotUtilOptimized(node* root){
  773. if(root == NULL)
  774. return 0;
  775.  
  776. int lHeight = printIfTreeBalancedOrNotUtilOptimized(root->left);
  777. int rHeight = printIfTreeBalancedOrNotUtilOptimized(root->right);
  778.  
  779. int aHeight = (lHeight>rHeight) ? (lHeight-rHeight) : (rHeight-lHeight);
  780. root->isBalanced = (aHeight >= 2) ? false : true;
  781. return MAX(lHeight, rHeight) + 1;
  782. }
  783.  
  784. node* sortedArrayToBSTUtil(int arr[], int left, int right){
  785. if(left > right)
  786. return NULL;
  787.  
  788. int mid = (left+right)/2;
  789. node* root = createNode(arr[mid]);
  790. root->left = sortedArrayToBSTUtil(arr, left, mid-1);
  791. root->right = sortedArrayToBSTUtil(arr, mid+1, right);
  792. return root;
  793. }
  794.  
  795. void pathFromRootToNodeUtil(node* root, node* find, int path[], int& pathIndex, bool& found){
  796. if(root == NULL && find == NULL)
  797. return;
  798.  
  799. if(root == NULL || find == NULL)
  800. return;
  801.  
  802. if(found)
  803. return;
  804.  
  805. path[pathIndex++] = root->data;
  806.  
  807. if(find->data == root->data){
  808. for(int i = 0; i < pathIndex; i++){
  809. cout<<path[i]<<" ";
  810. }
  811. found = true;
  812. return;
  813. }
  814.  
  815. if(find->data < root->data)
  816. pathFromRootToNodeUtil(root->left, find, path, pathIndex, found);
  817.  
  818. pathFromRootToNodeUtil(root->right, find, path, pathIndex, found);
  819. }
  820.  
  821. int sizeOfTree2Util(node* root){
  822. if(root == NULL)
  823. return 0;
  824.  
  825. return 1 + sizeOfTree2Util(root->left) + sizeOfTree2Util(root->right);
  826. }
  827.  
  828. bool isBSTUtil(node* root){
  829. if(root == NULL)
  830. return true;
  831.  
  832. if(root->left && root->data < root->left->data)
  833. return false;
  834.  
  835. if(root->right && root->data > root->right->data )
  836. return false;
  837.  
  838. return isBSTUtil(root->left) && isBSTUtil(root->right);
  839. }
  840. bool isBSTUtilByMinMax(node* root, int MIN, int MAX){
  841. if(root == NULL)
  842. return true;
  843.  
  844. if(root->data < MIN)
  845. return false;
  846.  
  847. if(root->data > MAX)
  848. return false;
  849.  
  850. return isBSTUtilByMinMax(root->left, MIN, root->data) && isBSTUtilByMinMax(root->right, root->data, MAX);
  851. }
  852.  
  853. int findMaxWidthOfTree(node* rootm){
  854. if(rootm == NULL)
  855. return MINV;
  856.  
  857. queue q;
  858. int maxWidth = MINV;
  859. q.enQueue(rootm);
  860.  
  861. while(!q.isEmpty()){
  862. int size = q.size();
  863. maxWidth = size > maxWidth ? size : maxWidth;
  864. while(size--){
  865. node*temp = q.deQueue();
  866. if(temp->left)
  867. q.enQueue(temp->left);
  868. if(temp->right)
  869. q.enQueue(temp->right);
  870. }
  871. }
  872.  
  873. return maxWidth;
  874. }
  875.  
  876. void printNodesBetweenTwoLevelsUtil(node* root, int l1, int l2){
  877. if(root == NULL)
  878. return;
  879.  
  880. queue q;
  881. q.enQueue(root);
  882. int level = 0;
  883. while(!q.isEmpty()){
  884. int size = q.size();
  885. level++;
  886. while(size--){
  887. node* temp = q.deQueue();
  888. if(level >= l1 && level <= l2)
  889. cout<<temp->data<<" ";
  890. if(temp->left)
  891. q.enQueue(temp->left);
  892. if(temp->right)
  893. q.enQueue(temp->right);
  894. }
  895. cout<<endl;
  896. }
  897. }
  898.  
  899. bool isBSTEqualUtil(node* root1, node* root2){
  900. if(root1 == NULL && root2 == NULL)
  901. return true;
  902.  
  903. if(root1 != NULL && root2 == NULL)
  904. return false;
  905.  
  906. if(root1 == NULL && root2 != NULL)
  907. return false;
  908.  
  909. if(root1->data != root2->data)
  910. return false;
  911.  
  912. return isBSTEqualUtil(root1->left, root2->left) && isBSTEqualUtil(root1->right, root2->right);
  913. }
  914.  
  915. void printingRightViewUtil(node* root, int& currLevel, int level){
  916. if(root == NULL)
  917. return;
  918.  
  919. if(level > currLevel){
  920. cout<<root->data<<" ";
  921. currLevel = level;
  922. }
  923.  
  924. printingRightViewUtil(root->right, currLevel, level+1);
  925. printingRightViewUtil(root->left, currLevel, level+1);
  926. }
  927.  
  928. void printingLeftViewUtil(node* root, int& currLevel, int level){
  929. if(root == NULL)
  930. return;
  931.  
  932. if(level > currLevel){
  933. cout<<root->data<<" ";
  934. currLevel = level;
  935. }
  936.  
  937. printingLeftViewUtil(root->left, currLevel, level+1);
  938. printingLeftViewUtil(root->right, currLevel, level+1);
  939. }
  940.  
  941. node* LCABSTUtil(node* root, node* n1, node* n2){
  942. if(root == NULL)
  943. return NULL;
  944.  
  945. if(n1 == NULL || n2 == NULL)
  946. return NULL;
  947.  
  948. if(n1->data < root->data && n2->data > root->data)
  949. return root;
  950.  
  951. if(n1->data < root->data && n2->data < root->data)
  952. return LCABSTUtil(root->left, n1, n2);
  953.  
  954. return LCABSTUtil(root->right, n1, n2);
  955. }
  956.  
  957. node* LCABTreeUtil(node* root, node* n1, node* n2){
  958. if(root == NULL)
  959. return NULL;
  960.  
  961. if(n1 == NULL || n2 == NULL)
  962. return NULL;
  963.  
  964. if(root == n1 || root == n2)
  965. return root;
  966.  
  967. node* left = LCABTreeUtil(root->left, n1, n2);
  968. node* right = LCABTreeUtil(root->right, n1, n2);
  969.  
  970. if(left && right)
  971. return root;
  972.  
  973. return (left != NULL) ? left : right;
  974. }
  975.  
  976. void verticalSumOfBST(node* root, int hd, map<int,int>& vSum){
  977. if(root == NULL)
  978. return;
  979.  
  980. map<int, int>::iterator itr = vSum.find(hd);
  981. if(itr == vSum.end()){
  982. vSum[hd] = root->data;
  983. } else {
  984. vSum[hd] = itr->second + root->data;
  985. }
  986.  
  987. verticalSumOfBST(root->left, hd - 1, vSum);
  988. verticalSumOfBST(root->right, hd + 1, vSum);
  989. }
  990.  
  991. void printTreeInVerticalOrderPathUtil(node* root, int hd, map<int, vector<node*> >& vOrder){
  992. if(root == NULL)
  993. return;
  994.  
  995. vOrder[hd].push_back(root);
  996. printTreeInVerticalOrderPathUtil(root->left, hd - 1, vOrder);
  997. printTreeInVerticalOrderPathUtil(root->right, hd + 1, vOrder);
  998. }
  999.  
  1000. void printNodesThatDontHaveSibilingsUtils(node* root){
  1001. if(root == NULL)
  1002. return;
  1003.  
  1004. if(root->left == NULL && root->right != NULL){
  1005. cout<<root->right->data<<" ";
  1006. }
  1007.  
  1008. if(root->left != NULL && root->right == NULL){
  1009. cout<<root->left->data<<" ";
  1010. }
  1011.  
  1012. printNodesThatDontHaveSibilingsUtils(root->left);
  1013. printNodesThatDontHaveSibilingsUtils(root->right);
  1014. }
  1015.  
  1016. int distanceOfNodeFromRootUtil(node* root, node* find, int dist){
  1017. if(root == NULL && find == NULL)
  1018. return 0;
  1019.  
  1020. if(root == NULL || find == NULL)
  1021. return 0;
  1022.  
  1023. if(root->data == find->data){
  1024. return dist;
  1025. }
  1026.  
  1027. if(find->data < root->data)
  1028. return distanceOfNodeFromRootUtil(root->left, find, dist+1);
  1029.  
  1030. return distanceOfNodeFromRootUtil(root->right, find, dist+1);
  1031. }
  1032.  
  1033. void printAllNodesAtDistanceDFromRootUtil(node* root, int dist, int dDist){
  1034. if(root == NULL)
  1035. return;
  1036.  
  1037. if(dist == dDist){
  1038. cout<<root->data<<" ";
  1039. }
  1040.  
  1041. printAllNodesAtDistanceDFromRootUtil(root->left, dist+1, dDist);
  1042. printAllNodesAtDistanceDFromRootUtil(root->right, dist+1, dDist);
  1043. }
  1044.  
  1045. int distanceBetweenTwoNodesUtil(node* root, node* n1, node* n2){
  1046.  
  1047. if(root == NULL)
  1048. return 0;
  1049.  
  1050. if(n1 == NULL && n2 == NULL)
  1051. return 0;
  1052.  
  1053. if(n1 == NULL && n2 != NULL)
  1054. return distanceOfNodeFromRootUtil(root, n2, 0);
  1055.  
  1056. if(n1 != NULL && n2 == NULL)
  1057. return distanceOfNodeFromRootUtil(root, n1, 0);
  1058.  
  1059. int nd1 = distanceOfNodeFromRootUtil(root, n1, 0);
  1060. int nd2 = distanceOfNodeFromRootUtil(root, n2, 0);
  1061.  
  1062. node* lca = LCABSTUtil(root, n1, n2);
  1063. int nd3 = distanceOfNodeFromRootUtil(root, lca, 0);
  1064.  
  1065. return ((nd1 + nd2) - (2 * (nd3)));
  1066. }
  1067.  
  1068. void maxSumFromRootToAllLeavesUtil(node* rootm, int sum, int& mSum){
  1069. if(rootm == NULL)
  1070. return;
  1071.  
  1072. sum += rootm->data;
  1073. if(rootm->left == NULL && rootm->right == NULL){
  1074. mSum = sum > mSum ? sum : mSum;
  1075. sum -= rootm->data;
  1076. return;
  1077. }
  1078.  
  1079. maxSumFromRootToAllLeavesUtil(rootm->left, sum, mSum);
  1080. maxSumFromRootToAllLeavesUtil(rootm->right, sum, mSum);
  1081. sum -= rootm->data;
  1082. }
  1083.  
  1084. void printAllNodesAtDistanceDFromLeafUtil(node* rootm, int dist, int disk, int path[], int& pathIndex, bool pathp[])
  1085. {
  1086. if(rootm == NULL)
  1087. return;
  1088.  
  1089. path[pathIndex] = rootm->data;
  1090. pathp[pathIndex] = false;
  1091. pathIndex++;
  1092.  
  1093. if(rootm->left == NULL && rootm->right == NULL){
  1094. int distance = dist - disk - 1;
  1095. if(distance >= 0 && pathp[distance] == false){
  1096. cout<<path[distance]<<" ";
  1097. pathp[distance] = true;
  1098. }
  1099. pathIndex--;
  1100. return;
  1101. }
  1102.  
  1103. printAllNodesAtDistanceDFromLeafUtil(rootm->left, dist+1, disk, path, pathIndex, pathp);
  1104. printAllNodesAtDistanceDFromLeafUtil(rootm->right, dist+1, disk, path, pathIndex, pathp);
  1105. pathIndex--;
  1106. }
  1107.  
  1108. void printBTreeInZigZagUtil(node* root){
  1109. if(root == NULL)
  1110. return;
  1111.  
  1112. queue q;
  1113. stack s;
  1114. bool zigzag = false;
  1115. q.enQueue(root);
  1116.  
  1117. while(!q.isEmpty()){
  1118. int size = q.size();
  1119. while(size--){
  1120. node* temp = q.deQueue();
  1121. if(!zigzag)
  1122. cout<<temp->data<<" ";
  1123. else
  1124. s.push(temp);
  1125. if(temp){
  1126. if(temp->left)
  1127. q.enQueue(temp->left);
  1128. if(temp->right)
  1129. q.enQueue(temp->right);
  1130. }
  1131. }
  1132.  
  1133. while(!s.isEmpty()){
  1134. node* temp = s.pop();
  1135. if(temp)
  1136. cout<<temp->data<<" ";
  1137. }
  1138. zigzag = !zigzag;
  1139. cout<<endl;
  1140. }
  1141. }
  1142.  
  1143. string itostr(int s){
  1144. stringstream ss;
  1145. ss<<s;
  1146. return ss.str();
  1147. }
  1148.  
  1149. void subTreePreOrder(node* root, string& str){
  1150. if(root == NULL) {
  1151. str.append("X");
  1152. return;
  1153. }
  1154.  
  1155. str.append(itostr(root->data));
  1156. subTreePreOrder(root->left, str);
  1157. subTreePreOrder(root->right, str);
  1158. }
  1159.  
  1160. bool printTreeIsSubTreeOfATreeUtil(node* rootm, node* roots){
  1161. if(rootm == NULL && roots == NULL)
  1162. return true;
  1163.  
  1164. if(rootm == NULL || roots == NULL)
  1165. return false;
  1166.  
  1167. string mStr, sStr;
  1168. subTreePreOrder(rootm, mStr);
  1169. subTreePreOrder(roots, sStr);
  1170.  
  1171. return mStr.find(sStr) != -1;
  1172. }
  1173.  
  1174. node* BSTFromInOrderPostOrderUtil(int post[], int& postIndex, int min, int max){
  1175. if(postIndex < 0)
  1176. return NULL;
  1177.  
  1178. node* root = NULL;
  1179.  
  1180. if(post[postIndex] > min && post[postIndex] < max){
  1181. root = createNode(post[postIndex--]);
  1182. root->right = BSTFromInOrderPostOrderUtil(post, postIndex, root->data, max);
  1183. root->left = BSTFromInOrderPostOrderUtil(post, postIndex, min, root->data);
  1184. }
  1185.  
  1186. return root;
  1187. }
  1188.  
  1189. node* bTreeFromInOrderPostOrderUtil(int in[], int post[], int& postIndex, int left, int right){
  1190. if(left > right)
  1191. return NULL;
  1192.  
  1193. node* root = createNode(post[postIndex--]);
  1194.  
  1195. if(left == right)
  1196. return root;
  1197.  
  1198. int index;
  1199. for(index = left; index <= right; index++){
  1200. if(root->data == in[index])
  1201. break;
  1202. }
  1203.  
  1204. root->right = bTreeFromInOrderPostOrderUtil(in, post, postIndex, index+1, right);
  1205. root->left = bTreeFromInOrderPostOrderUtil(in, post, postIndex, left, index-1);
  1206. return root;
  1207. }
  1208.  
  1209. node* BSTFromInOrderPreOrderUtil(int pre[], int& preIndex, int min, int max, int size){
  1210. if(preIndex > size)
  1211. return NULL;
  1212.  
  1213. node* root = NULL;
  1214.  
  1215. if(pre[preIndex]>min && pre[preIndex]<max){
  1216. root = createNode(pre[preIndex++]);
  1217. root->left = BSTFromInOrderPreOrderUtil(pre, preIndex, min, root->data, size);
  1218. root->right = BSTFromInOrderPreOrderUtil(pre, preIndex, root->data, max, size);
  1219. }
  1220.  
  1221. return root;
  1222. }
  1223.  
  1224. node* bTreeFromInOrderPreOrderUtil(int in[], int pre[], int& preIndex, int left, int right){
  1225. if(left > right)
  1226. return NULL;
  1227.  
  1228. node* root = createNode(pre[preIndex++]);
  1229. if(left == right)
  1230. return root;
  1231.  
  1232. int index;
  1233. for(index = left; index <= right; index++){
  1234. if(root->data == in[index])
  1235. break;
  1236. }
  1237.  
  1238. root->left = bTreeFromInOrderPreOrderUtil(in, pre, preIndex, left, index-1);
  1239. root->right = bTreeFromInOrderPreOrderUtil(in, pre, preIndex, index+1, right);
  1240. return root;
  1241. }
  1242.  
  1243. bool isSibilingsUtil(node* root, node* n1, node* n2){
  1244. if(root == NULL)
  1245. return false;
  1246.  
  1247. if(n1 == NULL && n2 == NULL)
  1248. return true;
  1249.  
  1250. if(n1 == NULL || n2 == NULL)
  1251. return false;
  1252.  
  1253. if(root->left && root->right){
  1254. if(root->left == n1 && root->right == n2)
  1255. return true;
  1256. if(root->left == n2 && root->right == n1)
  1257. return true;
  1258. }
  1259.  
  1260. return isSibilingsUtil(root->left, n1, n2) || isSibilingsUtil(root->right, n1, n2);
  1261. }
  1262.  
  1263. bool distanceOfNodeFromRootInBTreeUtil(node* root, node* find, int dist, int& fDist){
  1264. if(root == NULL)
  1265. return false;
  1266.  
  1267. if(find == NULL)
  1268. return false;
  1269.  
  1270. if(root->data == find->data){
  1271. fDist = dist;
  1272. return true;
  1273. }
  1274.  
  1275. if(distanceOfNodeFromRootInBTreeUtil(root->left, find, dist+1, fDist))
  1276. return true;
  1277.  
  1278. if(distanceOfNodeFromRootInBTreeUtil(root->right, find, dist+1, fDist))
  1279. return true;
  1280.  
  1281. return false;
  1282. }
  1283.  
  1284. bool isCousinsUtil(node* root, node* c1, node* c2){
  1285. if(root == NULL)
  1286. return false;
  1287.  
  1288. if((c1 == NULL && c2 == NULL)||(c1 == NULL || c2 == NULL))
  1289. return false;
  1290.  
  1291. int c1level, c2level;
  1292. if(!distanceOfNodeFromRootInBTreeUtil(root, c1, 0, c1level))
  1293. return false;
  1294.  
  1295. if(!distanceOfNodeFromRootInBTreeUtil(root, c2, 0, c2level))
  1296. return false;
  1297.  
  1298. if(c1level != c2level)
  1299. return false;
  1300.  
  1301. return !isSibilingsUtil(root, c1, c2);
  1302. }
  1303.  
  1304. void calcNextSiblings(node* root){
  1305. if(root == NULL)
  1306. return;
  1307.  
  1308. queue q;
  1309. stack s;
  1310. q.enQueue(root);
  1311. while(!q.isEmpty()){
  1312. int size = q.size();
  1313. while(size--){
  1314. node* temp = q.deQueue();
  1315. if(temp){
  1316. s.push(temp);
  1317. if(temp->left)
  1318. q.enQueue(temp->left);
  1319. if(temp->right)
  1320. q.enQueue(temp->right);
  1321. }
  1322. }
  1323.  
  1324. node* last = NULL;
  1325. while(!s.isEmpty()){
  1326. node* temp = s.pop();
  1327. temp->sibiling = last;
  1328. last = temp;
  1329. }
  1330. }
  1331. }
  1332.  
  1333. void printSibilingsOfNodeUtil(node* root){
  1334. if(root == NULL)
  1335. return;
  1336.  
  1337. while(root){
  1338. cout<<root->data<<" ";
  1339. root = root->sibiling;
  1340. }
  1341. }
  1342.  
  1343. node* inOrderWithoutParentUtil(node* root, node* find){
  1344. if(root == NULL || find == NULL)
  1345. return NULL;
  1346.  
  1347. if(find->right)
  1348. return getMinNode(find->right);
  1349.  
  1350. if(find == getMaxNode(root))
  1351. return NULL;
  1352.  
  1353. node* successor = NULL;
  1354. while(root){
  1355. if(find->data < root->data){
  1356. successor = root;
  1357. root = root->left;
  1358. }else if(find->data > root->data){
  1359. root = root->right;
  1360. }else{
  1361. break;
  1362. }
  1363. }
  1364.  
  1365. return root ? successor : NULL;
  1366. }
  1367.  
  1368. node* linkedListToBSTUtil(lnode** head, int length){
  1369. if(length <= 0)
  1370. return NULL;
  1371.  
  1372. node* left = linkedListToBSTUtil(head, length/2);
  1373. node* root = createNode((*head)->data);
  1374. (*head) = (*head)->next;
  1375. root->left = left;
  1376. root->right = linkedListToBSTUtil(head, length - length/2 - 1);
  1377. return root;
  1378. }
  1379.  
  1380. node* doubleLinkedListToBSTUtil(lnode** head, int length){
  1381. if(length <= 0)
  1382. return NULL;
  1383.  
  1384. node* left = doubleLinkedListToBSTUtil(head, length/2);
  1385. node* root = createNode((*head)->data);(*head)=(*head)->next;
  1386. root->left = left;
  1387. root->right = doubleLinkedListToBSTUtil(head, length - length/2 - 1);
  1388. return root;
  1389. }
  1390.  
  1391. lnode* doubleLinkedListToBSTInplaceUtil(lnode** head, int length){
  1392. if(length <= 0)
  1393. return NULL;
  1394.  
  1395. lnode* left = doubleLinkedListToBSTInplaceUtil(head, length/2);
  1396. lnode* root = *head; (*head) = (*head)->next;
  1397. root->previous = left;
  1398. root->next = doubleLinkedListToBSTInplaceUtil(head, length - length/2 -1);
  1399. return root;
  1400. }
  1401.  
  1402. node* ceilInBSTUtil(node* root, int ceil){
  1403. if(root == NULL)
  1404. return NULL;
  1405.  
  1406. if(root->data == ceil)
  1407. return root;
  1408.  
  1409. if(ceil > root->data)
  1410. return ceilInBSTUtil(root->right, ceil);
  1411.  
  1412. node* lCeil = ceilInBSTUtil(root->left, ceil);
  1413. return (lCeil && (lCeil->data >= ceil)) ? lCeil : root;
  1414. }
  1415.  
  1416. node* floorInBSTUtil(node* root, int floor){
  1417. if(root == NULL)
  1418. return NULL;
  1419.  
  1420. if(root->data == floor)
  1421. return root;
  1422.  
  1423. if(floor < root->data)
  1424. return floorInBSTUtil(root->left, floor);
  1425.  
  1426. node* lFloor = floorInBSTUtil(root->right, floor);
  1427. return (lFloor && (lFloor->data <= floor)) ? lFloor : root;
  1428. }
  1429.  
  1430. node* btreeToDoubleLinkedListUtil(node* root) {
  1431. if (root == NULL)
  1432. return NULL;
  1433.  
  1434. if (root->left) {
  1435. node* left = btreeToDoubleLinkedListUtil(root->left);
  1436.  
  1437. for (; left->right; left = left->right);
  1438.  
  1439. left->next = root;
  1440.  
  1441. root->previous = left;
  1442. }
  1443.  
  1444. if (root->right) {
  1445. node* right = btreeToDoubleLinkedListUtil(root->right);
  1446.  
  1447. for (; right->left; right = right->left);
  1448.  
  1449. right->previous = root;
  1450.  
  1451. root->next = right;
  1452. }
  1453.  
  1454. return root;
  1455. }
  1456.  
  1457. node* kthLargestElementUtil(node* root, int k) {
  1458. if (root == NULL)
  1459. return NULL;
  1460.  
  1461. while (root) {
  1462. if (k == (root->rightChilds + 1))
  1463. return root;
  1464. else if (k > (root->rightChilds + 1)) {
  1465. k = k - (root->rightChilds + 1);
  1466. root = root->left;
  1467. }
  1468. else
  1469. root = root->right;
  1470. }
  1471.  
  1472. return root;
  1473. }
  1474.  
  1475. node* kthSmallestElementUtil(node* root, int k) {
  1476. if (root == NULL)
  1477. return NULL;
  1478.  
  1479. while (root) {
  1480. if (k == root->leftChilds + 1)
  1481. return root;
  1482. else if (k > (root->leftChilds + 1)) {
  1483. k = k - (root->leftChilds + 1);
  1484. root = root->right;
  1485. }
  1486. else {
  1487. root = root->left;
  1488. }
  1489. }
  1490.  
  1491. return root;
  1492. }
  1493.  
  1494. void recorrectSwappedNodesUtil(node* rootm, node** first, node** middle, node** last, node** previous) {
  1495. if (rootm == NULL)
  1496. return;
  1497.  
  1498. recorrectSwappedNodesUtil(rootm->left, first, middle, last, previous);
  1499.  
  1500. if ((*previous) && rootm->data < (*previous)->data) {
  1501. if ((*first) == NULL) {
  1502. (*first) = (*previous);
  1503. (*middle) = rootm;
  1504. }
  1505. else {
  1506. (*last) = rootm;
  1507. }
  1508. }
  1509.  
  1510. *previous = rootm;
  1511. recorrectSwappedNodesUtil(rootm->right, first, middle, last, previous);
  1512. }
  1513.  
  1514. void secondLargestNodeUtil(node* root, int& count, node** sNode){
  1515. if(root == NULL)
  1516. return;
  1517.  
  1518. secondLargestNodeUtil(root->right, count, sNode);
  1519. count++;
  1520. if(count == 2)
  1521. *sNode = root;
  1522. secondLargestNodeUtil(root->left, count, sNode);
  1523. }
  1524.  
  1525. void deepestNodeInBTreeUtil(node* rootm, int dist, int& dLevel, node** dNode){
  1526. if(rootm == NULL)
  1527. return;
  1528.  
  1529. deepestNodeInBTreeUtil(rootm->left, dist+1, dLevel, dNode);
  1530.  
  1531. if(dLevel < dist){
  1532. *dNode = rootm;
  1533. dLevel = dist;
  1534. }
  1535.  
  1536. deepestNodeInBTreeUtil(rootm->right, dist+1, dLevel, dNode);
  1537. }
  1538.  
  1539. void deepestLeftNodeInBTreeUtil(node* rootm, int dist, int& dLevel, node** dNode){
  1540. if(rootm == NULL)
  1541. return;
  1542.  
  1543. deepestLeftNodeInBTreeUtil(rootm->left, dist+1, dLevel, dNode);
  1544.  
  1545. if(dLevel < dist){
  1546. if((*dNode) == NULL && rootm->parent && rootm->parent->left == rootm)
  1547. (*dNode) = rootm;
  1548. dLevel = dist;
  1549. }
  1550.  
  1551. deepestLeftNodeInBTreeUtil(rootm->right, dist+1, dLevel, dNode);
  1552. }
  1553.  
  1554. void createLinkedListAtEachLevelUtil(node* root){
  1555. if(root == NULL)
  1556. return;
  1557.  
  1558. queue q;
  1559. q.enQueue(root);
  1560. while(!q.isEmpty()){
  1561. int size = q.size();
  1562. lnode* headC = NULL;
  1563. lnode* headP = NULL;
  1564. while(size--){
  1565. node* temp = q.deQueue();
  1566. if(temp->left)
  1567. q.enQueue(temp->left);
  1568. if(temp->right)
  1569. q.enQueue(temp->right);
  1570. lnode* llnode = createLNode(temp->data);
  1571. if(headP)
  1572. headP->next = llnode;
  1573. if(headC == NULL)
  1574. headC = llnode;
  1575. headP = llnode;
  1576. }
  1577.  
  1578. while(headC){
  1579. lnode* temp = headC;
  1580. cout<<headC->data<<" ";
  1581. headC = headC->next;
  1582. delete temp;
  1583. }
  1584.  
  1585. delete headC;
  1586. cout<<endl;
  1587. }
  1588. }
  1589.  
  1590. void reverseAlternateLevelsOfBtreeUtil(node* rootm){
  1591. if(rootm == NULL)
  1592. return;
  1593.  
  1594. int arr[100];
  1595. int aIndex = 0;
  1596. queue q;
  1597. q.enQueue(rootm);
  1598. bool alternative = true;
  1599.  
  1600. while(!q.isEmpty()){
  1601. int size = q.size();
  1602. while(size--){
  1603. node* temp = q.deQueue();
  1604. if(!alternative)
  1605. temp->data = arr[--aIndex];
  1606. cout<<temp->data<<" ";
  1607. if(temp->left){
  1608. q.enQueue(temp->left);
  1609. if(alternative)
  1610. arr[aIndex++] = temp->left->data;
  1611. }
  1612. if(temp->right){
  1613. q.enQueue(temp->right);
  1614. if(alternative)
  1615. arr[aIndex++] = temp->right->data;
  1616. }
  1617. }
  1618. alternative = !alternative;
  1619. cout<<endl;
  1620. }
  1621.  
  1622. }
  1623.  
  1624. int maxSumBetweenAnyTwoLeavesUtil(node* root, int& maxSum){
  1625. if(root == NULL)
  1626. return 0;
  1627.  
  1628. int leftMaxSum = maxSumBetweenAnyTwoLeavesUtil(root->left, maxSum);
  1629. int rightMaxSum = maxSumBetweenAnyTwoLeavesUtil(root->right, maxSum);
  1630. int max = MAX(MAX(leftMaxSum, rightMaxSum), leftMaxSum+rightMaxSum+root->data);
  1631. maxSum = max > maxSum ? max : maxSum;
  1632. return MAX(leftMaxSum, rightMaxSum)+root->data;
  1633. }
  1634.  
  1635. void updateNumberOfLeftAndRightChildsUtil(node* rootm, int& count){
  1636. if(rootm == NULL){
  1637. count--;
  1638. return;
  1639. }
  1640.  
  1641. int lCount = count;
  1642. updateNumberOfLeftAndRightChildsUtil(rootm->left, ++count);
  1643. rootm->leftChilds = count - lCount;
  1644.  
  1645. updateNumberOfLeftAndRightChildsUtil(rootm->right, ++count);
  1646. rootm->rightChilds = count - lCount - rootm->leftChilds;
  1647. }
  1648.  
  1649. node* updateLeftMinAndRightMaxUtil(node* rootm)
  1650. {
  1651. if(rootm == NULL)
  1652. return NULL;
  1653.  
  1654. node* left = updateLeftMinAndRightMaxUtil(rootm->left);
  1655. node* right = updateLeftMinAndRightMaxUtil(rootm->right);
  1656.  
  1657. rootm->leftMin = left ? left->leftMin : rootm->data;
  1658. rootm->rightMax = right ? right->rightMax : rootm->data;
  1659. return rootm;
  1660. }
  1661.  
  1662. node* largestBSTTreeinBTreeUtil(node* root, int& maxChilds, node** maxNode, int& count){
  1663. if(root == NULL){
  1664. count--;
  1665. return NULL;
  1666. }
  1667.  
  1668. int lCount = count;
  1669. node* left = largestBSTTreeinBTreeUtil(root->left, maxChilds, maxNode, ++count);
  1670. root->leftChilds = count - lCount;
  1671.  
  1672. node* right = largestBSTTreeinBTreeUtil(root->right, maxChilds, maxNode, ++count);
  1673. root->rightChilds = count - lCount - root->leftChilds;
  1674.  
  1675. root->leftMin = left ? left->leftMin : root->data;
  1676. root->rightMax = right ? right->rightMax : root->data;
  1677.  
  1678. if((root->left == NULL && root->right == NULL)||
  1679. (root->left != NULL && root->right == NULL && root->left->isBST && root->data > root->left->rightMax)||
  1680. (root->right != NULL && root->left == NULL && root->right->isBST && root->data < root->right->leftMin)||
  1681. (root->left != NULL && root->right != NULL && root->left->isBST && root->right->isBST && root->left->rightMax < root->data && root->right->leftMin > root->data)
  1682. ){
  1683. root->isBST = true;
  1684. if(root->leftChilds + root->rightChilds + 1 > maxChilds){
  1685. maxChilds = root->leftChilds + root->rightChilds + 1;
  1686. *maxNode = root;
  1687. }
  1688.  
  1689. }
  1690.  
  1691. return root;
  1692. }
  1693.  
  1694. void swap(int& a, int& b){
  1695. int c = a;
  1696. a = b;
  1697. b = c;
  1698. }
  1699.  
  1700. void maxHeapify(int* arr, int size, int i)
  1701. {
  1702. int largest = i;
  1703. int left = 2 * i + 1;
  1704. int right = 2 * i + 2;
  1705.  
  1706. if(left < size && arr[left] > arr[largest])
  1707. largest = left;
  1708.  
  1709. if(right < size && arr[right] > arr[largest])
  1710. largest = right;
  1711.  
  1712. if(largest != i){
  1713. swap(arr[i], arr[largest]);
  1714. maxHeapify(arr, size, largest);
  1715. }
  1716. }
  1717.  
  1718. void heapSortIncUtil(int* arr, int size)
  1719. {
  1720. //Build Max-Heapify.
  1721. for(int i = ((size/2) - 1) ; i >= 0; i--)
  1722. maxHeapify(arr, size, i);
  1723.  
  1724. for(int i = size-1; i >= 0; i--){
  1725. swap(arr[0], arr[i]);
  1726. maxHeapify(arr, i, 0);
  1727. }
  1728.  
  1729. for(int i = 0; i < size; i++)
  1730. cout<<arr[i]<<" ";
  1731. cout<<endl;
  1732. }
  1733.  
  1734. void minHeapify(int* arr, int size, int i)
  1735. {
  1736. int smallest = i;
  1737. int left = 2 * i + 1;
  1738. int right = 2 * i + 2;
  1739.  
  1740. if(left < size && arr[left] < arr[smallest])
  1741. smallest = left;
  1742. if(right < size && arr[right] < arr[smallest])
  1743. smallest = right;
  1744.  
  1745. if(smallest != i){
  1746. swap(arr[i], arr[smallest]);
  1747. minHeapify(arr, size, smallest);
  1748. }
  1749. }
  1750.  
  1751. void heapSortDecUtil(int* arr, int size)
  1752. {
  1753. //Build Min-Heapify
  1754. for(int i = size/2-1; i >= 0; i--)
  1755. minHeapify(arr, size, i);
  1756.  
  1757. for(int i = size-1; i >= 0; i--){
  1758. swap(arr[0], arr[i]);
  1759. minHeapify(arr, i, 0);
  1760. }
  1761.  
  1762. for(int i = 0; i < size; i++)
  1763. cout<<arr[i]<<" ";
  1764. cout<<endl;
  1765. }
  1766.  
  1767. struct HeapNode{
  1768. int data;
  1769. int row;
  1770. int col;
  1771. HeapNode(){
  1772. data = -1;
  1773. row = -1;
  1774. col = -1;
  1775. }
  1776. HeapNode(int mdata, int mrow, int mcol){
  1777. data = mdata;
  1778. row = mrow;
  1779. col = mcol;
  1780. }
  1781. };
  1782.  
  1783. void minHeapForKSortedArrayUtil(HeapNode* node, int size, int i){
  1784. int smallest = i;
  1785. int left = 2 * i + 1;
  1786. int right = 2 * i + 2;
  1787.  
  1788. if(left < size && node[left].data < node[smallest].data)
  1789. smallest = left;
  1790.  
  1791. if(right < size && node[right].data < node[smallest].data)
  1792. smallest = right;
  1793.  
  1794. if(smallest != i){
  1795. swap(node[i].data, node[smallest].data);
  1796. swap(node[i].row, node[smallest].row);
  1797. swap(node[i].col, node[smallest].col);
  1798. minHeapForKSortedArrayUtil(node, size, smallest);
  1799. }
  1800. }
  1801.  
  1802. void minHeapForKSortedArray(HeapNode* heap, int size){
  1803. for(int i = size/2-1; i >= 0; i--)
  1804. minHeapForKSortedArrayUtil(heap, size, i);
  1805. }
  1806.  
  1807. void mergeKSortedArraysUtil(int arr[4][4], int row, int col)
  1808. {
  1809. HeapNode nodes[4][4];
  1810. for(int i = 0; i < row; i++){
  1811. for(int j = 0; j < col; j++){
  1812. nodes[i][j].data = arr[i][j];
  1813. nodes[i][j].row = i;
  1814. nodes[i][j].col = j;
  1815. }
  1816. }
  1817.  
  1818. int result[row*col];
  1819. int resultIndex = 0;
  1820.  
  1821. HeapNode heap[row];
  1822. for(int i = 0; i < row; i++)
  1823. heap[i] = nodes[i][0];
  1824.  
  1825. while(!(resultIndex >= row*col)){
  1826. //minHeap
  1827. minHeapForKSortedArray(heap, row);
  1828.  
  1829. //min heap node
  1830. HeapNode& min = heap[0];
  1831. result[resultIndex++] = min.data;
  1832. min = min.col < col - 1 ? nodes[min.row][min.col + 1] : HeapNode(INF, INF, INF);
  1833. }
  1834.  
  1835. for(int i = 0; i < row*col; i++)
  1836. cout<<result[i]<<" ";
  1837. cout<<endl;
  1838. }
  1839.  
  1840. int nodeHeight(node* n){
  1841. return n ? n->height : 0;
  1842. }
  1843.  
  1844. int getBalance(node* n){
  1845. if(n == NULL)
  1846. return 0;
  1847.  
  1848. return nodeHeight(n->left) - nodeHeight(n->right);
  1849. }
  1850.  
  1851. node* rotateLeft(node* root){
  1852. node* A = root;
  1853. node* B = A->right;
  1854. node* T2 = B->left;
  1855.  
  1856. /*rotate*/
  1857. B->left = A;
  1858. A->right = T2;
  1859.  
  1860. /*update Heights*/
  1861. A->height = MAX(nodeHeight(A->left), nodeHeight(A->right)) + 1;
  1862. B->height = MAX(nodeHeight(B->left), nodeHeight(B->right)) + 1;
  1863.  
  1864. return B;
  1865. }
  1866.  
  1867. node* rotateRight(node* root){
  1868. node* A = root;
  1869. node* B = A->left;
  1870. node* T2 = B->right;
  1871.  
  1872. /*rotate*/
  1873. B->right = A;
  1874. A->left = T2;
  1875.  
  1876. /*update Heights*/
  1877. A->height = MAX(nodeHeight(A->left), nodeHeight(A->right)) + 1;
  1878. B->height = MAX(nodeHeight(B->left), nodeHeight(B->right)) + 1;
  1879.  
  1880. return B;
  1881. }
  1882.  
  1883. node* avlInsertionUtil(node* root, int data){
  1884. /*1. Normal BST insertion*/
  1885. if(root == NULL)
  1886. return createNode(data);
  1887.  
  1888. if(data < root->data){
  1889. root->left = avlInsertionUtil(root->left, data);
  1890. }else if(data > root->data){
  1891. root->right = avlInsertionUtil(root->right, data);
  1892. }else
  1893. return root;
  1894.  
  1895. /*2. Height of node */
  1896. root->height = MAX(nodeHeight(root->left), nodeHeight(root->right)) + 1;
  1897.  
  1898. /*3. Get Balance */
  1899. int balance = getBalance(root);
  1900.  
  1901. /*4. If Unbalanced Rotate Accordingly */
  1902. /* right rotate */
  1903. if(balance > 1 && data < root->left->data)
  1904. return rotateRight(root);
  1905.  
  1906. /* left rotate */
  1907. if(balance < -1 && data > root->right->data)
  1908. return rotateLeft(root);
  1909.  
  1910. /* left right rotate */
  1911. if(balance > 1 && data > root->left->data){
  1912. root->left = rotateLeft(root->left);
  1913. return rotateRight(root);
  1914. }
  1915.  
  1916. /* right left rotate */
  1917. if(balance < -1 && data < root->right->data){
  1918. root->right = rotateRight(root->right);
  1919. return rotateLeft(root);
  1920. }
  1921.  
  1922. return root;
  1923. }
  1924.  
  1925. node* avlDeletionUtil(node* root, int data){
  1926. if(root == NULL)
  1927. return NULL;
  1928.  
  1929. if(data < root->data){
  1930. root->left = avlDeletionUtil(root->left, data);
  1931. } else if(data > root->data){
  1932. root->right = avlDeletionUtil(root->right, data);
  1933. } else {
  1934. if(root->right == NULL && root->left == NULL){
  1935. delete root;
  1936. return NULL;
  1937. } else if(root->left != NULL && root->right == NULL){
  1938. node* temp = root->left;
  1939. temp->parent = root->parent;
  1940. delete root;
  1941. return temp;
  1942. } else if(root->left == NULL && root->right != NULL){
  1943. node* temp = root->right;
  1944. temp->parent = root->parent;
  1945. delete root;
  1946. return temp;
  1947. } else {
  1948. node* minNode = getMinNode(root->right);
  1949. root->data = minNode->data;
  1950. root->right = avlDeletionUtil(root->right, minNode->data);
  1951. }
  1952. }
  1953.  
  1954. /* 2. Update Height */
  1955. root->height = MAX(nodeHeight(root->left), nodeHeight(root->right)) + 1;
  1956.  
  1957. /* 3. get balance */
  1958. int balance = getBalance(root);
  1959.  
  1960. /* if unbalanced perform rotate operations */
  1961. /*left - left unbalanced*/
  1962. if(balance > 1 && getBalance(root->left) >= 0)
  1963. return rotateRight(root);
  1964.  
  1965. /*right - right unbalanced*/
  1966. if(balance < -1 && getBalance(root->right) <= 0)
  1967. return rotateLeft(root);
  1968.  
  1969. /* left - right unbalanced*/
  1970. if(balance > 1 && getBalance(root->left) < 0){
  1971. root->left = rotateLeft(root->left);
  1972. return rotateRight(root);
  1973. }
  1974.  
  1975. /* right - left unbalanced*/
  1976. if(balance < -1 && getBalance(root->right) > 0){
  1977. root->right = rotateRight(root->right);
  1978. return rotateLeft(root);
  1979. }
  1980.  
  1981. return root;
  1982. }
  1983. /* MuVen ---------------------------------------------------------- */
  1984. void insertInToBST(){
  1985. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  1986. node* root = NULL;
  1987. for(int i = 0; i < 7; i++){
  1988. root = insert(root, arr[i]);
  1989. }
  1990. inOrder(root);
  1991. }
  1992.  
  1993. void insertInToBSTByItr(){
  1994. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  1995. node* root = NULL;
  1996. for(int i = 0; i < 7; i++){
  1997. root = insertByItr(root, arr[i]);
  1998. }
  1999. inOrder(root);
  2000. }
  2001.  
  2002. void preOrderBST(){
  2003. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2004. node* root = NULL;
  2005. for(int i = 0; i < 7; i++){
  2006. root = insert(root, arr[i]);
  2007. }
  2008. preOrder(root);
  2009. }
  2010.  
  2011. void postOrderBST(){
  2012. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2013. node* root = NULL;
  2014. for(int i = 0; i < 7; i++){
  2015. root = insert(root, arr[i]);
  2016. }
  2017. postOrder(root);
  2018. }
  2019.  
  2020. void inOrderBST(){
  2021. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2022. node* root = NULL;
  2023. for(int i = 0; i < 7; i++){
  2024. root = insert(root, arr[i]);
  2025. }
  2026. inOrder(root);
  2027. }
  2028.  
  2029. void preOrderBSTWithoutRecurr(){
  2030. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2031. node* root = NULL;
  2032. for(int i = 0; i < 7; i++){
  2033. root = insert(root, arr[i]);
  2034. }
  2035. preOrderWithoutRecurr(root);
  2036. }
  2037.  
  2038.  
  2039. void postOrderBSTWithoutRecurr(){
  2040. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2041. node* root = NULL;
  2042. for(int i = 0; i < 7; i++){
  2043. root = insert(root, arr[i]);
  2044. }
  2045. postOrderWithoutRecurr(root);
  2046. }
  2047.  
  2048. void inOrderBSTWithoutRecurr(){
  2049. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2050. node* root = NULL;
  2051. for(int i = 0; i < 7; i++){
  2052. root = insert(root, arr[i]);
  2053. }
  2054. inOrderWithoutRecurr(root);
  2055. }
  2056.  
  2057. void greaterSumTree(){
  2058. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2059. node* root = NULL;
  2060. for(int i = 0; i < 7; i++){
  2061. root = insert(root, arr[i]);
  2062. }
  2063. int sum = 0;
  2064. greaterSumTreeUtil(root, sum);
  2065. inOrder(root);
  2066. }
  2067.  
  2068. void sumOfAllLeftLeavesBST(){
  2069. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2070. node* root = NULL;
  2071. for(int i = 0; i < 11; i++){
  2072. root = insert(root, arr[i]);
  2073. }
  2074.  
  2075. int sumLeftLeaves = 0;
  2076. sumOfAllLeftLeavesBSTUtil(root, sumLeftLeaves);
  2077. cout<<"Sum of All Left Leaves "<<sumLeftLeaves;
  2078. }
  2079.  
  2080. void convertBinaryTreeToSumTree(){
  2081. node* root = createNode(5);
  2082. root->left = createNode(-1);
  2083. root->left->left = createNode(-2);
  2084. root->left->right = createNode(4);
  2085. root->right = createNode(3);
  2086. root->right->left = createNode(-6);
  2087. root->right->right = createNode(10);
  2088.  
  2089. convertBinaryTreeToSumTreeUtil(root);
  2090. inOrder(root);
  2091. }
  2092.  
  2093. void deleteInBST(){
  2094. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2095. node* root = NULL;
  2096. for(int i = 0; i < 11; i++){
  2097. root = insert(root, arr[i]);
  2098. }
  2099.  
  2100. inOrder(root);cout<<endl;
  2101. deleteNode(root, 50);
  2102. inOrder(root);cout<<endl;
  2103. deleteNode(root, 55);
  2104. inOrder(root);
  2105. }
  2106.  
  2107. void findInBST(){
  2108. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2109. node* root = NULL;
  2110. for(int i = 0; i < 11; i++){
  2111. root = insert(root, arr[i]);
  2112. }
  2113.  
  2114. findInBSTUtilReccur(root, 30) ? cout<<"30 found in BST by Reccur" : cout<<"30 not found in BST";cout<<endl;
  2115. findInBSTUtilReccur(root, 33) ? cout<<"33 found in BST by Reccur" : cout<<"33 not found in BST";cout<<endl;
  2116. findInBSTUtilItr(root, 30) ? cout<<"30 found in BST by Itr" : cout<<"30 not found in BST";cout<<endl;
  2117. findInBSTUtilItr(root, 33) ? cout<<"33 found in BST by Itr" : cout<<"33 not found in BST";
  2118. }
  2119.  
  2120. void sizeOfTree(){
  2121. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2122. node* root = NULL;
  2123. for(int i = 0; i < 11; i++){
  2124. root = insert(root, arr[i]);
  2125. }
  2126.  
  2127. int size = 0;
  2128. sizeOfTreeUtilByRecurr(root, size);
  2129. cout<<"size by recurr:"<<size<<endl;
  2130.  
  2131. size = 0;
  2132. sizeOfTreeUtilWithoutRecurr(root, size);
  2133. cout<<"size without recurr:"<<size;
  2134. }
  2135.  
  2136. void BFSOfTree(){
  2137. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2138. node* root = NULL;
  2139. for(int i = 0; i < 11; i++){
  2140. root = insert(root, arr[i]);
  2141. }
  2142.  
  2143. BFSOfTreeUtil(root);
  2144. }
  2145.  
  2146. void diameterOfTree(){
  2147. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2148. node* root = NULL;
  2149. for(int i = 0; i < 11; i++){
  2150. root = insert(root, arr[i]);
  2151. }
  2152.  
  2153. cout<<"Diameter of tree:"<<DiameterOfTreeUtil(root);cout<<endl;
  2154. cout<<"Diameter of tree(optimize):"<<DiameterOfTreeUtilOptimize(root).diameter;
  2155. }
  2156.  
  2157. void maxElementOfTree(){
  2158. node* root = createNode(5);
  2159. root->left = createNode(-1);
  2160. root->left->left = createNode(-2);
  2161. root->left->right = createNode(4);
  2162. root->right = createNode(3);
  2163. root->right->left = createNode(-6);
  2164. root->right->right = createNode(10);
  2165.  
  2166. int max = MINV;
  2167. maxElementOfTreeUtil(root, max);
  2168. cout<<"Max element of tree:"<<max;cout<<endl;
  2169. cout<<"Max element of tree By Reccur:"<<maxElementOfTreeUtilByRecurr(root);
  2170. }
  2171.  
  2172. void mirrorTree(){
  2173. node* root = createNode(5);
  2174. root->left = createNode(-1);
  2175. root->left->left = createNode(-2);
  2176. root->left->right = createNode(4);
  2177. root->right = createNode(3);
  2178. root->right->left = createNode(-6);
  2179. root->right->right = createNode(10);
  2180.  
  2181. node* rootm = createNode(5);
  2182. rootm->left = createNode(3);
  2183. rootm->left->left = createNode(10);
  2184. rootm->left->right = createNode(-6);
  2185. rootm->right = createNode(-1);
  2186. rootm->right->left = createNode(4);
  2187. rootm->right->right = createNode(-2);
  2188.  
  2189. mirrorTreeUtil(root, rootm) ? cout<<"Both trees are mirror to each other":cout<<"Both trees are not mirror to each other";
  2190. }
  2191.  
  2192. void createMirrorTree(){
  2193. node* rootm = createNode(5);
  2194. rootm->left = createNode(3);
  2195. rootm->left->left = createNode(10);
  2196. rootm->left->right = createNode(-6);
  2197. rootm->right = createNode(-1);
  2198. rootm->right->left = createNode(4);
  2199. rootm->right->right = createNode(-2);
  2200.  
  2201. node* mroot = createNode(5);
  2202. mroot->left = createNode(-1);
  2203. mroot->left->left = createNode(-2);
  2204. mroot->left->right = createNode(4);
  2205. mroot->right = createNode(3);
  2206. mroot->right->left = createNode(-6);
  2207. mroot->right->right = createNode(10);
  2208.  
  2209. createMirrorTreeUtil(mroot);
  2210. isTreesSame(mroot, rootm) ? cout<<"created mirror tree successfully":cout<<"Couldnt created mirror tree";
  2211. }
  2212.  
  2213. void printAllPathsFromRootToLeaf(){
  2214. node* rootm = createNode(5);
  2215. rootm->left = createNode(3);
  2216. rootm->left->left = createNode(2);
  2217. rootm->left->right = createNode(2);
  2218. rootm->right = createNode(1);
  2219. rootm->right->left = createNode(4);
  2220. rootm->right->right = createNode(4);
  2221.  
  2222. int path[100] = {0};
  2223. int pathIndex = 0;
  2224. int sum = 10;
  2225. printAllPathsFromRootToLeafUtil(rootm, sum, path, pathIndex);
  2226. }
  2227.  
  2228. void reverseLevelOrderTraversal(){
  2229. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2230. node* rootm = NULL;
  2231. for(int i = 0; i < 11; i++){
  2232. rootm = insert(rootm, arr[i]);
  2233. }
  2234.  
  2235. queue q;
  2236. if(rootm)
  2237. q.enQueue(rootm);
  2238. reverseLevelOrderTraversalUtil(q);
  2239. if(rootm)
  2240. cout<<rootm->data<<endl;
  2241. }
  2242.  
  2243. void findHeightWithoutRecurrsion(){
  2244. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2245. node* rootm = NULL;
  2246. for(int i = 0; i < 11; i++){
  2247. rootm = insert(rootm, arr[i]);
  2248. }
  2249.  
  2250. cout<<"Height of the tree without Recurrsion:"<<findHeightWithoutRecurrsionUtil(rootm);
  2251. }
  2252.  
  2253. void printAllFullNodesInBTree()
  2254. {
  2255. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2256. node* rootm = NULL;
  2257. for(int i = 0; i < 11; i++){
  2258. rootm = insert(rootm, arr[i]);
  2259. }
  2260.  
  2261. printAllFullNodesInBTreeUtil(rootm);
  2262. }
  2263.  
  2264. void printBottomViewOfBST(){
  2265. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2266. node* rootm = NULL;
  2267. for(int i = 0; i < 11; i++){
  2268. rootm = insert(rootm, arr[i]);
  2269. }
  2270.  
  2271. map<int, node*> bView;
  2272. printBottomViewOfBSTUtil(rootm, 0, bView);
  2273.  
  2274. for(map<int, node*>::iterator itr = bView.begin(); itr != bView.end(); ++itr){
  2275. node* temp = itr->second;
  2276. if(temp){
  2277. cout<<temp->data<<" ";
  2278. }
  2279. }
  2280. }
  2281.  
  2282. void constructBSTFromPreOrder(){
  2283. int arr[7] = {50, 30, 20, 40, 70, 60, 80};
  2284.  
  2285. int preOrderIndex = 0;
  2286. node* root = constructBSTFromPreOrderUtil(arr, preOrderIndex, arr[preOrderIndex], MINV, MAXV, 7);
  2287. preOrder(root);
  2288. }
  2289.  
  2290. void printTopViewOfBST(){
  2291. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 35, 55, 75};
  2292. node* rootm = NULL;
  2293. for(int i = 0; i < 11; i++){
  2294. rootm = insert(rootm, arr[i]);
  2295. }
  2296.  
  2297. map<int, node*> tView;
  2298. printTopViewOfBSTUtil(rootm, 0, tView);
  2299. for(map<int, node*>::iterator itr = tView.begin(); itr != tView.end(); ++itr){
  2300. node* temp = itr->second;
  2301. if(temp)
  2302. cout<<temp->data<<" ";
  2303. }
  2304. }
  2305.  
  2306. void printInorderSuccessorAndPredecessor(){
  2307. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75};
  2308. node* rootm = NULL;
  2309. for(int i = 0; i < 11; i++){
  2310. rootm = insert(rootm, arr[i]);
  2311. }
  2312.  
  2313. node* ioSuccessor = inOrderSuccessor(rootm->left->right->right);
  2314. if(ioSuccessor)
  2315. cout<<"Inorder Successor:"<<ioSuccessor->data<<endl;
  2316.  
  2317. node* ioPredecessor = inOrderPredecessor(rootm->right->left->left);
  2318. if(ioPredecessor)
  2319. cout<<"Inorder Predecessor:"<<ioPredecessor->data;
  2320. }
  2321.  
  2322. void printHeightOfTree(){
  2323. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75};
  2324. node* rootm = NULL;
  2325. for(int i = 0; i < 11; i++){
  2326. rootm = insert(rootm, arr[i]);
  2327. }
  2328.  
  2329. cout<<"Height Of Tree:"<<getHeightOfTree(rootm);
  2330. }
  2331.  
  2332. void printHeightOfNodeInTree(){
  2333. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2334. node* rootm = NULL;
  2335. for(int i = 0; i < 12; i++){
  2336. rootm = insert(rootm, arr[i]);
  2337. }
  2338.  
  2339. int height = printHeightOfNodeInTreeUtil(rootm, rootm->left->left->left->left, 0);
  2340. cout<<"Node found at height:"<<height;
  2341. }
  2342.  
  2343. void printIfTreeBalancedOrNot(){
  2344. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2345. node* rootm = NULL;
  2346. for(int i = 0; i < 12; i++){
  2347. rootm = insert(rootm, arr[i]);
  2348. }
  2349.  
  2350. int arr1[11] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75};
  2351. node* rootm1 = NULL;
  2352. for(int i = 0; i < 11; i++){
  2353. rootm1 = insert(rootm1, arr1[i]);
  2354. }
  2355.  
  2356. printIfTreeBalancedOrNotUtil(rootm) ? cout<<"Tree is Balanced" : cout<<"Tree is not Balanced";cout<<endl;
  2357. printIfTreeBalancedOrNotUtil(rootm1) ? cout<<"Tree is Balanced" : cout<<"Tree is not Balanced";cout<<endl;
  2358.  
  2359. int arr2[7] = {10, 5, 15, 3, 7, 12, 20};
  2360. node* rootm2 = NULL;
  2361. for(int i = 0; i < 7; i++){
  2362. rootm2 = insert(rootm2, arr2[i]);
  2363. }
  2364. printIfTreeBalancedOrNotUtilOptimized(rootm2);
  2365. rootm2->isBalanced ? cout<<"Tree is Balanced" : cout<<"Tree is not Balanced";cout<<endl;
  2366.  
  2367. int arr3[9] = {10, 5, 15, 3, 7, 12, 20, 1, 0};
  2368. node* rootm3 = NULL;
  2369. for(int i = 0; i < 9; i++){
  2370. rootm3 = insert(rootm3, arr3[i]);
  2371. }
  2372. printIfTreeBalancedOrNotUtilOptimized(rootm3);
  2373. rootm3->isBalanced ? cout<<"Tree is Balanced" : cout<<"Tree is not Balanced";cout<<endl;
  2374.  
  2375. int arr4[3] = {10, 20, 30};
  2376. node* rootm4 = NULL;
  2377. for(int i = 0; i < 3; i++){
  2378. rootm4 = insert(rootm4, arr4[i]);
  2379. }
  2380. printIfTreeBalancedOrNotUtilOptimized(rootm4);
  2381. rootm4->isBalanced ? cout<<"Tree is Balanced" : cout<<"Tree is not Balanced";
  2382. }
  2383.  
  2384. void sortedArrayToBST(){
  2385. int arr[9]={1,2,3,4,5,6,7,8,9};
  2386. node* root = sortedArrayToBSTUtil(arr, 0, 8);
  2387. cout<<"BST inorder:";
  2388. inOrder(root);
  2389. }
  2390.  
  2391. void pathFromRootToNode(){
  2392. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2393. node* rootm = NULL;
  2394. for(int i = 0; i < 12; i++){
  2395. rootm = insert(rootm, arr[i]);
  2396. }
  2397.  
  2398. int path[100];
  2399. int pathIndex = 0;
  2400. bool found = false;
  2401. pathFromRootToNodeUtil(rootm, rootm->left->left->left->left, path, pathIndex, found);
  2402. }
  2403.  
  2404. void sizeOfTree2(){
  2405. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2406. node* rootm = NULL;
  2407. for(int i = 0; i < 12; i++){
  2408. rootm = insert(rootm, arr[i]);
  2409. }
  2410.  
  2411. cout<<"size of tree:"<<sizeOfTree2Util(rootm);
  2412. }
  2413.  
  2414. void isBST(){
  2415. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2416. node* rootm = NULL;
  2417. for(int i = 0; i < 12; i++){
  2418. rootm = insert(rootm, arr[i]);
  2419. }
  2420.  
  2421. isBSTUtil(rootm) ? cout<<"Is Binary Search Tree" : cout<<"Not Binary Search Tree";cout<<endl;
  2422. isBSTUtilByMinMax(rootm, MINV, MAXV) ? cout<<"Is Binary Search Tree" : cout<<"Not Binary Search Tree";cout<<endl;
  2423.  
  2424. node* mroot = createNode(5);
  2425. mroot->left = createNode(-1);
  2426. mroot->left->left = createNode(-2);
  2427. mroot->left->right = createNode(4);
  2428. mroot->right = createNode(3);
  2429. mroot->right->left = createNode(-6);
  2430. mroot->right->right = createNode(10);
  2431. isBSTUtil(mroot) ? cout<<"Is Binary Search Tree" : cout<<"Not Binary Search Tree";cout<<endl;
  2432. isBSTUtilByMinMax(mroot, MINV, MAXV) ? cout<<"Is Binary Search Tree" : cout<<"Not Binary Search Tree";
  2433. }
  2434.  
  2435. void findMaxWidthOfTree(){
  2436. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2437. node* rootm = NULL;
  2438. for(int i = 0; i < 12; i++){
  2439. rootm = insert(rootm, arr[i]);
  2440. }
  2441.  
  2442. cout<<"Max width in the tree:"<<findMaxWidthOfTree(rootm);
  2443. }
  2444.  
  2445. void printNodesBetweenTwoLevels(){
  2446. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2447. node* rootm = NULL;
  2448. for(int i = 0; i < 12; i++){
  2449. rootm = insert(rootm, arr[i]);
  2450. }
  2451.  
  2452. printNodesBetweenTwoLevelsUtil(rootm, 2, 4);
  2453. }
  2454.  
  2455. void isBSTEqual(){
  2456. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2457. node* rootm = NULL;
  2458. for(int i = 0; i < 12; i++){
  2459. rootm = insert(rootm, arr[i]);
  2460. }
  2461.  
  2462. int arr2[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2463. node* rootm2 = NULL;
  2464. for(int i = 0; i < 12; i++){
  2465. rootm2 = insert(rootm2, arr2[i]);
  2466. }
  2467.  
  2468. isBSTEqualUtil(rootm, rootm2) ? cout<<"Both Trees are equal":cout<<"Both Trees are not equal";
  2469. }
  2470.  
  2471. void printingRightView(){
  2472. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2473. node* rootm = NULL;
  2474. for(int i = 0; i < 12; i++){
  2475. rootm = insert(rootm, arr[i]);
  2476. }
  2477.  
  2478. int currLevel = 0;
  2479. printingRightViewUtil(rootm, currLevel, 1);
  2480. }
  2481.  
  2482. void printingLeftView(){
  2483. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2484. node* rootm = NULL;
  2485. for(int i = 0; i < 12; i++){
  2486. rootm = insert(rootm, arr[i]);
  2487. }
  2488.  
  2489. int currLevel = 0;
  2490. printingLeftViewUtil(rootm, currLevel, 1);
  2491. }
  2492.  
  2493. void LCABST(){
  2494. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2495. node* rootm = NULL;
  2496. for(int i = 0; i < 12; i++){
  2497. rootm = insert(rootm, arr[i]);
  2498. }
  2499.  
  2500. node* lca = LCABSTUtil(rootm, rootm->left->left, rootm->left->right);
  2501. lca ? cout<<"LCA1:"<<lca->data: cout<<"No LCA found";cout<<endl;
  2502. lca = LCABSTUtil(rootm, rootm->left->left, rootm->right->right);
  2503. lca ? cout<<"LCA2:"<<lca->data: cout<<"No LCA found";cout<<endl;
  2504. lca = LCABSTUtil(rootm, rootm->right->left, rootm->right->right);
  2505. lca ? cout<<"LCA3:"<<lca->data: cout<<"No LCA found";cout<<endl;
  2506. lca = LCABSTUtil(rootm, NULL, rootm->right->right);
  2507. lca ? cout<<"LCA4:"<<lca->data: cout<<"No LCA found";
  2508. }
  2509.  
  2510. void LCABTree(){
  2511. node* mroot = createNode(5);
  2512. mroot->left = createNode(-1);
  2513. mroot->left->left = createNode(-2);
  2514. mroot->left->right = createNode(4);
  2515. mroot->right = createNode(3);
  2516. mroot->right->left = createNode(-6);
  2517. mroot->right->right = createNode(10);
  2518.  
  2519. node* lca = LCABTreeUtil(mroot, mroot->left->right, mroot->left->left);
  2520. lca ? cout<<"LCA of BTree :"<<lca->data: cout<<"No LCA found";
  2521. }
  2522.  
  2523. void verticalSumOfBST(){
  2524. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2525. node* rootm = NULL;
  2526. for(int i = 0; i < 12; i++){
  2527. rootm = insert(rootm, arr[i]);
  2528. }
  2529.  
  2530. map<int, int> vSum;
  2531. verticalSumOfBST(rootm,0, vSum);
  2532. for(map<int, int>::iterator itr = vSum.begin(); itr != vSum.end(); ++itr){
  2533. cout<<itr->second<<" ";
  2534. }
  2535. }
  2536.  
  2537. void printTreeInVerticalOrderPath(){
  2538. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2539. node* rootm = NULL;
  2540. for(int i = 0; i < 12; i++){
  2541. rootm = insert(rootm, arr[i]);
  2542. }
  2543.  
  2544. map<int, vector<node*> > vOrder;
  2545. printTreeInVerticalOrderPathUtil(rootm, 0, vOrder);
  2546. for(map<int, vector<node*> >::iterator itr = vOrder.begin(); itr != vOrder.end(); ++itr){
  2547. cout<<itr->first<<" ";
  2548. vector<node*> temp = itr->second;
  2549. for(vector<node*>::iterator itr = temp.begin(); itr != temp.end(); ++itr){
  2550. node* temp = *itr;
  2551. cout<<temp->data<<" ";
  2552. }
  2553. cout<<endl;
  2554. }
  2555. }
  2556.  
  2557. void printNodesThatDontHaveSibilings(){
  2558. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2559. node* rootm = NULL;
  2560. for(int i = 0; i < 12; i++){
  2561. rootm = insert(rootm, arr[i]);
  2562. }
  2563.  
  2564. printNodesThatDontHaveSibilingsUtils(rootm);
  2565. }
  2566.  
  2567. void distanceOfNodeFromRoot(){
  2568. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2569. node* rootm = NULL;
  2570. for(int i = 0; i < 12; i++){
  2571. rootm = insert(rootm, arr[i]);
  2572. }
  2573.  
  2574. cout<<"Distance of node from root:"<<distanceOfNodeFromRootUtil(rootm, rootm->left->left->left->left, 0);
  2575. }
  2576.  
  2577. void printAllNodesAtDistanceDFromRoot(){
  2578. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2579. node* rootm = NULL;
  2580. for(int i = 0; i < 12; i++){
  2581. rootm = insert(rootm, arr[i]);
  2582. }
  2583.  
  2584. printAllNodesAtDistanceDFromRootUtil(rootm, 0, 3);
  2585. }
  2586.  
  2587. void distanceBetweenTwoNodes(){
  2588. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2589. node* rootm = NULL;
  2590. for(int i = 0; i < 12; i++){
  2591. rootm = insert(rootm, arr[i]);
  2592. }
  2593.  
  2594. cout<<"Distance between two nodes:"<<distanceBetweenTwoNodesUtil(rootm, rootm->left->left->left->left, rootm->right->right)<<endl;
  2595. cout<<"Distance between two nodes:"<<distanceBetweenTwoNodesUtil(rootm, rootm->left->left->left->left, rootm->left->right->right)<<endl;
  2596. cout<<"Distance between two nodes:"<<distanceBetweenTwoNodesUtil(rootm, rootm->left->left->left->left, rootm->right->right->left)<<endl;
  2597. cout<<"Distance between two nodes:"<<distanceBetweenTwoNodesUtil(rootm, rootm->left->left->left->left, rootm->right->right->left->left);
  2598. }
  2599.  
  2600. void maxSumFromRootToAllLeaves(){
  2601. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2602. node* rootm = NULL;
  2603. for(int i = 0; i < 12; i++){
  2604. rootm = insert(rootm, arr[i]);
  2605. }
  2606.  
  2607. int mSum = MINV;
  2608. maxSumFromRootToAllLeavesUtil(rootm, 0, mSum);
  2609. cout<<"Max Sum is :"<<mSum;
  2610. }
  2611.  
  2612. void printAllNodesAtDistanceDFromLeaf(){
  2613. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2614. node* rootm = NULL;
  2615. for(int i = 0; i < 12; i++){
  2616. rootm = insert(rootm, arr[i]);
  2617. }
  2618.  
  2619. int path[100] = {0};
  2620. int pathIndex = 0;
  2621. bool pathp[100] = {false};
  2622. printAllNodesAtDistanceDFromLeafUtil(rootm, 0, 2, path, pathIndex, pathp);
  2623. }
  2624.  
  2625. void printBTreeInZigZag(){
  2626. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2627. node* rootm = NULL;
  2628. for(int i = 0; i < 12; i++){
  2629. rootm = insert(rootm, arr[i]);
  2630. }
  2631.  
  2632. printBTreeInZigZagUtil(rootm);
  2633. }
  2634.  
  2635. void printTreeIsSubTreeOfATree(){
  2636. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2637. node* rootm = NULL;
  2638. for(int i = 0; i < 12; i++){
  2639. rootm = insert(rootm, arr[i]);
  2640. }
  2641.  
  2642. node* roots = createNode(70);
  2643. roots->right = createNode(80);
  2644. roots->left = createNode(60);
  2645. roots->left->left = createNode(55);
  2646. roots->right->left = createNode(75);
  2647. printTreeIsSubTreeOfATreeUtil(rootm, roots) ? cout<<"roots is subtree of rootm" : cout<<"roots is not subtree of rootm"; cout<<endl;
  2648.  
  2649. roots->right->left->right = createNode(77);
  2650. printTreeIsSubTreeOfATreeUtil(rootm, roots) ? cout<<"roots is subtree of rootm" : cout<<"roots is not subtree of rootm"; cout<<endl;
  2651.  
  2652. }
  2653.  
  2654. void bTreeFromInOrderPostOrder(){
  2655. int in[12] = {5, 10, 20, 30, 40, 45, 50, 55, 60, 70, 75, 80};
  2656. int post[12] = {5, 10, 20, 45, 40, 30, 55, 60, 75, 80, 70, 50};
  2657.  
  2658. int postIndex = 11;
  2659. node* bst = BSTFromInOrderPostOrderUtil(post, postIndex, MINV, MAXV);
  2660. postIndex = 11;
  2661. node* btree = bTreeFromInOrderPostOrderUtil(in, post, postIndex, 0, 11);
  2662.  
  2663. inOrder(bst);cout<<endl;
  2664. inOrder(btree);cout<<endl;
  2665. postOrder(bst);cout<<endl;
  2666. postOrder(btree);cout<<endl;
  2667. }
  2668.  
  2669. void bTreeFromInOrderPreOrder(){
  2670. int in[12] = {5, 10, 20, 30, 40, 45, 50, 55, 60, 70, 75, 80};
  2671. int pre[12] ={50, 30, 20, 10, 5, 40, 45, 70, 60, 55, 80, 75};
  2672.  
  2673. int preIndex = 0;
  2674. node* bst = BSTFromInOrderPreOrderUtil(pre, preIndex, MINV, MAXV, 11);
  2675.  
  2676. preIndex = 0;
  2677. node* btree = bTreeFromInOrderPreOrderUtil(in, pre, preIndex, 0, 11);
  2678.  
  2679. inOrder(bst);cout<<endl;
  2680. inOrder(btree);cout<<endl;
  2681. preOrder(bst);cout<<endl;
  2682. preOrder(btree);cout<<endl;
  2683. }
  2684.  
  2685. void isSibilings(){
  2686. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2687. node* rootm = NULL;
  2688. for(int i = 0; i < 12; i++){
  2689. rootm = insert(rootm, arr[i]);
  2690. }
  2691.  
  2692. isSibilingsUtil(rootm, rootm->left->left, rootm->left->right) ? cout<<"Both nodes are sibilings":cout<<"Both nodes are not sibilings";cout<<endl;
  2693. isSibilingsUtil(rootm, rootm->left->left, rootm->right) ? cout<<"Both nodes are sibilings":cout<<"Both nodes are not sibilings";cout<<endl;
  2694. isSibilingsUtil(rootm, rootm->right->right, rootm->right->left) ? cout<<"Both nodes are sibilings":cout<<"Both nodes are not sibilings";cout<<endl;
  2695. }
  2696.  
  2697. void isCousins(){
  2698. node* root = createNode(1);
  2699. root->left = createNode(2);
  2700. root->right = createNode(3);
  2701. root->left->left = createNode(4);
  2702. root->left->left->left = createNode(7);
  2703. root->right->left = createNode(6);
  2704. root->right->left->left = createNode(8);
  2705. root->right->left->right = createNode(9);
  2706.  
  2707. isCousinsUtil(root, root->left->left->left, root->right->left->right) ?
  2708. cout<<"Both Nodes are Cousins":cout<<"Both Nodes are not Cousins";cout<<endl;
  2709. isCousinsUtil(root, root->left->left->left, root->right->left) ?
  2710. cout<<"Both Nodes are Cousins":cout<<"Both Nodes are not Cousins";cout<<endl;
  2711. }
  2712.  
  2713. void printSibilingsOfNode(){
  2714. node* root = createNode(1);
  2715. root->left = createNode(2);
  2716. root->right = createNode(3);
  2717. root->left->left = createNode(4);
  2718. root->left->left->left = createNode(7);
  2719. root->right->left = createNode(6);
  2720. root->right->left->left = createNode(8);
  2721. root->right->left->right = createNode(9);
  2722. calcNextSiblings(root);
  2723.  
  2724. printSibilingsOfNodeUtil(root);cout<<endl;
  2725. printSibilingsOfNodeUtil(root->left);cout<<endl;
  2726. printSibilingsOfNodeUtil(root->left->left);cout<<endl;
  2727. printSibilingsOfNodeUtil(root->left->left->left);
  2728. }
  2729.  
  2730. void inOrderWithoutParent(){
  2731. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2732. node* rootm = NULL;
  2733. for(int i = 0; i < 12; i++){
  2734. rootm = insert(rootm, arr[i]);
  2735. }
  2736.  
  2737. node* io = inOrderWithoutParentUtil(rootm, rootm->left->right);
  2738. io?cout<<"Inorder without parent:"<<io->data:cout<<"No IO Successor";cout<<endl;
  2739. io = inOrderWithoutParentUtil(rootm, rootm->left->right->right);
  2740. io?cout<<"Inorder without parent:"<<io->data:cout<<"No IO Successor";cout<<endl;
  2741. io = inOrderWithoutParentUtil(rootm, rootm->right->right);
  2742. io?cout<<"Inorder without parent:"<<io->data:cout<<"No IO Successor";cout<<endl;
  2743. io = inOrderWithoutParentUtil(rootm, rootm->right->left->left);
  2744. io?cout<<"Inorder without parent:"<<io->data:cout<<"No IO Successor";cout<<endl;
  2745. }
  2746.  
  2747. void linkedListToBST(){
  2748. lnode* head = createLNode(1);
  2749. head->next = createLNode(2);
  2750. head->next->next = createLNode(3);
  2751. head->next->next->next = createLNode(4);
  2752. head->next->next->next->next = createLNode(5);
  2753. head->next->next->next->next->next = createLNode(6);
  2754. head->next->next->next->next->next->next = createLNode(7);
  2755. head->next->next->next->next->next->next->next = createLNode(8);
  2756. head->next->next->next->next->next->next->next->next = createLNode(9);
  2757.  
  2758. node* root = linkedListToBSTUtil(&head, 9);
  2759. inOrder(root);
  2760. }
  2761.  
  2762. void doubleLinkedListToBST(){
  2763. lnode* head = createLNode(1);
  2764. head->previous = NULL;
  2765.  
  2766. head->next = createLNode(2);
  2767. head->next->previous = head;
  2768.  
  2769. head->next->next = createLNode(3);
  2770. head->next->next->previous = head->next;
  2771.  
  2772. head->next->next->next = createLNode(4);
  2773. head->next->next->next->previous = head->next->next;
  2774.  
  2775. head->next->next->next->next = createLNode(5);
  2776. head->next->next->next->next->previous = head->next->next->next;
  2777.  
  2778. head->next->next->next->next->next = createLNode(6);
  2779. head->next->next->next->next->next->previous = head->next->next->next->next;
  2780.  
  2781. head->next->next->next->next->next->next = createLNode(7);
  2782. head->next->next->next->next->next->next->previous = head->next->next->next->next->next;
  2783.  
  2784. head->next->next->next->next->next->next->next = createLNode(8);
  2785. head->next->next->next->next->next->next->next->previous = head->next->next->next->next->next->next;
  2786.  
  2787. head->next->next->next->next->next->next->next->next = createLNode(9);
  2788. head->next->next->next->next->next->next->next->next->previous = head->next->next->next->next->next->next->next;
  2789.  
  2790. node* root = doubleLinkedListToBSTUtil(&head, 9);
  2791. inOrder(root);
  2792. }
  2793.  
  2794. void doubleLinkedListToBSTInplace(){
  2795. lnode* head = createLNode(1);
  2796. head->previous = NULL;
  2797.  
  2798. head->next = createLNode(2);
  2799. head->next->previous = head;
  2800.  
  2801. head->next->next = createLNode(3);
  2802. head->next->next->previous = head->next;
  2803.  
  2804. head->next->next->next = createLNode(4);
  2805. head->next->next->next->previous = head->next->next;
  2806.  
  2807. head->next->next->next->next = createLNode(5);
  2808. head->next->next->next->next->previous = head->next->next->next;
  2809.  
  2810. head->next->next->next->next->next = createLNode(6);
  2811. head->next->next->next->next->next->previous = head->next->next->next->next;
  2812.  
  2813. head->next->next->next->next->next->next = createLNode(7);
  2814. head->next->next->next->next->next->next->previous = head->next->next->next->next->next;
  2815.  
  2816. head->next->next->next->next->next->next->next = createLNode(8);
  2817. head->next->next->next->next->next->next->next->previous = head->next->next->next->next->next->next;
  2818.  
  2819. head->next->next->next->next->next->next->next->next = createLNode(9);
  2820. head->next->next->next->next->next->next->next->next->previous = head->next->next->next->next->next->next->next;
  2821.  
  2822. lnode* iroot = doubleLinkedListToBSTInplaceUtil(&head, 9);
  2823. inOrderL(iroot);
  2824. }
  2825.  
  2826. void ceilInBST(){
  2827. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2828. node* rootm = NULL;
  2829. for(int i = 0; i < 12; i++){
  2830. rootm = insert(rootm, arr[i]);
  2831. }
  2832.  
  2833. node* ceil = ceilInBSTUtil(rootm, 43);
  2834. ceil ? cout<<"ceil is "<<ceil->data : cout<<"No Ceil";cout<<endl;
  2835. ceil = ceilInBSTUtil(rootm, 45);
  2836. ceil ? cout<<"ceil is "<<ceil->data : cout<<"No Ceil";cout<<endl;
  2837. ceil = ceilInBSTUtil(rootm, 77);
  2838. ceil ? cout<<"ceil is "<<ceil->data : cout<<"No Ceil";cout<<endl;
  2839. ceil = ceilInBSTUtil(rootm, 88);
  2840. ceil ? cout<<"ceil is "<<ceil->data : cout<<"No Ceil";
  2841. }
  2842.  
  2843. void floorInBST(){
  2844. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2845. node* rootm = NULL;
  2846. for(int i = 0; i < 12; i++){
  2847. rootm = insert(rootm, arr[i]);
  2848. }
  2849.  
  2850. node* floor = floorInBSTUtil(rootm, 43);
  2851. floor ? cout<<"floor is "<<floor->data : cout<<"No floor";cout<<endl;
  2852. floor = floorInBSTUtil(rootm, 45);
  2853. floor ? cout<<"floor is "<<floor->data : cout<<"No floor";cout<<endl;
  2854. floor = floorInBSTUtil(rootm, 77);
  2855. floor ? cout<<"floor is "<<floor->data : cout<<"No floor";cout<<endl;
  2856. floor = floorInBSTUtil(rootm, 88);
  2857. floor ? cout<<"floor is "<<floor->data : cout<<"No floor";
  2858. }
  2859.  
  2860. void btreeToDoubleLinkedList() {
  2861. int arr[12] = { 50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5 };
  2862. node* rootm = NULL;
  2863. for (int i = 0; i < 12; i++) {
  2864. rootm = insert(rootm, arr[i]);
  2865. }
  2866.  
  2867. rootm = btreeToDoubleLinkedListUtil(rootm);
  2868. while (rootm) {
  2869. if (rootm->left)
  2870. rootm = rootm->left;
  2871. else
  2872. break;
  2873. }
  2874.  
  2875. printTreeUsingNext(rootm);
  2876. }
  2877.  
  2878. void kthLargestElement() {
  2879. int arr[12] = { 50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5 };
  2880. node* rootm = NULL;
  2881. for (int i = 0; i < 12; i++) {
  2882. rootm = insert(rootm, arr[i]);
  2883. }
  2884.  
  2885. inOrder(rootm); cout << endl;
  2886. node* n = kthLargestElementUtil(rootm, 5);
  2887. n ? cout << n->data : cout << "No Larger element"; cout << endl;
  2888. n = kthLargestElementUtil(rootm, 1);
  2889. n ? cout << n->data : cout << "No Larger element"; cout << endl;
  2890. n = kthLargestElementUtil(rootm, 12);
  2891. n ? cout << n->data : cout << "No Larger element"; cout << endl;
  2892. n = kthLargestElementUtil(rootm, 11);
  2893. n ? cout << n->data : cout << "No Larger element"; cout << endl;
  2894.  
  2895. }
  2896.  
  2897. void kthSmallestElement() {
  2898. int arr[12] = { 50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5 };
  2899. node* rootm = NULL;
  2900. for (int i = 0; i < 12; i++) {
  2901. rootm = insert(rootm, arr[i]);
  2902. }
  2903. inOrder(rootm); cout << endl;
  2904. node* n = kthSmallestElementUtil(rootm, 5);
  2905. n ? cout << n->data : cout << "No Smaller element"; cout << endl;
  2906. n = kthSmallestElementUtil(rootm, 1);
  2907. n ? cout << n->data : cout << "No Smaller element"; cout << endl;
  2908. n = kthSmallestElementUtil(rootm, 12);
  2909. n ? cout << n->data : cout << "No Smaller element"; cout << endl;
  2910. n = kthSmallestElementUtil(rootm, 11);
  2911. n ? cout << n->data : cout << "No Smaller element"; cout << endl;
  2912. }
  2913.  
  2914. void recorrectSwappedNodes() {
  2915. int arr[12] = { 50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5 };
  2916. node* rootm = NULL;
  2917. for (int i = 0; i < 12; i++) {
  2918. rootm = insert(rootm, arr[i]);
  2919. }
  2920.  
  2921. rootm->left->data = 70;
  2922. rootm->right->data = 30;
  2923.  
  2924. cout << "Before correction" << endl; inOrder(rootm); cout << endl;
  2925. node* first = NULL, *middle = NULL, *last = NULL, *previous = NULL;
  2926. recorrectSwappedNodesUtil(rootm, &first, &middle, &last, &previous);
  2927.  
  2928. if (first && last) {
  2929. int temp = first->data;
  2930. first->data = last->data;
  2931. last->data = temp;
  2932. }
  2933. else {
  2934. int temp = first->data;
  2935. first->data = middle->data;
  2936. middle->data = temp;
  2937. }
  2938.  
  2939. cout << "After correction" << endl; inOrder(rootm); cout << endl;
  2940.  
  2941.  
  2942. rootm->left->data = 50;
  2943. rootm->data = 30;
  2944.  
  2945. cout << "Before correction" << endl; inOrder(rootm); cout << endl;
  2946. node *firsts = NULL, *middles = NULL, *lasts = NULL, *previouss = NULL;
  2947. recorrectSwappedNodesUtil(rootm, &firsts, &middles, &lasts, &previouss);
  2948.  
  2949. if (firsts && lasts) {
  2950. int temp = firsts->data;
  2951. firsts->data = lasts->data;
  2952. lasts->data = temp;
  2953. }
  2954. else {
  2955. int temp = firsts->data;
  2956. firsts->data = middles->data;
  2957. middles->data = temp;
  2958. }
  2959.  
  2960. cout << "After correction" << endl; inOrder(rootm); cout << endl;
  2961.  
  2962. rootm->left->left->data = 30;
  2963. rootm->left->data = 20;
  2964. cout << "Before correction" << endl; inOrder(rootm); cout << endl;
  2965. node *firstt = NULL, *middlet = NULL, *lastt = NULL, *previoust = NULL;
  2966. recorrectSwappedNodesUtil(rootm, &firstt, &middlet, &lastt, &previoust);
  2967.  
  2968. if (firstt && lastt) {
  2969. int temp = firstt->data;
  2970. firstt->data = lastt->data;
  2971. lastt->data = temp;
  2972. }
  2973. else {
  2974. int temp = firstt->data;
  2975. firstt->data = middlet->data;
  2976. middlet->data = temp;
  2977. }
  2978.  
  2979. cout << "After correction" << endl; inOrder(rootm);
  2980. }
  2981.  
  2982. void secondLargestNode(){
  2983. int arr[12] = { 50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5 };
  2984. node* rootm = NULL;
  2985. for (int i = 0; i < 12; i++) {
  2986. rootm = insert(rootm, arr[i]);
  2987. }
  2988.  
  2989. int count = 0;
  2990. node* sCount = NULL;
  2991. secondLargestNodeUtil(rootm, count, &sCount);
  2992. sCount ? cout<<"Second Largest Node:"<<sCount->data:cout<<"No Second Largest Node";
  2993. }
  2994.  
  2995. void deepestNodeInBTree(){
  2996. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  2997. node* rootm = NULL;
  2998. for(int i = 0; i < 12; i++){
  2999. rootm = insert(rootm, arr[i]);
  3000. }
  3001.  
  3002. int dLevel = 0;
  3003. node* dNode = NULL;
  3004. deepestNodeInBTreeUtil(rootm, 1, dLevel, &dNode);
  3005. dNode ? cout<<"Deepest node is :"<<dNode->data:cout<<"No Deepest Node found";
  3006. }
  3007.  
  3008. void deepestLeftNodeInBTree(){
  3009. int arr[11] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75};
  3010. node* rootm = NULL;
  3011. for(int i = 0; i < 11; i++){
  3012. rootm = insert(rootm, arr[i]);
  3013. }
  3014.  
  3015. int dLevel = 0;
  3016. node* dNode = NULL;
  3017. deepestLeftNodeInBTreeUtil(rootm, 1, dLevel, &dNode);
  3018. dNode ? cout<<"Deepest Left node is :"<<dNode->data:cout<<"No Deepest Left Node found";
  3019. }
  3020.  
  3021. void createLinkedListAtEachLevel(){
  3022. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  3023. node* rootm = NULL;
  3024. for(int i = 0; i < 12; i++){
  3025. rootm = insert(rootm, arr[i]);
  3026. }
  3027.  
  3028. createLinkedListAtEachLevelUtil(rootm);
  3029. }
  3030.  
  3031. void reverseAlternateLevelsOfBtree(){
  3032. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  3033. node* rootm = NULL;
  3034. for(int i = 0; i < 12; i++){
  3035. rootm = insert(rootm, arr[i]);
  3036. }
  3037.  
  3038. reverseAlternateLevelsOfBtreeUtil(rootm);
  3039. }
  3040.  
  3041. void maxSumBetweenAnyTwoLeaves(){
  3042. int arr[12] = {50, 30, 20, 40, 70, 60, 80, 10, 45, 55, 75, 5};
  3043. node* rootm = NULL;
  3044. for(int i = 0; i < 12; i++){
  3045. rootm = insert(rootm, arr[i]);
  3046. }
  3047.  
  3048. int maxSum = MINV;
  3049. maxSumBetweenAnyTwoLeavesUtil(rootm, maxSum);
  3050. cout<<"Max Sum is:"<<maxSum;
  3051. }
  3052.  
  3053. void updateNumberOfLeftAndRightChilds(){
  3054. node* rootm = createNode(50);
  3055. rootm->left = createNode(10);
  3056. rootm->left->left = createNode(5);
  3057. rootm->left->left->left = createNode(4);
  3058. rootm->left->left->left->left = createNode(3);
  3059. rootm->left->left->left->left->left = createNode(2);
  3060. rootm->left->left->left->left->left->left = createNode(1);
  3061. rootm->left->left->left->left->left->left->left = createNode(0);
  3062. rootm->left->right = createNode(20);
  3063. rootm->right = createNode(60);
  3064. rootm->right->left = createNode(55);
  3065. rootm->right->left->left = createNode(45);
  3066. rootm->right->left->left->left = createNode(35);
  3067. rootm->right->left->left->left->left = createNode(30);
  3068. rootm->right->left->left->left->left->left = createNode(10);
  3069. rootm->right->left->left->left->left->right = createNode(32);
  3070. rootm->right->left->left->left->right = createNode(40);
  3071. rootm->right->right = createNode(70);
  3072. rootm->right->right->left = createNode(65);
  3073. rootm->right->right->right = createNode(80);
  3074. rootm->right->right->right->right = createNode(90);
  3075. int nChilds = 0;
  3076. updateNumberOfLeftAndRightChildsUtil(rootm, nChilds);
  3077. }
  3078.  
  3079. void updateLeftMinAndRightMax(){
  3080. node* rootm = createNode(50);
  3081. rootm->left = createNode(10);
  3082. rootm->left->left = createNode(5);
  3083. rootm->left->left->left = createNode(4);
  3084. rootm->left->left->left->left = createNode(3);
  3085. rootm->left->left->left->left->left = createNode(2);
  3086. rootm->left->left->left->left->left->left = createNode(1);
  3087. rootm->left->left->left->left->left->left->left = createNode(0);
  3088. rootm->left->right = createNode(20);
  3089. rootm->right = createNode(60);
  3090. rootm->right->left = createNode(55);
  3091. rootm->right->left->left = createNode(45);
  3092. rootm->right->left->left->left = createNode(35);
  3093. rootm->right->left->left->left->left = createNode(30);
  3094. rootm->right->left->left->left->left->left = createNode(10);
  3095. rootm->right->left->left->left->left->right = createNode(32);
  3096. rootm->right->left->left->left->right = createNode(40);
  3097. rootm->right->right = createNode(70);
  3098. rootm->right->right->left = createNode(65);
  3099. rootm->right->right->right = createNode(80);
  3100. rootm->right->right->right->right = createNode(90);
  3101. updateLeftMinAndRightMaxUtil(rootm);
  3102. printLMinAndRMax(rootm);
  3103. }
  3104.  
  3105. void largestBSTTreeinBTree(){
  3106. node* rootm = createNode(50);
  3107. rootm->left = createNode(10);
  3108. rootm->left->left = createNode(5);
  3109. rootm->left->left->left = createNode(4);
  3110. rootm->left->left->left->left = createNode(3);
  3111. rootm->left->left->left->left->left = createNode(2);
  3112. rootm->left->left->left->left->left->left = createNode(1);
  3113. rootm->left->left->left->left->left->left->left = createNode(0);
  3114. rootm->left->right = createNode(20);
  3115. rootm->right = createNode(60);
  3116. rootm->right->left = createNode(55);
  3117. rootm->right->left->left = createNode(45);
  3118. rootm->right->left->left->left = createNode(35);
  3119. rootm->right->left->left->left->left = createNode(30);
  3120. rootm->right->left->left->left->left->left = createNode(10);
  3121. rootm->right->left->left->left->left->right = createNode(32);
  3122. rootm->right->left->left->left->right = createNode(40);
  3123. rootm->right->right = createNode(70);
  3124. rootm->right->right->left = createNode(65);
  3125. rootm->right->right->right = createNode(80);
  3126. rootm->right->right->right->right = createNode(90);
  3127. /* 50
  3128. / \
  3129. 10 60
  3130. / \ / \
  3131. 5 20 55 70
  3132. / / / \
  3133. 4 45 65 80
  3134. / / \
  3135. 3 35 90
  3136. / / \
  3137. 2 30 40
  3138. / / \
  3139. 1 10 32
  3140. /
  3141. 0 */
  3142.  
  3143. int maxChilds = MINV;
  3144. node* maxNode = NULL;
  3145. int count = 0;
  3146. largestBSTTreeinBTreeUtil(rootm, maxChilds, &maxNode, count);
  3147. inOrder(maxNode);
  3148. }
  3149.  
  3150. void heapSort(){
  3151. int arr[] = {12, 11, 13, 5, 6, 7};
  3152. int n = sizeof(arr)/sizeof(arr[0]);
  3153.  
  3154. cout<<"Increasing: ";heapSortIncUtil(arr, n);
  3155. cout<<"Decreasing: ";heapSortDecUtil(arr, n);
  3156. }
  3157.  
  3158. void mergeKSortedArrays(){
  3159. int arr[4][4] ={{1, 5, 8, 9},
  3160. {2, 3, 7, 10},
  3161. {4, 6, 11, 15},
  3162. {2, 4, 6, 9}};
  3163.  
  3164. mergeKSortedArraysUtil(arr, 4, 4);
  3165. }
  3166.  
  3167. void avlInsertion(){
  3168. node* rootm = NULL;
  3169. for(int i = 0; i < 20; i++)
  3170. rootm = avlInsertionUtil(rootm, i);
  3171.  
  3172. BFSOfTreeUtil(rootm);
  3173. }
  3174.  
  3175. void avlDeletion(){
  3176. node* rootm = NULL;
  3177. for(int i = 0; i <= 20; i++)
  3178. rootm = avlInsertionUtil(rootm, i);
  3179.  
  3180. BFSOfTreeUtil(rootm);
  3181. cout<<endl;
  3182.  
  3183. for(int i = 3; i >= 0; i--){
  3184. rootm = avlDeletionUtil(rootm, i);
  3185. BFSOfTreeUtil(rootm);
  3186. printIfTreeBalancedOrNotUtilOptimized(rootm);
  3187. rootm->isBalanced ? cout<<"Tree is Balanced" : cout<<"Tree is not Balanced";cout<<endl;
  3188. cout<<endl;
  3189. }
  3190. }
  3191.  
  3192. // Need to Implement
  3193. // 1.Con­struct a binary tree from given Inorder and Level Order Traversal.
  3194. // 2.Con­struct Binary Search Tree from a given Pre­order Tra­ver­sal Using Stack (With­out Recur­sion)
  3195. // 3.Red-Black Trees.
  3196. // 4.B-Trees.
  3197. // 5.Segment Trees.
  3198. // 6.Threaded Trees.
  3199.  
  3200. int main(){
  3201. cout<<"\n\n1.Insert in to BST \n";insertInToBST();
  3202. cout<<"\n\n2.Insert in to BST by ITR \n";insertInToBSTByItr();
  3203. cout<<"\n\n3.PreOrder BST \n";preOrderBST();
  3204. cout<<"\n\n4.PostOrder BST \n";postOrderBST();
  3205. cout<<"\n\n5.InOrder BST \n";inOrderBST();
  3206. cout<<"\n\n6.PreOrder BST without Recurr\n";preOrderBSTWithoutRecurr();
  3207. cout<<"\n\n7.PostOrder BST without Recurr\n";postOrderBSTWithoutRecurr();
  3208. cout<<"\n\n8.InOrder BST without Recurr\n";inOrderBSTWithoutRecurr();
  3209. cout<<"\n\n9.Greater Sum BST \n";greaterSumTree();
  3210. cout<<"\n\n10.Sum of all left leaves in BST \n";sumOfAllLeftLeavesBST();
  3211. cout<<"\n\n11.Convert Binary Tree to its Sum tree \n";convertBinaryTreeToSumTree();
  3212. cout<<"\n\n12.delete in to BST \n";deleteInBST();
  3213. cout<<"\n\n13.find in to BST \n";findInBST();
  3214. cout<<"\n\n14.size of BST \n";sizeOfTree();
  3215. cout<<"\n\n15.BFS of BST \n";BFSOfTree();
  3216. cout<<"\n\n16.Diameter of BST \n";diameterOfTree();
  3217. cout<<"\n\n17.Max element in BinaryTree \n";maxElementOfTree();
  3218. cout<<"\n\n18.Mirror Tree \n";mirrorTree();
  3219. cout<<"\n\n19.Create Mirror Tree \n";createMirrorTree();
  3220. cout<<"\n\n20.Print All paths from root to leaf whose Sum is X \n";printAllPathsFromRootToLeaf();
  3221. cout<<"\n\n21.Reverse Level Order Traversal \n";reverseLevelOrderTraversal();
  3222. cout<<"\n\n22.Find Height of BST without Recurrsion\n";findHeightWithoutRecurrsion();
  3223. cout<<"\n\n23.Print All Full Node in BTree\n";printAllFullNodesInBTree();
  3224. cout<<"\n\n24.Print Bottom View of BST\n";printBottomViewOfBST();
  3225. cout<<"\n\n25.Construct BST from PreOrder Traversal using Recursion\n";constructBSTFromPreOrder();
  3226. cout<<"\n\n26.Print Top View of a Tree\n";printTopViewOfBST();
  3227. cout<<"\n\n27.Inorder Successor & Predecessor Tree\n";printInorderSuccessorAndPredecessor();
  3228. cout<<"\n\n28.Height of Tree\n";printHeightOfTree();
  3229. cout<<"\n\n29.Get Height of a node in a tree\n";printHeightOfNodeInTree();
  3230. cout<<"\n\n30.Find whether tree is balanced or not\n";printIfTreeBalancedOrNot();
  3231. cout<<"\n\n31.Sorted Array to BST of MinimalHeight\n";sortedArrayToBST();
  3232. cout<<"\n\n32.Path from root to the node\n";pathFromRootToNode();
  3233. cout<<"\n\n33.Size of Tree\n";sizeOfTree2();
  3234. cout<<"\n\n34.Is BST ?\n";isBST();
  3235. cout<<"\n\n34.Max Width In Tree\n";findMaxWidthOfTree();
  3236. cout<<"\n\n35.Print All Node Between Two Levels\n";printNodesBetweenTwoLevels();
  3237. cout<<"\n\n36.Is BST equal ?\n";isBSTEqual();
  3238. cout<<"\n\n37.Printing RightView\n";printingRightView();
  3239. cout<<"\n\n38.Printing LeftView\n";printingLeftView();
  3240. cout<<"\n\n39.LCA of BST\n";LCABST();
  3241. cout<<"\n\n40.LCA of BTree\n";LCABTree();
  3242. cout<<"\n\n41.Vertical Sum of BST\n";verticalSumOfBST();
  3243. cout<<"\n\n42.Print Tree in Vertical Order Path\n";printTreeInVerticalOrderPath();
  3244. cout<<"\n\n43.Print Nodes That Dont have Siblings\n";printNodesThatDontHaveSibilings();
  3245. cout<<"\n\n44.Find Distance Of Node From Root\n";distanceOfNodeFromRoot();
  3246. cout<<"\n\n45.Print All Nodes at Distance d from root\n";printAllNodesAtDistanceDFromRoot();
  3247. cout<<"\n\n46.Distance between two nodes\n";distanceBetweenTwoNodes();
  3248. cout<<"\n\n47.Maximum Sum From root to all leaf's\n";maxSumFromRootToAllLeaves();
  3249. cout<<"\n\n48.Print All Nodes at Distance d from leaf\n";printAllNodesAtDistanceDFromLeaf();
  3250. cout<<"\n\n49.Print BTree in zig-zag fashion\n";printBTreeInZigZag();
  3251. cout<<"\n\n50.Print If Tree is SubTree of Tree\n";printTreeIsSubTreeOfATree();
  3252. cout<<"\n\n51.Construct Binary Tree from inorder and postorder\n";bTreeFromInOrderPostOrder();
  3253. cout<<"\n\n52.Construct Binary Tree from inorder and preorder\n";bTreeFromInOrderPreOrder();
  3254. cout<<"\n\n53.Are two nodes sibilings to each other ?\n";isSibilings();
  3255. cout<<"\n\n54.Are two nodes cousins to each other ?\n";isCousins();
  3256. cout<<"\n\n55.Print next Sibilings of a node\n";printSibilingsOfNode();
  3257. cout<<"\n\n56.Inorder without parent link\n";inOrderWithoutParent();
  3258. cout<<"\n\n57.Convert Linked List to BST\n";linkedListToBST();
  3259. cout<<"\n\n58.Convert Double Linked List to BST\n";doubleLinkedListToBST();
  3260. cout<<"\n\n59.Convert Double Linked List to BST InPlace\n";doubleLinkedListToBSTInplace();
  3261. cout<< "\n\n60.Convert BTree to Double Linked LIst\n"; btreeToDoubleLinkedList();
  3262. cout<< "\n\n61.Ceil Of BST\n"; ceilInBST();
  3263. cout<< "\n\n62.Floor Of BST\n"; floorInBST();
  3264. cout<< "\n\n63.Kth Largest Element of BST\n"; kthLargestElement();
  3265. cout<< "\n\n64.Kth Smallest Element of BST\n"; kthSmallestElement();
  3266. cout<< "\n\n65.Recorret Swapped Nodes\n"; recorrectSwappedNodes();
  3267. cout<< "\n\n66.Second Largest Node\n"; secondLargestNode();
  3268. cout<<"\n\n67.Deepest Node in BTree\n";deepestNodeInBTree();
  3269. cout<<"\n\n68.Deepest Left Node in BTree\n";deepestLeftNodeInBTree();
  3270. cout<<"\n\n69.Create Linked Lists of all node level\n";createLinkedListAtEachLevel();
  3271. cout<<"\n\n70.Reverse Alternate levels of a BTree\n";reverseAlternateLevelsOfBtree();
  3272. cout<<"\n\n71.Max Sum Between Any Two Leaves\n";maxSumBetweenAnyTwoLeaves();
  3273. cout<<"\n\n72.Update Number of Left And Right Childs in a Tree\n";updateNumberOfLeftAndRightChilds();
  3274. cout<<"\n\n73.Update Left Min and Right Max of every node\n";updateLeftMinAndRightMax();
  3275. cout<<"\n\n74.Largest BST tree in Binary Tree\n";largestBSTTreeinBTree();
  3276. cout<<"\n\n75.HeapSort\n";heapSort();
  3277. cout<<"\n\n76.Merge K-Sorted Arrays\n";mergeKSortedArrays();
  3278. cout<<"\n\n77.AVL insertion\n";avlInsertion();
  3279. cout<<"\n\n78.AVL deletion\n";avlDeletion();
  3280. cout<<endl;
  3281. }
Add Comment
Please, Sign In to add comment