Reputation: 91
I want to create AVL tree with following structure
struct avl
{
int a;
int b;
struct avl* left;
struct avl* right;
int height;
}
I created my AVL tree according to "a" value, I mean it is sorted according to this value. However, I want to find minimum of "b" value. If I want to find minimum of "a" value, I could use recursive function by going to left, but "b" value is independent from AVL tree.
How can I find the minimum of this "b" value in AVL tree?
I thought to use recursive function, but if I found minimum value of "b", I would lose this value because of recursion.
Upvotes: 1
Views: 57
Reputation: 91
I used stack implementation to travel all nodes, or there is Morris Traversal algorithm without using stack or recursive. This is the basic solution of my question. By using stack implementation, you can find any min value, and it is not confusing like recursive
Upvotes: 0
Reputation: 18420
For recursive functions, you have to think about a base case and a recursion case.
Base case: Empty tree (NULL pointer). The empty tree does not have a minimum value, but you could use INT_MAX for it.
Recursive case: Find the minimum of the left subtree and the right subtree. Compare both with your current b value and return the minimum of the three.
It should be easy to implement this algorithm.
Upvotes: 1