Error in sort words dictionary by using AVL tree
Help me fix this error.
file .h
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
|
#ifndef BST_H
#define BST_H
#include <iostream>
#include <cstring>
#include <iomanip>
template <class T>
struct Node
{
T *data;//key word dictionary
Node *left;
Node *right;
// <=> Method return data of a Node
T *GetData()
{
return data;
}
};
template <class T>
class BST
{
private:
Node<T> *root;
public:
//Constructor
BST();
//Check Empty Tree
bool isEmpty()const;
//Insert a Node
void InsertNode(const T *);
//return Parent Node
Node<T> *returnParentNode(const T *);
Node<T> *ReturnNodeDad(Node<T> *);
void DeleteAnyoneNode(const T *);
//Traverse Binary Tree
void TraverseTree();
//
void DuyetGiuaCay(Node<T> *p);
};
template <class T>
BST<T>::BST()
{
root = NULL;
}
//
template <class T>
bool BST<T>::isEmpty()const
{
return root == NULL;
}
//
template <class T>
void BST<T>::InsertNode(const T *gt)
{
Node<T> *newNode = new Node<T>;
//newNode->data = new T[20];
//newNode->data = gt;
strcpy(newNode->data,gt);
newNode->left = NULL;
newNode->right = NULL;
if(isEmpty())
{
root = newNode;
return;
}
else
{
//begin from root
Node<T> *curr = root;
Node<T> *parent;
while(curr!=NULL)
{
parent = curr;
if(curr->data > newNode->data)
curr = curr->left;
else
curr = curr->right;
}
if(parent->data > newNode->data)
parent->left = newNode;
else
parent->right= newNode;
}
}
//method find parent Node
template <class T>
Node<T> *BST<T>::returnParentNode(const T *gt)
{
bool found = false;
Node<T> *tmp = root;
Node<T> *parent;
while(tmp->data !=gt)
{
parent = tmp;
if(tmp->data == gt)
{
found = true;
break;
}
else
{
if(tmp->data > gt)
tmp = tmp->left;
else
tmp = tmp->right;
}
}
if(parent->left ==tmp || parent->right == tmp)
return parent;
else
return NULL;
}
//Delete Node from BST
template <class T>
void BST<T>::DeleteAnyoneNode(const T *gt)
{
bool found = false;
//Traverse BST to fine a Node include gt
Node<T> *curr = root;
while(curr!=NULL)//
if(curr->data == gt)
{
found = true;
break;
}
else
{
if(curr->data > gt)
curr = curr->left; // <=> move left
else
curr = curr->right; // <=> move right
}
}
if(!found)
{
std::cout<<"Du lieu khong tim thay!"<<std::endl;
return;
}
//
//===============================CASE 1: if Node is leaf=====================================================
if(curr->left == NULL && curr->right == NULL)//
{
Node<T> *cha = returnParentNode(gt);
if(cha->left == curr)
cha->left = NULL;
else
cha->right = NULL;
delete curr;
return;
}
//end TH1
//==============================TH2: Only one Node ============================================
if( curr->left != NULL && curr->right == NULL || curr->right != NULL && curr->left == NULL)
{
if(curr->left != NULL && curr->right == NULL)
{
Node<T> *cha = returnParentNode(gt);
if(cha->left ==curr)
{
cha->left = curr->left;
delete curr;
}
else
{
cha->right = curr->left;
delete curr;
}
}
else
{
Node<T> *cha = returnParentNode(gt);
if(cha->left == curr)
{
cha->left = curr->right;
delete curr;
}
else
{
cha->right = curr->right;
delete curr;
}
}
}//end TH2
//============================================= TH3: have two Node ==================================
else
{
//ta se tim Node co gia tri gan Node nhat
}
}
//Method Traverse Tree
template <class T>
void BST<T>::TraverseTree()
{
DuyetGiuaCay(root);
}
template <class T>
void BST<T>::DuyetGiuaCay(Node<T> *p)
{
if(p!=NULL)
{
std::cout<<p->data<<"; ";
if(p->left)
DuyetGiuaCay(p->left);
if(p->right)
DuyetGiuaCay(p->right);
}
else
return;
}
#endif
|
file .cpp
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
|
#include "BinaryTreeSearch.h"
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;
int main()
{
BST<char const *> a;
a.InsertNode("A");
a.InsertNode("B");
a.InsertNode("C");
a.InsertNode("D");
a.InsertNode("E");
a.InsertNode("F");
a.InsertNode("G");
//nhanh ben phai cua cay
a.InsertNode("tuanviet");
a.InsertNode("MinhLong");
a.InsertNode("John");
a.InsertNode("Alan Smith");
a.InsertNode("VanderSar");
a.InsertNode("XuanThang");
a.InsertNode("Manh Linh");
cout<<a.returnParentNode("G")->GetData()<<endl;
a.DeleteAnyoneNode("John");
a.TraverseTree();
cout<<endl;
//getch();
return 0;
}
|
What is the error ?
Topic archived. No new replies allowed.