Question

arch tree for the words banana, peach, onut, mango, and papaya using alphabet- Build a binary search Describe an algo this nu
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Binary Search Tree (BST) is a tree data structure, where the left subtree of a node contains keys having less than or equal to the value of the key of the current node and the right subtree of a node contains keys having greater than the value of the key of the current node. In BST each node can have atmost two children, left child will have the key value lesser than the parent node and right child will have the key value greater than the parent node. In case of alphabetical Binary Search Tree, we add the new node by following dictionary-order. We can also think the ASCII value of each letter and then compare the ASCII value of the letters of nodes to add a new node.

For example, ASCII value of 'a' is '97'.Let us consider the current node has a word that starts with 'a'. Now if a new word is to entered which starts with 'c', we can compare their ASCII values in order to determine the position of the new incoming word. ASCII value of 'c' (99) > ASCII value of 'a' (97). So we need to check the right subtree of the current node again. If we found both the existing word in the node and the new incoming node has the same initial letter, then we will compare their second letter to determine the position of the new node.

Q1. The words that is to be entered to the BST (in alphabetical order) are : banana, peach, apple, pear, coconut, mango, papaya.

Enter word banana: We assume that initially the BST is NULL. It is the initial incoming node. So we will banana as the root node of BST.

banana

Enter word peach: We need to compare the incoming word with the root. ASCII value of 'p' (112) > ASCII value of 'b' (98). So we need to check the right subtree of the root, but it's NULL. So we add the word 'peach' as the right child of root node.

banana peach

Enter word apple: We need to compare the word with root. ASCII value of 'a' (97) < ASCII value of 'b' (98). So we need to check the left subtree of root. Since it's empty, so we add the word as the left child of the root.

banana apple peach

Enter word pear: We need to compare the word with root. ASCII value of 'p' (112) > ASCII value of 'b' (98). We check the right subtree and we found that it's not NULL. If we compare the word again with the right child of node 'banana', we found that the first three letters of both the words are same (peach and pear). So we compare the fourth letters of each words and found that ASCII of 'r' (114) > ASCII of 'c' (99). So we add the word pear as the right child of the node 'peach'.

banana apple peach pear

Enter word coconut: We compare the word with root and we check the right subtree ( ASCII of 'c' (99) > ASCII of 'b' (98)). We then compare the node 'peach' with the word and found that ASCII of 'c' (99) < ASCII of 'p' (112) and add the new word as the left child of node 'peach'.

banana apple peach coconut pear

Enter word mango: Comparing the word with root we check the right subtree. ASCII of 'm' (109) > ASCII of 'b' (98). Also, ASCII of 'm' (109) < ASCII of 'p' (112) , so now we again check the left subtree of node 'peach'. Again we found a word starting with 'c' (coconut) and we will check the right subtree of node 'coconut', since ASCII of 'm' (109) > ASCII of 'c' (99). Since it's pointing to NULL, we add the word mango as the right child of node 'coconut'.

banana apple peach coconut pear mango

Enter word papaya: ASCII of 'p' (112) > ASCII of 'b' (98). So we check right subtree of root. We found that the first letter of the words are same (papaya and peach). So we compare the second letter and found that ASCII of 'a' (97) < ASCII of 'e' (101). We move to the left subtree and find ASCII of 'p' (112) > ASCII of 'c' (99) and move to right subtree. Again ASCII of 'p' (112) > ASCII of 'm' (109) and we find the right subtree of node 'mango' is NULL. So we add the word papaya as the right child of the node 'mango'.

banana apple peach coconut pear mango papaya

Q2. Words to be entered to the BST (in alphabetical order) are : oenology, phrenology, campanology, ornithology, ichthyology, limnology, alchemy, astrology.

The problem is similar to the previous one.

Enter word oenology: First word. Initially the BST is NULL. So it is added as root.

oenology

Enter word phrenology: ASCII of 'p' (112) > ASCII of 'o' (111). So check the right subtree. It's NULL, add the word as the right child of root 'oenology'.

oenology phrenology

Enter word campanology: ASCII of 'c' (99) < ASCII of 'o' (111). We check the left subtree and it's NULL. Add the word as the left child of node 'oenology'.

oenology campanology phrenology

Enter word ornithology: The first letter of the incoming word and that of root is same (oenology and ornithology. So we check the second letter of them. ASCII of 'r' (114) > ASCII of 'e' (101). Check right subtree. ASCII of 'o' (111) < ASCII of 'p' (112). We add the word as left child of node 'phrenology'.

oenology campanology phrenology ornithology

Enter word ichthyology: ASCII of 'i' (105) < ASCII of 'o' (111). Check the left subtree of root. ASCII of 'i' (105) > ASCII of 'c' (99). Right subtree of node 'campanology' is NULL, add the word as it's right child.

oenology campanology phrenology ichthyology ornithology

Enter word limnology: ASCII of 'l' (108) < ASCII of 'o' (111). Check left subtree. ASCII of 'l' (108) > ASCII of 'c' (99). Check right subtree. ASCII of 'l' (108) > ASCII of 'i' (105). Check right subtree and it's NULL. Add the word as right child of node 'ichthyology'.

oenology campanology phrenology ichthyology ornithology limnology

Enter word alchemy: ASCII of 'a' (97) < ASCII of 'o' (111). Check left subtree. ASCII of 'a' (97) < ASCII of 'c' (99). Check left subtree and it's NULL, add the word as left child of node 'campanology'.

oenology campanology phrenology alchemy ichthyology ornithology limnology

Enter word astrology: ASCII of 'a' (97) < ASCII of 'o' (111). Check left subtree. ASCII of 'a' (97) < ASCII of 'c' (99). Check left subtree. Since the first letter of both the words (alchemy and astrology) are same now, we need to check the second letter of them. ASCII of 's' (115) > ASCII of 'l' (108). Check right subtree and it's NULL, so add the word as the right child of node 'alchemy'.

oenology campanology phrenology alchemy ichthyology ornithology astrology limnology

Add a comment
Know the answer?
Add Answer to:
arch tree for the words banana, peach, onut, mango, and papaya using alphabet- Build a binary...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT