Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1.  
  2. /* C++ program to construct tree using inorder
  3. and preorder traversals */
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. /* A binary tree node has data, pointer to left child
  8. and a pointer to right child */
  9. struct Node {
  10. char data;
  11. struct Node* left;
  12. struct Node* right;
  13. };
  14.  
  15. struct Node* newNode(char data)
  16. {
  17. struct Node* node = new Node;
  18. node->data = data;
  19. node->left = node->right = NULL;
  20. return (node);
  21. }
  22.  
  23. /* Recursive function to construct binary of size
  24. len from Inorder traversal in[] and Preorder traversal
  25. pre[]. Initial values of inStrt and inEnd should be
  26. 0 and len -1. The function doesn't do any error
  27. checking for cases where inorder and preorder
  28. do not form a tree */
  29. struct Node* buildTree(char in[], char pre[], int inStrt,
  30. int inEnd, unordered_map<char, int>& mp)
  31. {
  32. static int preIndex = 0;
  33.  
  34. if (inStrt > inEnd)
  35. return NULL;
  36.  
  37. /* Pick current node from Preorder traversal using preIndex
  38. and increment preIndex */
  39. int curr = pre[preIndex++];
  40. struct Node* tNode = newNode(curr);
  41.  
  42. /* If this node has no children then return */
  43. if (inStrt == inEnd)
  44. return tNode;
  45.  
  46. /* Else find the index of this node in Inorder traversal */
  47. int inIndex = mp[curr];
  48.  
  49. /* Using index in Inorder traversal, construct left and
  50. right subtress */
  51. tNode->left = buildTree(in, pre, inStrt, inIndex - 1, mp);
  52. tNode->right = buildTree(in, pre, inIndex + 1, inEnd, mp);
  53.  
  54. return tNode;
  55. }
  56.  
  57. // This function mainly creates an unordered_map, then
  58. // calls buildTree()
  59. struct Node* buldTreeWrap(char in[], char pre[], int len)
  60. {
  61. // Store indexes of all items so that we
  62. // we can quickly find later
  63. unordered_map<char, int> mp;
  64. for (int i = 0; i < len; i++)
  65. mp[in[i]] = i;
  66.  
  67. return buildTree(in, pre, 0, len - 1, mp);
  68. }
  69.  
  70. /* This funtcion is here just to test buildTree() */
  71. void printInorder(struct Node* node)
  72. {
  73. if (node == NULL)
  74. return;
  75. printInorder(node->left);
  76. printf("%c ", node->data);
  77. printInorder(node->right);
  78. }
  79.  
  80. /* Driver program to test above functions */
  81. int main()
  82. {
  83. char in[] = { 'D', 'B', 'E', 'A', 'F', 'C' };
  84. char pre[] = { 'A', 'B', 'D', 'E', 'C', 'F' };
  85. int len = sizeof(in) / sizeof(in[0]);
  86.  
  87. struct Node* root = buldTreeWrap(in, pre, len);
  88.  
  89. /* Let us test the built tree by printing
  90. Insorder traversal */
  91. printf("Inorder traversal of the constructed tree is \n");
  92. printInorder(root);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement