0% found this document useful (0 votes)
25 views4 pages

Writeup 4 Dictionary

Uploaded by

pamate1130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Writeup 4 Dictionary

Uploaded by

pamate1130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT NO.

Problem Statement: A Dictionary stores keywords & its meaning. 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.
Pre-requisite:
 Knowledge of C++ programming
 Basic knowledge of Binary Tree and Binary Search Tree.
Theory:

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

1
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.
3. Deletion :There are three possible cases to consider:
1. Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply
remove it from the tree.
2. Deleting a node with one child: Remove the node and replace it with its child.
3. Deleting a node with two children: Call the node to be deleted N. Do not delete N.
Instead, choose either its in-order successor node or its in-order predecessor node,
R. Replace the value of N with the value of R, then delete R.
As with all binary trees, a node's in-order successor is the left-most child of its right
subtree, and a node's in-order predecessor is the right-most child of its left subtree. In
either case, this node will have zero or one children. Delete it according to one of the
two simpler cases above.

Algorithms for creating keyword Dictionary using BST:

1. Create ( )
Step 1: Allocate the memory by using new keyword for a new node(temp). Make its
left and right node Pointer NULL.
Step 2: Accept the keyword and it’s meaning from user and store it in the Keyword and Meaning
part of new node.
Step 3: Check whether the root is pointing to NULL , if it is then assign the value of new
node Pointer to the root node pointer.
Step 4: If the root node pointer is not NULL, then call insert function to insert new
node temp in the tree at its correct position.
Step 5: stop

2. Insert function()
Step 1: Compare the keyword field of temp and keyword field of trav node
(which is at root initially) .Use strcmp function to compare the
strings(keywords)
Step 2: If temp-> Keyword is greater than trav ->Keyword, then perform
2
temp=temp->right otherwise temp=temp->left
Step 7: Repeat the step 1 and 2 until temp encounters the NULL value.
Step 8: Link the new node to the parent node of temp properly according to its value.

Algorithm to display entries in the dictionary in ascending order

3. Inorder_display (node *root )//to display keywords in ascending order

Step 1: Check whether root==NULL


Step 2: If not then call function inorder (root->left).
Step 3: Display Key word and it’s meaning
stored in root node.
Step 4: Call the function inorder ( root->right).
Step 4: Stop

// Search an element from binary search Tree

4. search ( )
Step 1: Accept the Keyword which is to be searched from the user.
Step 2: Store the address of the root node in the temp node pointer
Step 3: Traverse the tree until the required keyword and the
keyword in the temp matches.
If the required data is greater than the k e y word in the temp then
perform perform temp=temp->right
else
Perform temp=temp->left
If temp encounters the NULL value then come out of the loop statement by
using break statemment.
Step 4: return temp
5. Update entry ()
Step 1: Accept the Keyword for which entry in Dictionary needs to be updated.
Step 2: call search function to search the keyword
Step 3: If the pointer temp which is returned by search function is
NULL then required keyword is not present in tree.
If the required keyword is present then ask for new meaning of
keyword and update it in the meaning field of the temp node.
Step 4: stop.
6. Delete entry()
Step 1: Accept the Keyword x for which entry in Dictionary needs to be deleted
Step 2: Check if root= NULL then delete function will return NULL
Step 3: keyword x and root->keyword is compared using strcmp
function

3
- If x is less than root->keyword then call delete function
recursively to delete keyword from left subtree
- If x is greater than root->keyword then call delete function
recursively to delete keyword from right subtree
- If keyword and root->keyword is matching then
-If check if root is having left child then left child will take
place of root
-If check if root is having right child then right child will take
place of root
-If root is having both child nodes then find inorder successor
of root(temp) from right subtree of root
Replace root->keyword with temp->keyword and delete
temp from right subtree of root.
7. Max Comparisons()
For find out maximum comparisons to search any keyword from tree,
we need to find out height of tree

Step 1: when root =NULL height of tree =0


Step 2: when root has no left or right child then height of tree = 0
Step3:when root has left and/or right subtree then return maximum
from height of left subtree and height of right subtree as a height of
tree.
Step 4: Total number of comparison to find any keyword from
dictionary = height of tree +1

If the required keyword is present then ask for new meaning of


keyword and update it in the meaning field of the temp node.
Step 4: stop.

Conclusion:
Thus we studied the binary search tree and its operations and successfully implemented it for
solving the given problem.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy