forked from toru1234/TeamProject-10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.cpp
146 lines (130 loc) · 3.37 KB
/
BinaryTree.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
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
#include "BinaryTree.h"
BinaryTree::BinaryTree(const BinaryTree & tree)
{
// check if the tree is valid
if (tree.isEmpty())
{
rootPtr = 0;
count = 0;
}
else
{
// make the pointer pointing to the tree pointer.
rootPtr = copyTree(tree.rootPtr);
}
}
BinaryNode* BinaryTree::copyTree(const BinaryNode* nodePtr)
{
if (nodePtr == 0)
{
return 0;
}
else
{
// make a new node
BinaryNode* newNode = new BinaryNode(nodePtr->getWebsite());
newNode->setLeftPtr(copyTree(nodePtr->getLeftPtr()));
newNode->setRightPtr(copyTree(nodePtr->getRightPtr()));
return newNode;
}
}
void BinaryTree::destroyTree(BinaryNode* nodePtr)
{
// left-root-right
if (nodePtr == 0)
{
return;
}
destroyTree(nodePtr->getLeftPtr());
destroyTree(nodePtr->getRightPtr());
delete nodePtr;
}
void BinaryTree::inOrder(void visit(Website &),
const Website & minItem,
const Website & maxItem) const
{
_inorder(visit, rootPtr, minItem, maxItem);
}
void BinaryTree::_preorder(void visit(Website &), BinaryNode* nodePtr) const
{
// root-left-right
if (nodePtr != 0)
{
Website web = nodePtr->getWebsite();
visit(web);
_preorder(visit, nodePtr->getLeftPtr());
_preorder(visit, nodePtr->getRightPtr());
}
}
void BinaryTree::_inorder(void visit(Website &), BinaryNode* nodePtr) const
{
// left-root-right
if (nodePtr == 0)
{
return;
}
_inorder(visit, nodePtr->getLeftPtr());
Website web = nodePtr->getWebsite();
visit(web);
_inorder(visit, nodePtr->getRightPtr());
}
void BinaryTree::_inorder(void visit(Website &),
BinaryNode* nodePtr,
const Website & minItem,
const Website & maxItem) const
{
// Node is null
if (nodePtr == 0)
{
return;
}
// Node is outside of desired range
Website currentItem = nodePtr->getWebsite();
if (currentItem < minItem || currentItem > maxItem)
{
return;
}
_inorder(visit, nodePtr->getLeftPtr(), minItem, maxItem);
visit(currentItem);
_inorder(visit, nodePtr->getRightPtr(), minItem, maxItem);
}
void BinaryTree::_postorder(void visit(Website &), BinaryNode* nodePtr) const
{
//left-right-root
if (nodePtr == 0)
{
return;
}
_postorder(visit, nodePtr->getLeftPtr());
_postorder(visit, nodePtr->getRightPtr());
Website web = nodePtr->getWebsite();
visit(web);
}
BinaryTree & BinaryTree::operator= (const BinaryTree & sourceTree)
{
// clear data first(if the left object is not empty)
if (!this->isEmpty())
{
this->clear();
}
rootPtr = copyTree(sourceTree.rootPtr);
count = sourceTree.count;
return *this;
}
void BinaryTree::_printTreeIndented(BinaryNode* nodePtr, int depth) const
{
// check if the node points to null.
if (nodePtr == 0)
{
return;
}
// go to the right
_printTreeIndented(nodePtr->getRightPtr(), depth + 1);
for (int i = depth-1; i > 0; --i)
{
cout << "\t";
}
cout << depth << "." << nodePtr->getWebsite() << endl;
// go to the left
_printTreeIndented(nodePtr->getLeftPtr(), depth + 1);
}