Trees emphasis on Binary Search Trees - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Trees emphasis on Binary Search Trees

Description:

Sorted Collections. This is one implementation a sorted collection (recall the assignment on ... the collection contain this value. add add a new value in ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 9
Provided by: Steve57
Category:

less

Transcript and Presenter's Notes

Title: Trees emphasis on Binary Search Trees


1
Trees (emphasis on Binary Search Trees)
  • Reading 8.1 8.5
  • Motivation
  • search is a really important problem
  • binary search is an incredibly powerful tool to
    do it
  • Issues
  • binary search through an array is easy to
    implement
  • the main reason arrays work well is that it is
    easy to isolate the "left half" and "right half"
    of the array
  • but arrays have their problems with insertion,
    deletion, resizing, and so on
  • and these problems will be especially bad in
    binary search situations, since adds and removes
    will almost always be to the middle of the array,
    to keep it in sorted order
  • linked structures solve some of those problems,
    but you lose the ability to isolate portions of
    the list effectively
  • The solution
  • who says a LinkedList has to have a single next
    pointer?

2
Arrays and LinkedLists are "Linear" Structures
-4
-1
-1
62
63
900
1000
1000
-4
62
-1
-1
63
900
1000
1000
?
3
A (Binary) Search Tree Structure
63
-1
1000
-1
62
900
1000
?
?
?
?
?
?
?
-4
?
?
-4
-1
-1
62
63
900
1000
1000
4
Why Is This A Binary Search Tree?
  • It's a tree rather than a list, because a tree
    node has multiple "out" or "child" pointers

-4
6
1
-1
6
-4
?
?
?
?
5
Tree Not a Graph
  • It's a tree rather than a graph because there
    cannot be two paths to the same node (a node has
    at most one parent, and every node has a parent
    except for the root node

-1
6
-4
-1
?
?
?
?
6
-4
?
?
?
-1
6
-4
?
?
?
6
Binary Tree
  • It's a binary tree because every node has at most
    two children

-1
-1
6
-4
-4
?
6
7
?
?
?
?
?
?
?
7
?
?
7
Binary Search Tree
  • It is a search tree because every node in the
    tree has the following property
  • the node's data value is greater than or equal to
    the data value of every node in the left subtree
  • the node's data value is less than or equal to
    the data value of every node in the right subtree
  • (so how would you search for thevalue 62, or
    1001, in this tree?)

63
-1
1000
-1
62
900
1000
?
?
?
?
?
?
?
-4
?
?
8
Sorted Collections
  • This is one implementation a sorted collection
    (recall the assignment on SortedLinkedList)
    because all the elements in the list can be
    compared to each other (using compareTo), and the
    list is maintained in sorted order
  • why?
  • So the standard operations are
  • contains does the collection contain this value
  • add add a new value in order
  • remove remove the first (or all) occurrences of
    this value
  • iterator return an iterator that will produce
    all values in the collection in ascending order
  • Our task implement BinarySearchTree
  • where should we start?
Write a Comment
User Comments (0)
About PowerShow.com