Binary Search Trees CMSC 132 Chapter 8.1 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Binary Search Trees CMSC 132 Chapter 8.1

Description:

Books, etc., often talk about each node of a binary tree just containing a key. Search trees ... hard/tricky case. 7. Deleting the root of a binary search tree ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 15
Provided by: Phili254
Learn more at: https://www.cs.umd.edu
Category:

less

Transcript and Presenter's Notes

Title: Binary Search Trees CMSC 132 Chapter 8.1


1
Binary Search TreesCMSC 132Chapter 8.1
  • Nelson Padua-Perez
  • Bill Pugh

2
Binary Search Tree
3
Building maps with binary search trees
  • Books, etc., often talk about each node of a
    binary tree just containing a key
  • Search trees are often used to implement maps
  • each non-empty node contains a key, a value, and
    left and right subtrees
  • What the ?!?! is the generic type ltK extends
    ComparableltKgtgt
  • Denotes any type K that can be compared to Ks
  • e.g., Strings can be comparedTod Strings, but
    Strings cannot be compareTod Integer

4
Searching a binary tree for a value X
  • Is the tree empty?
  • if so, then X is not present
  • If the key at the root of the tree
  • equal to X
  • X is present
  • greater than X
  • check to see if X present in left subtree
  • less than X
  • check to see if X present in right subtree

5
Inserting X into a binary tree (Listing 8.4)
  • Define the insertion function to return a new
    tree
  • If tree is empty, return a new tree containing
    just X
  • If the key at the root of the tree
  • equal to X
  • X is already represent present, just return
    existing tree
  • greater than X
  • replace left subtree with result of inserting X
    into left subtree
  • return existing tree
  • less than X
  • replace right subtree with result of inserting X
    into right subtree
  • return existing tree

6
Deleting X into a binary tree (Listing 8.4)
  • Define the deletion function to return a new tree
  • If tree is empty, X isnt in tree, return empty
    tree
  • If the key at the root of the tree
  • greater than X
  • replace left subtree with result of deleting X
    from left subtree
  • return existing tree
  • less than X
  • replace right subtree with result of deleting X
    from right subtree
  • return existing tree
  • equal to X
  • hard/tricky case

7
Deleting the root of a binary search tree
  • If one of the subtrees is empty can just return
    the other subtree
  • Otherwise
  • find the maximum element from the left subtree
  • delete it from the left subtree
  • make new tree, with the maximum element from the
    left subtree

8
Deleting 23
23
35
17
52
20
27
5
3
13
9
Deleting 23 consider subtrees
23
35
17
52
20
27
5
3
13
10
Deleting maximum element from left subtree
35
20
17
52
27
5
3
13
11
Form new tree, from resulting left subtree and
original right subtree
20
35
17
52
27
5
3
13
12
Binary search tree project
  • Project 6 has been posted to your linuxlab
    repository
  • Recursive, polymorphic binary search tree
  • used to implement a map
  • What do we mean by polymorphic?
  • implement two subtypes of Tree EmptyTree and
    NonEmptyTree
  • We use EmptyTree, rather than null to represent
    the empty tree
  • Invoke methods on tree nodes, get empty or
    nonempty functionality

13
Recursive, polymorphic linked lists
  • Give you code for linked lists that uses the same
    concept
  • Keys are kept in sorted order
  • Have an interface List, and subtypes EmptyList
    and NonEmptyList

14
compareTo
  • I always have to look up which way this works
  • Invoke a.compareTo(b)
  • if a.compareTo(b) 0, then a b
  • if a.compareTo(b) lt 0, then a lt b
  • if a.compareTo(b) gt 0, then a gt b
Write a Comment
User Comments (0)
About PowerShow.com