Binary Search Trees - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Binary Search Trees

Description:

A Binary Search Tree (BST) is a special case of the generic binary tree. ... You can decide that your BST will allow duplicates, but if you do, it complicates matters. ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 7
Provided by: cseBu
Category:
Tags: binary | bst | search | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Binary Search Trees
2
What are they?
  • A Binary Search Tree (BST) is a special case of
    the generic binary tree.
  • A Binary Search Tree is kept in sorted order,
    so that a value can be quickly found in the tree.
  • An in-order traversal of the BST will return the
    elements in sorted order.

3
Sorted Data
  • A BST has to comply with the following rule All
    elements in the left subtree of any node in the
    tree must be less than that node, and all
    elements in the right subtree must be greater.
  • In order to make this judgment, the items in a
    BST must be Comparable.

4
What about duplicates?
  • You can decide that your BST will allow
    duplicates, but if you do, it complicates
    matters. You must decide to consistently treat
    duplicates as either greater than, or less than,
    their duplicate value.
  • You must also come up with a strategy for
    deleting duplicate values. Do you delete them
    all? Only the first?

5
BST Operations
  • public Comparable add(Comparable c)
  • public Comparable remove(Comparable c)
  • public boolean contains(Comparable c)
  • public Comparable getEntry(Comparable c)
  • public boolean isEmpty()
  • public Iterator iterator()

6
Efficiency of Operations
  • When a tree is balanced, finding a node takes
    O(log n) time. However, at the worst case of an
    unbalanced tree, it can take O(n) time.
  • When a tree is static, you can get good
    performance, but dynamic additions and removals
    can mess with a well balanced tree.
  • Tips never add nodes in a sorted order. The
    ideal way is to add from the middle, but a random
    ordering is actually a pretty good way to get a
    tree set up.
Write a Comment
User Comments (0)
About PowerShow.com