Guest User

Untitled

a guest
Jul 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. struct _node {
  2. my_type data;
  3. struct _node *left;
  4. struct _node *right;
  5. }
  6.  
  7. q = queue_create ();
  8. queue_insert (q, head);
  9. temp = head;
  10. while (!empty (q))
  11. {
  12. temp = queue_remove (q);
  13. if (
  14. (temp->left == my_random_node_1) && (head->right == my_random_node_2) ||
  15. (temp->left == my_random_node_2) && (head->right == my_random_node_1)
  16. )
  17. {
  18. /* temp is the common parent of the two target notes */
  19. /* Do stuffs you need to do */
  20. }
  21.  
  22. /* Enqueue the childs, so that in successive iterations we can
  23. * check them, by taking out from the queue
  24. */
  25. push (q, temp->left);
  26. push (q, temp->right);
  27. }
  28.  
  29. int postorder (node *p, int r1, int r2)
  30. {
  31. int x = 0; /* The state variable */
  32. if (p->data == TERMINAL_VAL)
  33. return x;
  34.  
  35. /* 0x01 | 0x02 = 0x03 threfore
  36. * state one is when x = 0x01 or x = 0x02
  37. * state two is when x = 0x03
  38. */
  39. if (p->data == r1)
  40. x |= 0x01;
  41. else if (p->data == r2)
  42. x |= 0x02;
  43.  
  44. /* if we have x in state two, no need to search more
  45. */
  46. if (x != 0x03)
  47. x |= postorder (p->left, r1, r2);
  48. if (x != 0x03)
  49. x |= postorder (p->right, r1, r2);
  50.  
  51. /* In this node we are in state two, print node if this node
  52. * is not any of the two nodes r1 and r2. This makes sure that
  53. * is one random node is an ancestor of another random node
  54. * then it will not be printed instead its parent will be printed
  55. */
  56. if ((x == 0x03) && (p->data != r1) && (p->data != r2))
  57. {
  58. printf ("[%c] ", p->data);
  59. /* set state variable to 0 if we do not want to print
  60. * the ancestors of the first ancestor
  61. */
  62. x = 0;
  63. }
  64.  
  65. /* return state variable to parent
  66. */
  67. return x;
  68. }
  69.  
  70. commonAncestor tree a b:
  71. value := <value of node 'tree'>
  72. if (a < value) && (b < value)
  73. then commonAncestor (left tree) a b
  74. else if (a > value) && (b > value)
  75. then commonAncestor (right tree) a b
  76. else tree
  77.  
  78. 8
  79. 10 12
  80. 7
  81.  
  82. find(root, d1, d2, n1=null, n2=null)
  83. {
  84. if(n1 && n2) return;
  85. if(!root) return;
  86.  
  87. else if(root -> d == d1 ) n1 = root;
  88. else if(root -> d == d2 ) n2 = root;
  89. find(root->left, d1, d2, n1, n2);
  90. find(root->right, d1, d2, n1, n2);
  91. }
  92.  
  93. LCA(root, d1, d2)
  94. {
  95. node *n1=null, *n2=null;
  96. find(root, d1, d2, n1, n2);
  97. if(n1 == null || n2 == null )error 'nodes not present' exit(0);
  98. findIntersect(n1, n2);
  99. }
  100. findInterSect(node *n1, node *n2)
  101. {
  102. l1 = length(n1);
  103. l2 = length(n2);
  104. node *g = n2, *l = n1;
  105. diff = abs(l1 - l2);
  106. if(l1>l2) g = n1 l =n2
  107. while(diff) g = g->parent; diff--;
  108. // now both nodes are at same level
  109. while(g != l) g= g->parent, l = l->parent;
  110. }
  111.  
  112. node *FindCommonAncestor(node *root, node *node1, node *node2) {
  113. node *current = node1;
  114. node_list temp_list;
  115. temp_list.add(current);
  116. while (current != root) {
  117. current = current.parent;
  118. temp_list.add(current);
  119. }
  120. current = node2;
  121. while (current not in temp_list) {
  122. current = current.parent;
  123. }
  124. return current;
  125. }
  126.  
  127. <p>step 1:Pre order traversal unless any 1 of the node is met and save the nodes visited uptil now.<br>
  128. step 2:Inorder traversal,start saving the nodes when any 1 (of the two provided nodes) node is met,and save the list until the next node is met.<br>
  129. Step 3: post order traversal,start saving the nodes when both the nodes hav been visited....</p>
  130. <pre> A </pre>
  131. <pre> B C </pre>
  132. <pre> D E F G </pre>
  133. <pre>H I J K L M N O </pre>
  134. <p> Suppose H and E are two random nodes..<br> 1. ABDH <br>2. HDIBJE<br>3. EBLMENOGCA<br>Find the first node common in all three.... Your answer!!!
Add Comment
Please, Sign In to add comment