Binary Search Trees - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Binary Search Trees

Description:

a binary search tree (BST) is a binary tree with an ordering property of its elements: ... special about the order of the data in the BST, for each traversal? ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 22
Provided by: jasonm6
Category:
Tags: binary | bst | search | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Topic 13
  • Binary Search Trees

2
Chapter 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

4
Binary 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?

5
Binary 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

6
Search Operation
  • examples

7
Add 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

8
Adding Elements to a Binary Search Tree
9
Remove Operation
  • what happens when you remove an element from a
    BST?

10
Removing elements from a binary search tree
11
Removing the minimum element from a binary search
tree
12
Binary 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

13
Operations on a Binary Search Tree
14
UML description of the BinarySearchTreeADT
15
Implementing 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

16
BST Implementation
  • addElement
  • removeElement
  • removeAllOccurrences
  • removeMin
  • the rest are left as exercises

17
Using Binary Search Trees Implementing Ordered
Lists
  • analysis of the BinarySearchTreeOrderedList
    implementation

18
The common operations on a list
19
The operation particular to an ordered list
20
Analysis of linked list and binary search tree
implementations of an ordered list
21
A degenerate binary tree
Write a Comment
User Comments (0)
About PowerShow.com