1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H
#include <iostream>
#include "BinaryTree.h"
using namespace std;
template<class T> class BinarySearchTree: public BinaryTree<T>
{
public:
BinarySearchTree();
~BinarySearchTree();
// used function from problem area
void treeInsert(const T &insertItem);
void treeInsert(const T &insertItem1, const T &insertItem2);
// end used function area
private:
// problem area
void insert(Node<T> *newNode, Node<T> *parentNode);
void insert(Node<Map> *newNode, Node<Map> *parentNode);
// problem area
};
#endif
template<class T> BinarySearchTree<T>::BinarySearchTree(): BinaryTree() { }
template<class T> BinarySearchTree<T>::~BinarySearchTree() { }
template<class T> void BinarySearchTree<T>::treeInsert(const T &insertItem)
{
Node<T> *newNode = new Node<T>;
// initialize new node
newNode->info = insertItem;
newNode->lLink = NULL;
newNode->rLink = NULL;
// if tree is empty, make new node the root node
if(isEmpty())
{
root = newNode;
}
else
{
insert(newNode, root);
}
}
template<class T> void BinarySearchTree<T>::insert(Node<T> *newNode, Node<T> *parentNode)
{
if(newNode->info <= parentNode->info)
{
if(parentNode->lLink == NULL) // insert left
{
parentNode->lLink = newNode;
}
else
{
insert(newNode, parentNode->lLink);
}
}
else
{
if(parentNode->rLink == NULL) // insert right
{
parentNode->rLink = newNode;
}
else
{
insert(newNode, parentNode->rLink);
}
}
}
template<T> void treeInsert(const T &insertItem1, const T &insertItem2)
{
Node<Map> *newNode = new Node<Map>;
newNode->info.insert("Date", insertItem1);
newNode->info.insert("Reading", insertItem2);
newNode->lLink = NULL;
newNode->rLink = NULL;
// if tree is empty, make new node the root node
if(isEmpty())
{
root = newNode;
}
else
{
insert(newNode, root);
}
}
template<> void BinarySearchTree<Map>::insert(Node<Map> *newNode, Node<Map> *parentNode)
{
cout << ""; //todo
}
|