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
|
//____________ Private insert __________________
// Inserts element in the tree pointed to by
// aRoot.Called by public insert.
// Pre : aRoot k and d are defined.
// Post: If a node with same key as k is found,
// returns false. If an empty tree is reached,
// inserts element as a leaf node and returns true.
template <class keyType, class dataType>
bool binaryTree<keyType, dataType>::insert2(NodePointer &aRoot,
const keyType &k, const dataType &d)
{
// Check for empty tree.
if (aRoot == NULL)
{ // Attach new node
aRoot = new treeNode;
aRoot->left = NULL;
aRoot->right = NULL;
aRoot->key = k;
aRoot->data = d;
return true;
}
else if (k == aRoot->key)
return false;
else if (k < aRoot->key)
return insert2 (aRoot->left, k, d);
else
return insert2 (aRoot->right, k, d);
} // end of private insert
//____________ Public empty __________________
// Check if tree is empty
// Pre : none
// Post: Returns true if tree is empty, false
// otherwise
template <class keyType, class dataType>
bool binaryTree<keyType, dataType>::empty() const
{
return(root == NULL);
} // end of empty
//____________ Public traverse__________________
// Traverses a binary search tree in key order.
// Pre : none
// Post: Each element of the tree is displayed.
// Elements are displayed in key order.
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::traverse() const
{
traverse2 (root);
} // end of public traverse
//____________ Private traverse__________________
// Traverses the binary search tree pointed to
// by aRoot in key order. Called by traverse.
// Pre : aRoot is defined.
// Post: displays each node in key order.
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::traverse2 (NodePointer aRoot) const
{
if (aRoot != NULL)
{ // recursive in-order traversal
traverse2 (aRoot->left);
cout << aRoot->key << " " << aRoot->data << endl;
traverse2 (aRoot->right);
}
} // end of private traverse
//____Public pre-order traversal (Iterative)______
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::preorder () const
{
Stackt<NodePointer> s;
NodePointer t = root;
s.push(t);
while(!s.stackIsEmpty())
{
s.pop(t); cout << t->key << endl;
if ( t->right != NULL ) s.push(t->right);
if ( t->left != NULL ) s.push (t->left);
}
}
//____Public Level-order traversal (Iterative)______
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::levelorder () const
{
Queuet<NodePointer> q;
NodePointer t = root;
q.enqueue(t);
while(!q.queueIsEmpty())
{
q.dequeue(t); cout << t->key << endl;
if ( t->left != NULL ) q.enqueue (t->left);
if ( t->right != NULL ) q.enqueue (t->right);
}
}
//____________ Public graph__________________
// Graphic output of a BST
// Pre : none
// Post: Graphical representation is displayed.
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::graph() const
{
graph2 (0 , root);
}// end of public graph
//____________ Private graph__________________
// Graphic output of a subtree pointed to by
// aRoot with indent spaces
// Pre : none
// Post: Graphical representation is displayed.
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::graph2(int indent, NodePointer aRoot) const
{
if (aRoot != NULL)
{ // recursive in-order traversal
graph2 (indent+8, aRoot->right);
cout << setw(indent) << " " << aRoot->key << endl;
graph2 (indent+8, aRoot->left);
}
}// end of private graph
//____________ Public remove __________________
// Remove an element from the binary search tree
// Pre : k is defined.
// Post: if k is present, its node will be
// removed and tree will be modified
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::remove (const keyType &k)
{
bool found;
NodePointer x,parent;
// Search for element and its parent
parentSearch (k, found, x, parent);
if (!found)
{
cout << "Item not in BST\n";
return;
}
// else, element is found
if ((x->left != NULL)&&(x->right != NULL))
{ // Node has two children
// Find inorder successor and its parent
NodePointer xSucc = x->right;
parent = x;
while (xSucc->left != NULL) // descend left
{ parent = xSucc; xSucc = xSucc->left; }
// Move contents of xSucc to x and change x
// to point to successor, which will be removed
x->key = xSucc->key; x->data = xSucc->data;
x = xSucc;
} // end if node has two children
// Now, node has 0 or 1 child
NodePointer subtree = x->left; // subtree of x
if (subtree == NULL) subtree = x->right;
if (parent == NULL) root = subtree; //remove root
else if (parent->left == x) //parent left child
parent->left = subtree;
else parent->right = subtree; // right child
delete x;
} // end of public remove
//____________ Private parentSearch __________________
// Locate a node containing key k and its parent
// Pre : none.
// Post: locptr points to node containing k
// or is NULL if not found, and parent points to
// its parent. Used by remove
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::parentSearch (const keyType &k,
bool &found,
NodePointer &locptr,
NodePointer &parent) const
{
locptr = root; parent = NULL; found = false;
while (!found && locptr != NULL)
{
if (k < locptr->key) // descend left
{
parent = locptr; locptr = locptr->left;
}
else if (locptr->key < k) // descend right
{
parent = locptr; locptr = locptr->right;
}
else found = true; // el found
}// end while
} // end of private parentSearch
//____________ Public MAX__________________
// Traverses a binary search tree in key order.
// Gets the max of counter in the tree.
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::MAX(keyType & word, dataType & count) const
{
MAX2(root, word, count);
} // end of public MAX
//____________ Private MAX__________________
// Traverses the binary search tree pointed to
// Gets the max of counter in the tree.
template <class keyType, class dataType>
void binaryTree<keyType, dataType>::MAX2(NodePointer aRoot, keyType & word, dataType & count) const
{
if (aRoot != NULL)
{
MAX2(aRoot->left, word, count);
count= (aRoot->data > count) ? aRoot->data: count;
word=(aRoot->data > count) ? aRoot->key: word;
MAX2(aRoot->right, word, count);
}
}
|