I got a code of a BST and i need to create a function that will return the value of the parent that it's son/sons sum to the biggest sum - BST and not AVL.
i assume that the shortest code will be recursive but i didn't manage to get it right - i'll send the code without my function because it's wrong - like to get some help...
Thank you
#include <vector>
#include <iostream>
using namespace std;
//-----------------------------------------------------------------
// class Node
// a single Node from a binary tree
//-----------------------------------------------------------------
template <class T> class Node
{
public:
Node<T> * left;
Node<T> * right;
T value;
Node (T value);
Node (T value, Node<T> * left, Node<T> * right);
};
// class Node implementation
template <class T> Node<T>::Node(T val)
: value(val), left(0), right(0)
{
// no further initialization
}
template <class T>
Node<T>::Node(T val, Node<T> * l, Node<T> * r)
: value(val), left(l), right(r)
{
// no further initialization
}
//-----------------------------------------------------------------
// class Tree Binary Trees
// can process all nodes in Pre/In/Post order
//-----------------------------------------------------------------
template <class T> class Tree
{
//protected:
// Node<T> * root;
public:
Node<T> * root;
Tree();
~Tree();
int IsEmpty() const;
void Clear() { Clear(root); root=NULL;}
void PreOrder() { PreOrder(root); }
void InOrder() { InOrder(root); }
void PostOrder() { PostOrder(root); }
virtual void Process(T val) {cout<<val<<" ";}
private:
void Clear(Node<T> * current);
void PreOrder(Node<T> * current);
void InOrder(Node<T> * current);
void PostOrder(Node<T> * current);
};
// class Tree implementation
template <class T> Tree<T>::Tree() // initialize tree
{
root = 0;
}
template <class T> Tree<T>::~Tree() // deallocate tree
{
if (root != 0)
Clear(root);
}
template <class T> int Tree<T>::IsEmpty() const
{
return root==0;
}
template <class T>
void Tree<T>::Clear(Node<T> * current)
{
if(current)
{
// Release memory associated with children
if (current->left)
Clear(current->left);
if (current->right)
Clear(current->right);
delete current;
}
}
// PreOrder Processing of tree rooted at current
template <class T>
void Tree<T>::PreOrder(Node<T> * current)
{
// visit Node, left child, right child
if (current)
{
// Process current Node
Process(current->value);
// then visit children
PreOrder(current->left);
PreOrder(current->right);
}
}
// InOrder Processing of tree rooted at current
template <class T>
void Tree<T>::InOrder(Node<T> * current)
{
// visit left child, Node, right child
if (current)
{
InOrder(current->left);
Process(current->value);
InOrder(current->right);
}
}
// PostOrder Processing of tree rooted at current
template <class T>
void Tree<T>::PostOrder(Node<T> * current)
{
// visit left child, right child, node
if (current)
{
PostOrder(current->left);
PostOrder(current->right);
Process(current->value);
}
}
template <class T> class SearchTree : public Tree<T>
{
public:
// protocol for search trees
void Add(T value);
int Includes(T value) const {return Includes(root,value);}
void Remove(T value);
private:
void Add(Node<T> * current, T val);
int Includes(Node<T> * current, T val) const;
// method used internally to delete top Node
Node<T> * RemoveTop(Node<T> *){};
};
template <class T>
void SearchTree<T>::Add(T val)
{
// Add value to binary search tree
if (!root)
{
root = new Node<T>(val);
return;
}
Add(root,val);
}
template <class T>
void SearchTree<T>::Add(Node<T> * current,T val)
{
if (current->value < val)
// Add to right subtree
if (!current->right)
{
current->right=new Node<T>(val);
return;
}
else Add(current->right,val);
else
// Add to left subtree
if (!current->left)
{
current->left=new Node<T>(val);
return;
}
else Add(current->left,val);
}
template <class T>
int SearchTree<T>::Includes(Node<T> * current,T val) const
{
// see if argument value occurs in tree
if(!current)
return 0; // not found
if (current->value == val)
return 1;
if (current->value < val)
return Includes(current->right,val);
else
return Includes(current->left,val);
}
cout << "The Array before inserting to tree: " << endl;
for( int i = 0; i < 10; ++i ) {
int num = (int) rand() % 10;
cout << num << " ";
v.push_back( num );
}