631-14
TRANSCRIPT
-
8/3/2019 631-14
1/24
By Dawn J. LawrieLoyola College in Maryland
Lecture 14
Binary Search Trees
-
8/3/2019 631-14
2/24
2
The ADT Binary Search Tree
A deficiency of the ADT binary tree whichis 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
-
8/3/2019 631-14
3/24
3
The ADT Binary Search Tree
A data item in a binary search tree has aspecially designated search key
A search key is the part of a record thatidentifies it within a collection of records
KeyedItem class
Contains the search key as a data field and a
function for accessing the search key
-
8/3/2019 631-14
4/24
4
The ADT Binary Search Tree
Figure 10.18 UML diagram for the class BinarySearchTree
-
8/3/2019 631-14
5/24
5
Algorithms for the ADT Binary
Search Tree Operations A recursive search function
Searches the binary search tree bintree forthe item whose search key is searchKey
Searches recursively in the right or left subtree(depending on the value of the item) untilsearchKeymatches the search key of the nodesitem
search(in binTree:BinarySearchTree,in searchKey:KeyType);
-
8/3/2019 631-14
6/24
6
ADT Binary Search Tree: Insertion
Inserts newItem into the binary searchtree 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
-
8/3/2019 631-14
7/24
7
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
-
8/3/2019 631-14
8/24
8
Exercise
Beginning with an empty binary searchtree, what binary search tree is formedwhen you insert the following values in the
order given: J, N, B, A, W, E, T?
-
8/3/2019 631-14
9/24
9
ADT Binary Search Tree: Deletion
Three possible cases for deleting node N
Nis a leaf Set the pointer in Ns parent to NULL
Nhas only one child
Let Ns parent adopt Ns child
-
8/3/2019 631-14
10/24
10
ADT Binary Search Tree: Deletion
Three possible cases for deleting node N(continued)
Nhas two children
Locate another node Mthat is the leftmost node inNsright subtree
Mssearch keyis called the inorder successor of Nssearch key
Copy the item that is in Mto N Remove the node Mfrom the tree
-
8/3/2019 631-14
11/24
-
8/3/2019 631-14
12/24
12
ADT Binary Search Tree: Retrieval and
Traversal
The retrieval operation can beimplemented by refining the search
algorithm
Return the item with the desired search key ifit exists
Otherwise, throw TreeException
Traversals for a binary search tree are thesame as the traversals for a binary tree
-
8/3/2019 631-14
13/24
13
ADT Binary Search Tree
Theorem 10-1The inorder traversal of a binary searchtree Twill visit its nodes in sorted search-
key 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 abinary tree of height h can have is 2h 1
-
8/3/2019 631-14
14/24
14
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 areperformed
-
8/3/2019 631-14
15/24
15
The Efficiency of Binary Search
Tree Operations
Figure 10.32 Counting the nodes in a full binary tree of height h
-
8/3/2019 631-14
16/24
-
8/3/2019 631-14
17/24
17
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 numberof nodes in the tree is known
-
8/3/2019 631-14
18/24
18
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 thefirst and one past the last occurrence of avalue
-
8/3/2019 631-14
19/24
19
General Trees
An n-ary tree
A generalization of a binary tree whosenodes each can have no more than n
children
Figure 10.38A general tree
Figure 10. 41
An implementation of the n-ary tree in Figure 10.38
-
8/3/2019 631-14
20/24
20
Summary
Binary trees provide a hierarchicalorganization of data
The implementation of a binary tree isusually pointer-based
A client-defined visit function to thetraversal operation can customize theoperations on the items of the tree
-
8/3/2019 631-14
21/24
21
Summary
The binary search tree allows you to use abinary search-like algorithm to search foran 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 determinesthe efficiency of its operations
-
8/3/2019 631-14
22/24
22
Summary
An inorder traversal of a binary search treevisits the trees nodes in sorted search-keyorder
The treesortalgorithm efficiently sorts anarray by using the binary search trees
insertion and traversal operations
-
8/3/2019 631-14
23/24
23
Summary
Saving a binary search tree to a file
To restore the tree as a binary search tree ofminimum height
Perform inorder traversal while saving the tree to afile
To restore the tree to its original form
Perform preorder traversal while saving the tree toa file
-
8/3/2019 631-14
24/24
24
Final Review
Exam is cumulative
This will be a 2 hoursexam
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