-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Search Tree
123 lines (123 loc) · 3.24 KB
/
Binary Search Tree
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include<fstream>
using namespace std;
template< class ItemType >
struct TreeNode {
ItemType info;// Data member
TreeNode<ItemType>* left;// Pointer to left child
TreeNode<ItemType>* right; // Pointer to right child
};
template< class ItemType >
class TreeType {
public:
TreeType ( ) ; // constructor
~TreeType ( ) ; // destructor
bool IsEmpty ( ) ;
bool IsFull ( ) ;
int NumberOfNodes ( ) ;
void InsertItem ( ItemType item ) ;
void DeleteItem (ItemType item ) ;
void RetrieveItem ( ItemType& item , bool& found ) ;
void PrintTree (ofstream& outFile) ;
private:
TreeNode<ItemType>* root ;
};
template< class ItemType >
void PrintHelper ( TreeNode<ItemType>* ptr, ofstream& outFile ) ;
template< class ItemType >
void InsertHelper ( TreeNode<ItemType>*& ptr, ItemType item ) ;
template< class ItemType >
void RetrieveHelper ( TreeNode<ItemType>* ptr, ItemType& item,
bool& found ) ;
template< class ItemType >
void DestroyHelper ( TreeNode<ItemType>* ptr ) ;
template< class ItemType >
TreeType<ItemType> :: TreeType ( ) // constructor
{
root = NULL ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
template< class ItemType >
bool TreeType<ItemType> :: IsEmpty()
{
return ( root == NULL ) ;
}
template< class ItemType >
void TreeType<ItemType> :: InsertItem ( ItemType item )
{
InsertHelper ( root, item ) ;
}
template< class ItemType >
void InsertHelper ( TreeNode<ItemType>*& ptr, ItemType item )
{ if ( ptr == NULL )
{// INSERT item HERE AS LEAF
ptr = new TreeNode<ItemType> ;
ptr->right = NULL ;
ptr->left = NULL ;
ptr->info = item ;
}
else if ( item < ptr->info )// GO LEFT
InsertHelper( ptr->left , item ) ;
else if ( item > ptr->info ) // GO RIGHT
InsertHelper( ptr->right , item ) ;
}
template< class ItemType >
void TreeType<ItemType> :: RetrieveItem ( ItemType& item,
bool& found )
{
RetrieveHelper ( root, item, found ) ;
}
template< class ItemType >
void RetrieveHelper ( TreeNode<ItemType>* ptr, ItemType& item,
bool& found)
{ if ( ptr == NULL )
found = false ;
else if ( item < ptr->info )// GO LEFT
RetrieveHelper( ptr->left , item, found ) ;
else if ( item > ptr->info ) // GO RIGHT
RetrieveHelper( ptr->right , item, found ) ;
else
{ item = ptr->info ;
found = true ;
}
}
template< class ItemType >
void TreeType<ItemType> :: PrintTree ( ofstream& outFile )
{
PrintHelper ( root, outFile ) ;
}
template< class ItemType >
void PrintHelper ( TreeNode<ItemType>* ptr, ofstream& outFile )
{ if ( ptr != NULL )
{
PrintHelper( ptr->left , outFile ) ;// Print left subtree
outFile << ptr->info ;
PrintHelper( ptr->right, outFile ) ;
}
}
template< class ItemType >
TreeType<ItemType> :: ~TreeType ( )// DESTRUCTOR
{
DestroyHelper ( root ) ;
}
template< class ItemType >
void DestroyHelper ( TreeNode<ItemType>* ptr )
// Post: All nodes of the tree pointed to by ptr are deallocated.
{ if ( ptr != NULL )
{
DestroyHelper ( ptr->left ) ;
DestroyHelper ( ptr->right ) ;
delete ptr ;
}
}
int main()
{
int a, b, c;
TreeType<int, int, int> obj;
ifstream file;
file.open("TreeFile.txt");
while(!file.eof())
{
//Enter file data reading material algo :-)
}
}