Sei sulla pagina 1di 1

#include <fstream.

h> (Specification of Binary Tree) Function Number Of Nodes


struct TreeNode<ItemType>; template<class ItemType>
enum OrderType {PRE_ORDER, IN_ORDER, POST_ORDER}; int TreeType<ItemType>::NumberOfNodes() const
template<class ItemType> {
class TreeType { return CountNodes(root);
public: }
TreeType();  
~TreeType(); template<class ItemType>
TreeType(const TreeType<ItemType>&); int CountNodes(TreeNode<ItemType>* tree)
void operator=(const TreeType<ItemType>&); {
void MakeEmpty(); if (tree == NULL)
bool IsEmpty() const; return 0;
bool IsFull() const; else
int NumberOfNodes() const; return CountNodes(tree->left) + CountNodes(tree->right)
void RetrieveItem(ItemType&, bool& found); + 1;
void InsertItem(ItemType); }
void DeleteItem(ItemType); Running Time Big O (N)
void ResetTree(OrderType);
void GetNextItem(ItemType&, OrderType, bool&);
void PrintTree(ofstream&) const;
private:
TreeNode<ItemType>* root;
};

template <class ItemType>


template<class ItemType>
void TreeType<ItemType>:: RetrieveItem(ItemType& item,
void TreeType<ItemType>::InsertItem(ItemType item)
bool& found)
{
{ Retrieve(root, item, found);
Insert(root, item);

}
template<class ItemType>
 
void Retrieve(TreeNode<ItemType>* tree, ItemType&
template<class ItemType>
item, bool& found)
void Insert(TreeNode<ItemType>*& tree, ItemType item)
{
{
if (tree == NULL) // base case 2
if(tree == NULL) { // base case
found = false;
tree = new TreeNode<ItemType>;
else if(item < tree->info)
tree->right = NULL;
Retrieve(tree->left, item, found);
tree->left = NULL;
else if(item > tree->info)
tree->info = item;
Retrieve(tree->right, item, found);
}
else { // base case 1 else if(item < tree->info)

item = tree->info; Insert(tree->left, item);

found = true; else

}} Insert(tree->right, item);

Running Time O (h) } Running Tim O (h)

Potrebbero piacerti anche