struct Tree
{
int info;
Tree * left,* right;
Tree(int val, Tree * lbranch = 0, Tree * rbranch = 0);
};
Tree::Tree(int val, Tree * lbranch, Tree * rbranch)
{
info = val;
left = lbranch;
right = rbranch;
}
The following function returns the height of a tree.
int TreeHeight(Tree * t)
// postcondition: returns height of tree with root t
{
if (t == 0)
{
return 0;
}
else
{
return 1 + Max(TreeHeight(t->left),TreeHeight(t->right));
}
}
where the function Maxreturns the largest of its two integer parameters.
Problem 2
Write a function Tree2List that converts a binary search tree into a sorted linked list. You may find it useful to write auxiliary function(s) called by Tree2List .
Node * Tree2List(Tree * t)
// precondition: t is the root of a binary search tree
// postcondition: returns a pointer to the first node of a
// sorted linked list containing same values as t