1

I have to write a non recursive program to print the ancestor (parent of parent) of a given node in a Binary Tree. What logic should I use ?

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • 1
    I tried to first think of doing the above for a BST. Then I tried to think if in any way I could extend the logic for a binary tree. But that logic does not hold for a non-recursive routine. Logic for a BST : in an infinite loop, keep updating the root pointer until you have verified that that root pointer is the ancestor of the node (or return null if no such node exists in the BST). – Nikunj Banka Feb 16 '13 at 07:22
  • Or just modify the BST so that child node holds a reference to the parent node... – nhahtdh Feb 16 '13 at 07:27
  • @nhahtdh I find your suggestion a little too vague to be of any use. Do you have any link that could better explain your point ? – Nikunj Banka Feb 17 '13 at 16:14
  • @NikunjBanka: That's why it is a comment, not an answer. The idea is that: normally, the node only holds ref to 2 child nodes, but we add one more ref to the parent node so that we can traverse backward (when we are given a reference to the node). – nhahtdh Feb 17 '13 at 16:48

3 Answers3

2

Use any of the iterative implementations listed here and stop when you reach the grandparent node (node.Left.Left = desired OR node.Left.Right = desired OR node.Right.Left = desired OR node.Right.Right = desired). You'll obviously first need to test that a candidate node does indeed have grandchildren.

Ani
  • 111,048
  • 26
  • 262
  • 307
2

Use a non recursive sub routine to traverse the binary tree( refer to http://en.wikipedia.org/wiki/Tree_traversal#Implementations) and maintain a stack to store the all parent nodes in that array and whenever you pop from stack suitably pop the element from the stack. Finally when you find the element, second topmost element on the stack would be the ancestor.

1

You can just do a standard tree walk and remember the last two steps, like a limited stack. The below fragment uses an array[2] of pointers to remember the last two steps (Note: "opa" is Dutch for "granddad"):

#include <stdio.h>
#include <stdlib.h>

struct tree_node {
    struct tree_node * left;
    struct tree_node * right;
    int data;
};

        /* a bonsai-tree for testing */
struct tree_node nodes[10] =
/* 0 */ {{ nodes+1, nodes+2, 10}
/* 1 */ ,{ nodes+3, nodes+4, 5}
/* 2 */ ,{ nodes+5, nodes+6, 17}
/* 3 */ ,{ nodes+7, nodes+8, 3}
/* 4 */ ,{ nodes+9, NULL, 7}
/* 5 */ ,{ NULL, NULL, 14}
/* 6 */ ,{ NULL, NULL, 18}
/* 7 */ ,{ NULL, NULL, 1}
/* 8 */ ,{ NULL, NULL, 4}
        };

struct tree_node * find_opa(struct tree_node *ptr, int val)
{
struct tree_node *array[2] = {NULL,NULL};
unsigned step=0;

for (step=0; ptr; step++) {
        if (ptr->data == val) break;
        array[ step % 2] = ptr;
        ptr = (val < ptr->data) ? ptr->left : ptr->right;
        }

return ptr ? array[step %2] : NULL;
}

void show(struct tree_node *ptr, int indent)
{
if (!ptr) { printf("Null\n"); return; }

printf("Node(%d):\n", ptr->data);
printf("%*c=", indent, 'L');  show (ptr->left, indent+2);
printf("%*c=", indent, 'R');  show (ptr->right, indent+2);
}

int main(void)
{
struct tree_node *root, *opa;

root = nodes;
show (root, 0);

opa = find_opa(root, 4);
printf("Opa(4)=:\n" );
show (opa, 0);
return 0;
}
wildplasser
  • 43,142
  • 8
  • 66
  • 109
  • what is a bonsai tree ? Is it a specific type of tree ? – Nikunj Banka Feb 17 '13 at 15:54
  • It is a tiny tree that exists only because of its beauty ;-) – wildplasser Feb 17 '13 at 16:07
  • Additionally, your code works only for binary search trees. Do you have code that is well suited for general trees(the question demands a general implementation for a binary tree) ? – Nikunj Banka Feb 17 '13 at 16:12
  • No, the whole idea is that if you can find the path to a node, you can also find the way to its parent's parent. In the simplest case you would just add a parent pointer to each node. In my example, you find your way from the root to the node and remember the path taken. In both cases, finding the great-parent ~= finding the node. – wildplasser Feb 17 '13 at 16:21