0

Hello!

I need some help with writing a binary tree class. There are two methods that I have no idea how to approach:
1) Finding the left-most leaf.
Example:
...............A............
............../.\...........
.............B...C...........
............/..../\..........
...........D....E..F........
.............../..............
..............G..............
............./..............
............H..............
.........../..............
..........I..............

In this case, "I" would be the left-most leaf. Now, I have to write a method that returns a pointer to it.
My guess is that it would involve recursion in some way (basically, going down all the possible paths) and a static variable that would increase by 1 if it goes down a left path and decrease by one if it goes down a right path. Therefore, the path on which the variable gets the largest value is the path to the left leaf. But I dunno how to right this down in code. Besides, I dunno if it'll help in some way to return the pointer to the left-most leaf.
2) Deleting the left-most leaf. This, I have no idea how to do at all.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by GeorgeObl
0

Please post your binary tree implementation so we could discuss more on searching based on the code.

0

Well, all I have is this:

template <class T>
class Node
{
T data;
Node *left, *right;

Node (T i, Node<T> *l, Node<T> *r)
{
data = i;
left = l;
right = r;
}

friend class BinaryTree<T>;
};

template <class T>
class BinaryTree
{
Node *root;
public:
//destructor and default constructors will go here
};
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.