Lab 9
Lab 9
Note:
Lab manual cover following below Stack and Queue topics
{Tree, BST, Design and implement classes for binary tree nodes and nodes for general tree, Traverse
the tree with the three common orders, Operation such as searches, insertions, and removals on a
binary search tree and its applications}
Maintain discipline during the lab.
Just raise hand if you have any problem.
Completing all tasks of each lab is compulsory.
Get your lab checked at the end of the session.
● The left subtree of a particular node will always contain nodes whose keys are less than
that node’s key.
● The right subtree of a particular node will always contain nodes with keys greater than
that node’s key. The left and right subtree of a particular node will also, in turn, be binary
search trees
Task-1:
Build functionality along with working example named auto Grader which will assist DS teacher
to check students BST assignments such that if given tree is BST assign 10 points if not then
assign 0.
BST Insertion
Sample Code of class Nodes
Node leftChild;
Node rightChild; public:
Node(int key, string name) {
this.key = key;
this.name = name;
}
string toString() {
class BinaryTree {
private: Node root;
public:
void addNode(int key, string name) {
if (root == NULL) {
----------------------------
} else {
// Set root as the Node we will start with as we traverse the tree
-----------------------------
// Future parent for new Node
Node parent;
while (true) {
// root is the top set the parent node to the root node
--------------------------
B) Implement main.cpp for the code provided such that a given array is passed to form
a BST{ 15, 10, 20, 8, 12, 16, 25 }
Tree Traversals: Inorder, PreOrder, PostOrder
Pseudo code For Inorder Traversal (iteration)
Task-3:
Write recursive algorithms that perform preorder and inorder tree walks.
1. Visit Node.
2. Traverse Node’s left sub-tree.
3. Traverse Node’s right sub-tree
BST Deletion
BST Deletion
50 50
/ \ delete(20) / \
30 70 ---------> 30 70
/ \ / \ \ / \
20 40 60 80 40 60 80
2) Node to be deleted has only one child: Copy the child to the node and delete the child
50 50
/ \ delete(30) / \
30 70 ---------> 40 70
\ / \ / \
40 60 80 60 80
3) Node to be deleted has two children: Find inorder successor of the node. Copy contents of the
inorder successor to the node and delete the inorder successor. Note that inorder predecessor can
also be used.
50 60
/ \ delete(50) / \
40 70 ---------> 40 70
/ \ \
60 80 80
The important thing to note is, inorder successor is needed only when the right child is not
empty. In this particular case, inorder successor can be obtained by finding the minimum value
in the right child of the node.
Task:4
Given a BST and a range of keys(values), remove nodes from BST that have keys outside the given
range
Some points to Note:
Traversals
The binary search tree property allows us to obtain all the keys in a binary search tree in a sorted
order by a simple traversing algorithm, called an in order tree walk, that traverses the left sub
tree of the root in in order traverse, then accessing the root node itself, then traversing in in-order
the right sub tree of the root node.
The tree may also be traversed in preorder or post order traversals. By first accessing the root,
and then the left and the right sub-tree or the right and then the left sub-tree to be traversed in
preorder. And the opposite for the post order.
The algorithms are described below, with Node initialized to the tree’s root.
• Preorder Traversal
1. Visit Node.
2. Traverse Node’s left sub-tree.
3. Traverse Node’s right sub-tree.
• In-order Traversal
• Post-order Traversal
Searching
We use the following procedure to search for a node with a given key in a binary search tree.
Given a pointer to the root of the tree and a key k, TREE-SEARCH returns a pointer to a node with
key k if one exists; otherwise, it returns NIL.
TREE-SEARCH (x, k)
1 if x = NIL or k = key[x]
2 then return x
3 if k < key[x]
4 then return TREE-SEARCH (left[x], k)
5 else return TREE-SEARCH (right[x], k)
The procedure begins its search at the root and traces a path downward in the tree, as shown in
Figure 13.2. For each node x it encounters, it compares the key k with key[x]. If the two keys are
equal, the search terminates. If k is smaller than key[x], the search continues in the left subtree of
x, since the binary-search-tree property implies that k could not be stored in the right subtree.
Symmetrically, if k is larger than key[k], the search continues in the right subtree. The nodes
encountered during the recursion form a path downward from the root of the tree, and thus the
running time of TREE-SEARCH is O(h), where h is the height of the tree.