I am struggling in writing a method to return the parent node of a given Tree node. The class of the Tree is following:

class BinaryTree {
private:
int data;
BinaryTree *left;
BinaryTree *right;
public:
BinaryTree(int i, BinaryTree *L, BinaryTree *R) {
data=i; left=L; right=R;
}
~BinaryTree() {}
int RootData() { return data; }
BinaryTree* Left() { return left; }
BinaryTree* Right() { return right;}
};

The problem is that I need to write a member function which takes a Tree Node argument and returns the parent node of that node.

Thanks very much!

• [red]
Hi!!

It is not possible to get the parent of a node in the current form of the binary tree, since the structure does not store the parent.

Regards
Shaunak
[/red]

• the only way i can think of would be to start at the root of the entire tree, and traverse the tree until current.left or current.right is equal to the node you're looking for, then just return current. just a thought, good luck!

: [red]
: Hi!!
:
: It is not possible to get the parent of a node in the current form of the binary tree, since the structure does not store the parent.
:
: Regards
: Shaunak
: [/red]
:
:

• : I am struggling in writing a method to return the parent node of a given Tree node. The class of the Tree is following:
:
: class BinaryTree {
: private:
: int data;
: BinaryTree *left;
: BinaryTree *right;
: public:
: BinaryTree(int i, BinaryTree *L, BinaryTree *R) {
: data=i; left=L; right=R;
: }
: ~BinaryTree() {}
: int RootData() { return data; }
: BinaryTree* Left() { return left; }
: BinaryTree* Right() { return right;}
: };
:
: The problem is that I need to write a member function which takes a Tree Node argument and returns the parent node of that node.
:
: Thanks very much!
:

Declare another pointer "prev" and traverse the tree. save every previouse pointer to it. i think it will work
Thank you