Lab Assignment No.5(B)_Write-Up
Lab Assignment No.5(B)_Write-Up
5(B)
Aim:
A Dictionary stores keywords and its meanings. Provide facility for adding new keywords, deleting
keywords, updating values of any entry. Provide facility to display whole data sorted in ascending/
Descending order. Also find how many maximum comparisons may require for finding any
keyword. Use Binary Search Tree for implementation.
Title:
Operations on a Binary Search Tree: Construction, Insertion, Traversal, and Modification
Software Requirements :
Operating System recommended:- 64-bit Open source Linux or its derivative
Programming tools recommended:- Open Source G++/GCC
Objective:
To build a Binary Search Tree (BST) and perform basic operations like adding a node,
finding the longest path, getting the smallest value, swapping left and right children, and
searching for a value.
Theory:
Binary Tree : A tree is said to be a Binary tree if each node of the tree have maximum of two
child nodes. The children of the node of binary tree are ordered.
Binary Search Tree (BST ) : A binary tree in which each internal node x stores an element
such that the element stored in the left subtree of x are less than or equal to x and elements
stored in the right subtree of x are greater than or equal to x. This is called binary-search-tree
property.
Operations on Binary search Tree :
1. Insertion
Insertion begins as a search would begin; if the root is not equal to the value, we search the
left or right subtrees as before. Eventually, we will reach an external node and add the value
as its right or left child, depending on the node's value. In other words, we examine the root
and recursively insert the new node to the left subtree if the new value is less than the root, or
the right subtree if the new value is greater than or equal to the root.
2. Searching : Searching a binary tree for a specific value can be a recursive or iterative
process. This explanation covers a recursive method. We begin by examining the root node.
If the tree is null, the value we are searching for does not exist in the tree. Otherwise, if the
value equals the root, the search is successful. If the value is less than the root, search the left
subtree. Similarly, if it is greater than the root, search the right subtree. This process
is repeated until the value is found or the indicated subtree is null. If the searched value is not
found before a null subtree is reached, then the item must not be present in the tree.
Algorithm
2. Delete a Keyword
Procedure: Delete the node with the specified keyword.
Steps:
1. Search the tree for the node.
2. If node has no children, set it to null.
3. If node has one child, link it to the parent.
4. If node has two children, find the inorder successor, replace, and delete the
successor.
Flowchart :
Conclusion:
A Binary Search Tree (BST) enables efficient data operations, making it useful in searching,
sorting, and database applications.