Binary Search Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Binary Search Trees

Description:

Binary Search Trees Chapter 9 Binary Search Trees A binary tree that is built such that every node's left subtree has key values less than the node's key value, and ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 13
Provided by: ComputerS94
Learn more at: https://cse.buffalo.edu
Category:
Tags: binary | search | tree | trees

less

Transcript and Presenter's Notes

Title: Binary Search Trees


1
Binary Search Trees
  • Chapter 9

2
Binary Search Trees
  • A binary tree that is built such that
  • every node's left subtree has key values less
    than the node's key value, and the nodes right
    subtree has key values greater than the nodes
    key value. A new node is added as a leaf.
  • Above is NISTs (National Institute of Standards
    and Technology) definition
  • We will add a constraint that the key will be
    unique.

3
Binary Search Tree
  • We will define the operations of binary search
    tree.
  • We will use the simple binary tree that we
    designed earlier.
  • The semantics of the operation are loosely based
    on Chapter 9s discussion on binary search trees.

4
BST server side
5
BST Client side
6
(No Transcript)
7
Insert, and Search methods
  • Insert (key, element)
  • (Non_empty tree) Locate position (cursor) to
    insert. This will be parent of new node
    (key-element pair) to be inserted.
  • 1.1 If key lt cursors key, insert as left leaf
  • 1.2 Else if key gtcursors key, insert as right
    leaf
  • 1.3 Else replace value of element with new value.
  • (Empty tree) Create a tree with node with an item
    made up of key,element.

8
Search(key)
  • Locate the node with the given key.
  • 1.1 If one exists return element of the node
  • 1.2 Else return null.
  • Both insert and search method use a utility
    method
  • boolean locate(Key k, Btree b)
  • That return true, if key is found and sets a
    cursor to the location where it was found.
  • Returns false if not found, cursor has the
    location of parent for insert.

9
Object remove(Comparable key)
  • Empty case throw exception
  • NonEmpty case locate the key in the tree
  • found key at cursor
  • 2.1 Left sub tree of cursor empty set the
    cursors parents left or right sub tree with
    cursors right sub tree.
  • 2.2 Left sub tree nonempty right sub tree
    empty or non empty
  • 2.2.1 find the rightmost node
  • 2.2.2 replace cursor data with
    rightmost nodes data
  • 2.2.3 remove rightmost node
  • return Item found at the node
  • Not found Throw exception

10
Examples
  • Lets look at some representative examples using
    the animation tool.

11
(No Transcript)
12
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com