You are on page 1of 24

# Binary Search Trees

Lecture 14

## The ADT Binary Search Tree

A deficiency of the ADT binary tree which is corrected by the ADT binary search tree
Searching for a particular item

Record
A group of related items, called fields, that are not necessarily of the same data type

Field
A data element within a record

## The ADT Binary Search Tree

A data item in a binary search tree has a specially designated search key
A search key is the part of a record that identifies it within a collection of records

KeyedItem class
Contains the search key as a data field and a function for accessing the search key

4

## A recursive search function

Searches the binary search tree bintree for the item whose search key is searchKey Searches recursively in the right or left subtree (depending on the value of the item) until searchKey matches the search key of the nodes item
search(in binTree:BinarySearchTree, in searchKey:KeyType);

## ADT Binary Search Tree: Insertion

Inserts newItem into the binary search tree to which treePtr points
insertItem(inout treePtr:TreeNodePtr, in newItem:TreeItemType)

To copy a tree
Traverse it in preorder and insert each item visited into a new tree

## ADT Binary Search Tree: Insertion

Figure 10.23 (a) Insertion into an empty tree; (b) search terminates at a leaf; (c) insertion at a leaf

Exercise

Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the order given: J, N, B, A, W, E, T?

## Three possible cases for deleting node N

N is a leaf
Set the pointer in Ns parent to NULL

## N has only one child

Let Ns parent adopt Ns child

## Three possible cases for deleting node N (continued)

N has two children
Locate another node M that is the leftmost node in Ns right subtree
Ms search key is called the inorder successor of Ns search key

Copy the item that is in M to N Remove the node M from the tree

10

Exercise
M G D A H K L U R V T W

11

## The retrieval operation can be implemented by refining the search algorithm

Return the item with the desired search key if it exists Otherwise, throw TreeException

Traversals for a binary search tree are the same as the traversals for a binary tree
12

Theorem 10-1
The inorder traversal of a binary search tree T will visit its nodes in sorted searchkey order

Theorem 10-2
A full binary tree of height h 0 has 2h 1 nodes

Theorem 10-3
The maximum number of nodes that a binary tree of height h can have is 2h 1
13

## The Efficiency of Binary Search Tree Operations

Theorem 10-4
The minimum height of a binary tree with n nodes is log2(n+1)

The height of a particular binary search tree depends on the order in which insertion and deletion operations are performed

14

## Figure 10.32 Counting the nodes in a full binary tree of height h

15

Applications

Treesort
Uses the ADT binary search tree to sort an array of records into search-key order
Average case: O(n * log n) Worst case: O(n2)

16

Applications

## Algorithms for saving a binary search tree

Saving a binary search tree and then restoring it to its original shape
Uses preorder traversal to save the tree to a file

## Saving a binary tree and then restoring it to a balanced shape

Uses inorder traversal to save the tree to a file Can be used if the data is sorted and the number of nodes in the tree is known

17

## The STL Search Algorithms for Sorted Ranges

binary_search
Returns true if a specified value appears in the sorted range

lower_bound; upper_bound
Returns an iterator to the first occurrence; or to one past the last occurrence of a value

equal_range
Returns a pair of iterators that indicate the first and one past the last occurrence of a value
18

General Trees

An n-ary tree
A generalization of a binary tree whose nodes each can have no more than n children

Figure 10. 41 Figure 10.38 A general tree An implementation of the n-ary tree in Figure 10.38
19

Summary
Binary trees provide a hierarchical organization of data The implementation of a binary tree is usually pointer-based A client-defined visit function to the traversal operation can customize the operations on the items of the tree

20

Summary
The binary search tree allows you to use a binary search-like algorithm to search for an item with a specified value Binary search trees come in many shapes

The height of a binary search tree with n nodes can range from a minimum of log2(n + 1) to a maximum of n The shape of a binary search tree determines the efficiency of its operations

21

Summary
An inorder traversal of a binary search tree visits the trees nodes in sorted search-key order The treesort algorithm efficiently sorts an array by using the binary search trees insertion and traversal operations

22

Summary

## Saving a binary search tree to a file

To restore the tree as a binary search tree of minimum height
Perform inorder traversal while saving the tree to a file

## To restore the tree to its original form

Perform preorder traversal while saving the tree to a file

23

Final Review

## Exam is cumulative This will be a 2 hours exam Topics

Binary Search Trees Trees Big-O notation Sorting Inheritance

More Topics
Linear Data Structures Manipulating Linked Lists Allocating/Deallocating memory Memory Layout and the runtime stack Exceptions Recursion
24