You are on page 1of 5

#include <iostream>

#include <string>
#include <stdio.h>
using namespace std;

struct Student {
int id;
int age;
string name;
};

struct treeNode {
Student data;
treeNode* leftChild;
treeNode* rightChild;
};

class BSTree {
public:
BSTree();
~BSTree();
void addNode(Student& std);
void deleteNode(int id);
void search(int id);
void displayPreorder();
void displayInorder();
void displayPostorder();

private:
// utility functions
treeNode* makeEmpty(treeNode* t);
treeNode* deleteNode(int id, treeNode* t);
treeNode* findMin(treeNode* t);
treeNode* findMax(treeNode* t);
treeNode* find(treeNode* t, int id);
void preorder(treeNode* t);
void inorder(treeNode* t);
void postorder(treeNode* t);

treeNode* root;
};

// constructor
BSTree::BSTree() :root(0) { }

//destructor
BSTree::~BSTree()
{
root = makeEmpty(root);
}

void BSTree::deleteNode(int id)


{
root = deleteNode(id, root);
}

void BSTree::displayPreorder()
{
preorder(root);
cout << endl;
}
void BSTree::displayInorder()
{
inorder(root);
cout << endl;
}
void BSTree::displayPostorder()
{
postorder(root);
cout << endl;
}

void BSTree::search(int id)


{
root = find(root, id);
}

treeNode* BSTree::makeEmpty(treeNode* t)
{
if (t == NULL)
return NULL;
{
makeEmpty(t->leftChild);
makeEmpty(t->rightChild);
delete t;
}
return NULL;
}

void BSTree::addNode(Student& std)//建立二元樹 引入 student id,age,name


{
if (root == NULL) //建立 root
{
root = new treeNode;
root->data = std;
root->leftChild = NULL;
root->rightChild = NULL;
}
else//建立 child
{
treeNode* x = root;
treeNode* temp = new treeNode;//新增一個 treeNode 物件
temp->data = std;//將引入值傳入 data
temp->leftChild = NULL;
temp->rightChild = NULL;
while (1)
{
if (std.id < x->data.id && x->leftChild != NULL)//若 std 的 id 比
parent 小則丟到左邊
x = x->leftChild;
else if (std.id > x->data.id && x->rightChild != NULL)//若 std 的
id 比 parent 大則丟到右邊
x = x->rightChild;
else if (std.id < x->data.id && x->leftChild == NULL)//傳入後左結點
沒有 data 狀況下寫入
{
x->leftChild = temp;
break;
}
else if (std.id > x->data.id && x->rightChild == NULL)//傳入後左結
點沒有 data 狀況下寫入
{
x->rightChild = temp;
break;
}
else if (std.id == temp->data.id)
break;
}
}
}

treeNode* BSTree::findMin(treeNode* t)
{
if (t == NULL)
return NULL;
else if (t->leftChild == NULL)
return t;
else
return findMin(t->leftChild);
}

treeNode* BSTree::findMax(treeNode* t)
{
if (t == NULL)
return NULL;
else if (t->rightChild == NULL)
return t;
else
return findMax(t->rightChild);
}

treeNode* BSTree::deleteNode(int id, treeNode* t)


{
treeNode* temp;
if (t == NULL)
return NULL;
else if (id < t->data.id)
t->leftChild = deleteNode(id, t->leftChild);
else if (id> t->data.id)
t->rightChild = deleteNode(id, t->rightChild);
else if (t->leftChild && t->rightChild)
{
temp = findMin(t->rightChild);
t->data = temp->data;
t->rightChild = deleteNode(t->data.id, t->rightChild);
}
else
{
temp = t;
if (t->leftChild == NULL)
t = t->rightChild;
else if (t->rightChild == NULL)
t = t->leftChild;
delete temp;
}

return t;
}

void BSTree::preorder(treeNode* t)
{
if (t == NULL)
return;
cout << "Preorder\n" << "<" << t->data.id << ", " << t->data.age << ", " <<
t->data.name << ">" << endl;
preorder(t->leftChild);
preorder(t->rightChild);
}

void BSTree::inorder(treeNode* t)
{
if (t == NULL)
return;
inorder(t->leftChild);
cout << "Inorder\n" << "<" << t->data.id << ", " << t->data.age << ", " << t-
>data.name << ">" << endl;
inorder(t->rightChild);
}

void BSTree::postorder(treeNode* t)
{
if (t == NULL)
return;

postorder(t->leftChild);
postorder(t->rightChild);
cout << "Postorder\n" << "<" << t->data.id << ", " << t->data.age << ", " <<
t->data.name << ">" << endl;
}

treeNode* BSTree::find(treeNode* t, int id)


{
if (t == NULL)
return NULL;
else if (id < t->data.id)
return find(t->leftChild, id);
else if (id > t->data.id)
return find(t->rightChild, id);
else
return t;
}

int main() {
BSTree tree1;

Student std[7] = { { 103, 19, "Wang" }, { 102, 22, "Lin" }, { 101, 20, "Tsai"
}, { 104, 26, "Lee" }, { 106, 24, "Jack" }, { 105, 19, "Pass" }, {103, 25, "Huang"}
};
// A 103 拆解為二元樹後
順序 A
// / \ 102 104
/ \
// B C 101 106
B D
// / \ / \ 105
/ \
// D E F G
C E
for (int i = 0; i < 7; ++i) { //
/
tree1.addNode(std[i]); //
F
}
cout << "Preorder" << endl;// A->B->D->E->C->F->G
tree1.displayPreorder();
cout << "Inorder" << endl;// D->B->E->A->F->C->G
tree1.displayInorder();
cout << "Postorder" << endl;// C->B->F->E->D->A
tree1.displayPostorder();

tree1.deleteNode(103);

cout << "After delete 103\n" << endl;


cout << "Preorder" << endl;
tree1.displayPreorder();
cout << "Inorder" << endl;
tree1.displayInorder();
cout << "Postorder" << endl;
tree1.displayPostorder();
system("pause");
return 0;
}

You might also like