Title: Binary Search Trees
1Topic 13
2Chapter Objectives
- define a binary search tree abstract data
structure - demonstrate how a binary search tree can be used
to solve problems - examine various binary search tree
implementations - compare binary search tree implementations
3 Binary Search Tree
- a binary search tree (BST) is a binary tree with
an ordering property of its elements - for each node, its data element is greater than
that of its left child and less than or equal to
that of its right child - example
4Binary Search Tree (contd)
- so, the data in any node is
- greater than the data in any node in its left
subtree - less than or equal to the data in any node in its
right subtree - examples are these BSTs?
5Binary Search Tree Operations
- why is it called a binary search tree?
- algorithm to search for an item in the tree
- compare data item to the root of the (sub)tree
- if data item data at root, found
- if data item lt data at root, go to the left if
there is no left child, data item is not in tree - if data item gt data at root, go to the right if
there is no right child, data item is not in tree
6Search Operation
7Add Operation
- adding an element to a BST requires that the BST
property be maintained! - algorithm
- follow the algorithm for searching, until there
is no child - insert at that point
- examples
8Adding Elements to a Binary Search Tree
9Remove Operation
- what happens when you remove an element from a
BST?
10Removing elements from a binary search tree
11Removing the minimum element from a binary search
tree
12Binary Search Tree Traversals
- consider the traversals of a binary search tree
- preorder
- inorder
- postorder
- level-order
- is there anything special about the order of the
data in the BST, for each traversal? - examples
13Operations on a Binary Search Tree
14UML description of the BinarySearchTreeADT
15Implementing Binary Search Trees
- a BST is just a binary tree with the ordering
property imposed on all nodes in the tree - so, we can extend the BinaryTree class
16BST Implementation
- addElement
- removeElement
- removeAllOccurrences
- removeMin
- the rest are left as exercises
17Using Binary Search Trees Implementing Ordered
Lists
- analysis of the BinarySearchTreeOrderedList
implementation
18The common operations on a list
19The operation particular to an ordered list
20Analysis of linked list and binary search tree
implementations of an ordered list
21A degenerate binary tree