Please help me about Binary Tree

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!

Comments

  • : 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!
    :

    You have to start from the top to the tree (or root) and search the tree for the node. When searching, hold pointer to previous (parent) node in some temporary. When you've found the right node, return temporary pointer, which is pointing to parent.
  • [b][red]This message was edited by cptb at 2003-11-1 14:47:21[/red][/b][hr]
    : : 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!
    : :
    :
    : You have to start from the top to the tree (or root) and search the tree for the node. When searching, hold pointer to previous (parent) node in some temporary. When you've found the right node, return temporary pointer, which is pointing to parent.
    : Thanks for your reply, but the problem is when I use recursion, how can I trace the previous node? Another problem is when I do an InOrder, PostOrder or PreOrder Traversal, the prarent node appears with no any particular order. I tried to Push all node into a Stack and study the order but I finally failed.



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories