Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct _node {
- my_type data;
- struct _node *left;
- struct _node *right;
- }
- q = queue_create ();
- queue_insert (q, head);
- temp = head;
- while (!empty (q))
- {
- temp = queue_remove (q);
- if (
- (temp->left == my_random_node_1) && (head->right == my_random_node_2) ||
- (temp->left == my_random_node_2) && (head->right == my_random_node_1)
- )
- {
- /* temp is the common parent of the two target notes */
- /* Do stuffs you need to do */
- }
- /* Enqueue the childs, so that in successive iterations we can
- * check them, by taking out from the queue
- */
- push (q, temp->left);
- push (q, temp->right);
- }
- int postorder (node *p, int r1, int r2)
- {
- int x = 0; /* The state variable */
- if (p->data == TERMINAL_VAL)
- return x;
- /* 0x01 | 0x02 = 0x03 threfore
- * state one is when x = 0x01 or x = 0x02
- * state two is when x = 0x03
- */
- if (p->data == r1)
- x |= 0x01;
- else if (p->data == r2)
- x |= 0x02;
- /* if we have x in state two, no need to search more
- */
- if (x != 0x03)
- x |= postorder (p->left, r1, r2);
- if (x != 0x03)
- x |= postorder (p->right, r1, r2);
- /* In this node we are in state two, print node if this node
- * is not any of the two nodes r1 and r2. This makes sure that
- * is one random node is an ancestor of another random node
- * then it will not be printed instead its parent will be printed
- */
- if ((x == 0x03) && (p->data != r1) && (p->data != r2))
- {
- printf ("[%c] ", p->data);
- /* set state variable to 0 if we do not want to print
- * the ancestors of the first ancestor
- */
- x = 0;
- }
- /* return state variable to parent
- */
- return x;
- }
- commonAncestor tree a b:
- value := <value of node 'tree'>
- if (a < value) && (b < value)
- then commonAncestor (left tree) a b
- else if (a > value) && (b > value)
- then commonAncestor (right tree) a b
- else tree
- 8
- 10 12
- 7
- find(root, d1, d2, n1=null, n2=null)
- {
- if(n1 && n2) return;
- if(!root) return;
- else if(root -> d == d1 ) n1 = root;
- else if(root -> d == d2 ) n2 = root;
- find(root->left, d1, d2, n1, n2);
- find(root->right, d1, d2, n1, n2);
- }
- LCA(root, d1, d2)
- {
- node *n1=null, *n2=null;
- find(root, d1, d2, n1, n2);
- if(n1 == null || n2 == null )error 'nodes not present' exit(0);
- findIntersect(n1, n2);
- }
- findInterSect(node *n1, node *n2)
- {
- l1 = length(n1);
- l2 = length(n2);
- node *g = n2, *l = n1;
- diff = abs(l1 - l2);
- if(l1>l2) g = n1 l =n2
- while(diff) g = g->parent; diff--;
- // now both nodes are at same level
- while(g != l) g= g->parent, l = l->parent;
- }
- node *FindCommonAncestor(node *root, node *node1, node *node2) {
- node *current = node1;
- node_list temp_list;
- temp_list.add(current);
- while (current != root) {
- current = current.parent;
- temp_list.add(current);
- }
- current = node2;
- while (current not in temp_list) {
- current = current.parent;
- }
- return current;
- }
- <p>step 1:Pre order traversal unless any 1 of the node is met and save the nodes visited uptil now.<br>
- 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>
- Step 3: post order traversal,start saving the nodes when both the nodes hav been visited....</p>
- <pre> A </pre>
- <pre> B C </pre>
- <pre> D E F G </pre>
- <pre>H I J K L M N O </pre>
- <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