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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#include <iostream>
using namespace std;
struct TreeNode;
struct ItemType;
class TreeType
{
public:
TreeType();
~TreeType();
bool IsEmpty() const;
bool IsFull() const;
void RetrieveItem(ItemType& item, bool& found) const;
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void Print(std::ofstream& outFile) const;
private:
TreeNode* root;
};
struct ItemType
{
string name;
string number;
};
struct TreeNode
{
ItemType info;
TreeNode* left;
TreeNode* right;
};
bool TreeType::IsFull() const
{
TreeNode* location;
try
{
location=new TreeNode;
delete location;
return false;
}
catch(std::bad_alloc exeception)
{
return true;
}
}
bool TreeType::IsEmpty() const
{
return root==NULL;
}
void Retrieve(TreeNode* tree, ItemType& item, bool& found);
void TreeType::RetrieveItem(ItemType& item, bool& found) const
{
Retrieve(root, item, found);
}
void Retrieve(TreeNode* tree, ItemType& item, bool& found)
{
if (tree==NULL)
{
found=false;
}
else if (item < tree->info)
{
Retrieve(tree->left, item, found);
}
else if (item > tree->info)
{
Retrieve(tree->right, item, found);
}
else
{
item=tree->info;
found=true;
}
}
void Insert(TreeNode*& tree, ItemType item);
void TreeType::InsertItem(ItemType item)
{
Insert(root, item);
}
void Insert(TreeNode*& tree, ItemType item)
{
if (tree==NULL)
{
tree=new TreeNode;
tree->right= NULL;
tree->left= NULL;
tree->info= item;
}
else if (item < tree->info)
{
Insert(tree->left, item);
}
else
{
Insert(tree->right, item);
}
void DeleteNode(TreeNode*& tree);
void Delete(TreeNode*& tree, ItemType item);
void TreeType::DeleteItem(ItemType item)
{
Delete(root, item);
}
void Delete(TreeNode*& tree, ItemType item)
//Error: member function "TreeType::DeleteItem" may not be redeclared outside its class
{
if (item < tree->info)
{
Delete(tree->left, item);
}
else if (item > tree->info);
{
Delete(tree->right, item)
}
else
{
DeleteNode(tree);
}
}
void GetPredecessor(TreeNode* tree, ItemType& data);
void DeleteNode(TreeNode*& tree)
{
ItemType data;
TreeNode* tempPtr;
tempPtr=tree;
if(tree->left==NULL)
{
tree=tree->right;
delete tempPtr;
}
else if(tree->right==NULL)
{
tree=tree->left;
delete tempPtr;
}
else
{
GetPredecessor(tree->left, data);
tree->info=data;
Delete(tree->left, data);
//Error: identifier "data" is undefined
}
}
void GetPredecessor(TreeNode* tree, ItemType& data)
{
while(Tree->right !=NULL)
tree=tree->right;
data=tree->info;
}
void PrintTree(TreeNode* tree, std::ofstream& outFile)
{
if(tree !=NULL)
{
PrintTree(tree->left, outfile);
outFile<<tree->info;
PrintTree(tree->right, outfile);
}
}
void TreeType::Print(std::ofstream& outFile) const
{
PrintTree(root,outFile);
}
TreeType::TreeType()
{
root=NULL;
}
void Destory(TreeNode*& tree);
TreeType::~TreeType()
{
Destory(root);
}
void Destroy(TreeNode*& tree)
{
if (tree !=NULL)
{
Destroy(tree->left);
Destory(tree->right);
delete tree;
}
}
|