Binary Search Trees: Properties, Search, and Operations, Exams of Data Structures and Algorithms

The concept of binary search trees, their properties, and various operations such as search, insertion, and removal. It provides a step-by-step guide on how to implement these operations in c. Students will learn how binary search trees maintain sorted order and how they can be used to efficiently store and access data.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-xs4-1
koofers-user-xs4-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
worksheet 18: Binary Search Trees Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 18: Binary Search Trees
A
binary search tree
is a binary tree that has the following additional property: for
each node, the values in all descendants to the left of the node are less than or
equal to the value of the node, and the values in all descendants to the right are
greater than or equal. The following is an example binary search tree:
Notice that an inorder traversal of a BST will list the elements in sorted order. The
most important feature of a binary search tree is that operations can be
performed by walking the tree from the top (the root) to the bottom (the leaf). This
means that a BST can be used to produce a fast Bag implementation. For
example, suppose you find out if the name “Harry” is found in the tree shown.
You simply compare the value to the root (Juliet). Since Harry comes before
Juliet, you travel down the left child. Next you compare “Harry” to “Eve”. Since it
is larger, you travel down the right. Finally you find a node that matches the value
you are searching, and so you know it is in the collection. If you find a null pointer
along the path, as you would if you were searching for “Sam”, you would know
the value was not in the collection.
pf3
pf4
pf5

Partial preview of the text

Download Binary Search Trees: Properties, Search, and Operations and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 18: Binary Search Trees

A binary search tree is a binary tree that has the following additional property: for each node, the values in all descendants to the left of the node are less than or equal to the value of the node, and the values in all descendants to the right are greater than or equal. The following is an example binary search tree: Notice that an inorder traversal of a BST will list the elements in sorted order. The most important feature of a binary search tree is that operations can be performed by walking the tree from the top (the root) to the bottom (the leaf). This means that a BST can be used to produce a fast Bag implementation. For example, suppose you find out if the name “Harry” is found in the tree shown. You simply compare the value to the root (Juliet). Since Harry comes before Juliet, you travel down the left child. Next you compare “Harry” to “Eve”. Since it is larger, you travel down the right. Finally you find a node that matches the value you are searching, and so you know it is in the collection. If you find a null pointer along the path, as you would if you were searching for “Sam”, you would know the value was not in the collection.

Adding a value to a binary search tree is easy. You simply perform the same type of traversal as described above, and when you find a null value you insert a new node. Try inserting the values “Raymond”. Then try inserting “Sam”. Insertion is most easily accomplished by writing a private internal function that takes a Node and a value, and returns the new tree in which the Node has been inserted. In pseudo-code this routine is similar to the following: Node add (Node start, E newValue) if start is null then return a new Node with newValue otherwise if newValue is less than the value at start then set the left child to be the value returned by add(leftChild, newValue) otherwise set the right child to be add(rightChild, newValue) return the current node Removal is the most complex of the basic Bag operations. The difficulty is that removing a node leaves a “hole”. Imagine, for example, removing the value “Juliet” from the tree shown. What value should be used in place of the removed element? The answer is the leftmost child of the right node. This is because it is this value that is the smallest element in the right subtree. The leftmost child of a node is the value found by running through left child Nodes as far as possible. The leftmost child of the original tree shown above is “Adam”. The leftmost child of the right child of the node “Juliet” is the node “Romeo”. It is a simple matter to write a routine to find the value of the leftmost child of a node. You should verify that in each case if you remove an element the value of the node can be replaced by the leftmost child of the right node without destroying the BST property. A companion routine (removeLeftmost) is a function to return a tree with the leftmost child removed. Again, traverse the tree until the leftmost child is found. When found, return the right child (which could possibly be null). Otherwise as you return through the Nodes set the left child to the value returned by the recursive call, and return the current Node. Armed with these two routines, the general remove operation can be described as follows. Again it makes sense to write it as a recursive routine that returns the new tree with the value removed. Node remove (Node start, E testValue) if start.value is the value we seek decrease the value of dataSize if right child is null

void BSTremove (struct binarySearchTree *tree, EleType d) { if (BSTcontains(tree, d) { tree->root = BSTnodeRemove(tree->root, d); tree->size--; } } EleType leftMostChild (struct node * current) { } struct node * removeLeftmostChild (struct node *current) { } void BSTnodeRemove (struct node * current, EleType d) { }

On Your Own

  1. What is the primary characteristic of a binary search tree?
  2. Explain how the search for an element in a binary search tree is an example of the idea of divide and conquer.
  3. Try inserting the values 1 to 10 in order into a BST. What is the height of the resulting tree?
  4. Why is it important that a binary search tree remain reasonably balanced? What can happen if the tree becomes unbalanced?
  5. What is the maximum height of a BST that contains 100 elements? What is the minimum height?
  6. Explain why removing a value from a BST is more complicated than insertion.
  7. Suppose you want to test our BST algorithms. What would be some good boundary value test cases?
  8. Program a test driver for the BST algorithm and execute the operations using the test cases identified in the previous question.
  9. The smallest element in a binary search tree is always found as the leftmost child of the root. Write a method getFirst to return this value, and a method removeFirst to modify the tree so as to remove this value.
  10. With the methods described in the previous question, it is easy to create a data structure that stores values in a BST and implements the Priority Queue interface. Show this implementation, and describe the algorithmic execution time for each of the Priority Queue operations.
  11. Suppose you wanted to add the equals method to our BST class, where two trees are considered to be equal if they have the same elements. What is the complexity of your operation? As long as the tree remains relatively well balanced, the addition of values to a binary search tree is very fast. This can be seen in the execution timings shown below. Here the time required to place n random values into a collection is compared to a Skip List, which had the fastest execution times we have seen so far.