Binary search trees
This presentation is the property of its rightful owner.
Sponsored Links
1 / 23

Binary Search Trees PowerPoint PPT Presentation


  • 102 Views
  • Uploaded on
  • Presentation posted in: General

Binary Search Trees. Eileen Kraemer CSCI 2720 The University of Georgia Spring 2007. Overview. Binary trees Binary search trees Find Insert Delete. Hierarchical Data Structures. One-to-many relationship between elements Tree Single parent, multiple children Binary tree

Download Presentation

Binary Search Trees

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Binary search trees

Binary Search Trees

Eileen Kraemer

CSCI 2720

The University of Georgia

Spring 2007


Overview

Overview

  • Binary trees

  • Binary search trees

    • Find

    • Insert

    • Delete


Hierarchical data structures

Hierarchical Data Structures

  • One-to-many relationship between elements

  • Tree

    • Single parent, multiple children

  • Binary tree

    • Tree with 0–2 children per node

Tree

Binary Tree


Trees

Trees

  • Terminology

    • Root no predecessor

    • Leaf no successor

    • Interior non-leaf

    • Height distance from root to leaf

Root node

Height

Interior nodes

Leaf nodes


Types of binary trees

Types of Binary Trees

  • Degenerate – only one child

  • Balanced – mostly two children

  • Complete – always two children

Degenerate binary tree

Balanced binary tree

Complete binary tree


Binary trees properties

Degenerate

Height = O(n) for n nodes

Similar to linear list

Balanced

Height = O( log(n) ) for n nodes

Useful for searches

Binary Trees Properties

Degenerate binary tree

Balanced binary tree


Binary search trees1

Binary Search Trees

  • Key property

    • Value at node

      • Smaller values in left subtree

      • Larger values in right subtree

    • Example

      • X > Y

      • X < Z

X

Y

Z


Binary search trees2

Binary Search Trees

  • Examples

5

10

10

2

45

5

30

5

45

30

2

25

45

2

25

30

10

25

Binary search trees

Non-binary search tree


Binary tree implementation

Binary Tree Implementation

Class Node {

Value data;

Node left, right; // null if empty

void insert ( Value data1 ) { … }

void delete ( Value data2 ) { … }

Node find ( Value data3 ) { … }

}


Polymorphic binary tree implement

Polymorphic Binary Tree Implement.

Interface Node {

Node insert ( Value data1 ) { … }

}

Class EmptyNode {

Node insert ( Value data1 ) { … }

}

Class NonEmptyNode {

Value data;

Node left, right; // Either Empty or NonEmpty

void insert ( Value data1 ) { … }

}


Iterative search of binary tree

Iterative Search of Binary Tree

Node Find( Node n, Value key) {

while (n != null) {

if (n.data == key) // Found it

return n;

if (n.data > key) // In left subtree

n = n.left;

else // In right subtree

n = n.right;

}

return null;

}

Find( root,key_val );


Recursive search of binary tree

Recursive Search of Binary Tree

Node Find( Node n, Value key) {

if (n == null) // Not found

return( n );

elseif (n.data == key) // Found it

return( n );

else if (n.data > key) // In left subtree

return Find( n.left );

else // In right subtree

return Find( n.right );

}

Find( root );


Example binary searches

Example Binary Searches

  • Find ( 2 )

5

10

10 > 2, left

5 > 2, left

2 = 2, found

5 > 2, left

2 = 2, found

2

45

5

30

30

2

25

45

10

25


Example binary searches1

Example Binary Searches

  • Find ( 25 )

5

10

10 < 25, right

30 > 25, left

25 = 25, found

5 < 25, right

45 > 25, left

30 > 25, left

10 < 25, right

25 = 25, found

2

45

5

30

30

2

25

45

10

25


Binary search properties

Binary Search Properties

  • Time of search

    • Proportional to height of tree

    • Balanced binary tree

      • O( log(n) ) time

    • Degenerate tree

      • O( n ) time

      • Like searching linked list / unsorted array

  • Requires

    • Ability to compare key values


Binary search tree construction

Binary Search Tree Construction

  • How to build & maintain binary trees?

    • Insertion

    • Deletion

  • Maintain key property (invariant)

    • Smaller values in left subtree

    • Larger values in right subtree


Binary search tree insertion

Binary Search Tree – Insertion

  • Algorithm

    • Perform search for value X

    • Search will end at node Y (if X not in tree)

    • If X < Y, insert new leaf X as new left subtree for Y

    • If X > Y, insert new leaf X as new right subtree for Y

  • Observations

    • O( log(n) ) operation for balanced tree

    • Insertions may unbalance tree


Example insertion

Example Insertion

  • Insert ( 20 )

10

10 < 20, right

30 > 20, left

25 > 20, left

Insert 20 on left

5

30

2

25

45

20


Binary search tree deletion

Binary Search Tree – Deletion

  • Algorithm

    • Perform search for value X

    • If X is a leaf, delete X

    • Else // must delete internal node

      • a) Replace with largest value Y on left subtree OR smallest value Z on right subtree

      • b) Delete replacement value (Y or Z) from subtree

  • Observation

    • O( log(n) ) operation for balanced tree

    • Deletions may unbalance tree


Example deletion leaf

Example Deletion (Leaf)

  • Delete ( 25 )

10

10

10 < 25, right

30 > 25, left

25 = 25, delete

5

30

5

30

2

25

45

2

45


Example deletion internal node

Example Deletion (Internal Node)

  • Delete ( 10 )

10

5

5

5

30

5

30

2

30

2

25

45

2

25

45

2

25

45

Replacing 10 with largest value in left subtree

Replacing 5 with largest value in left subtree

Deleting leaf


Example deletion internal node1

Example Deletion (Internal Node)

  • Delete ( 10 )

10

25

25

5

30

5

30

5

30

2

25

45

2

25

45

2

45

Replacing 10 with smallest value in right subtree

Deleting leaf

Resulting tree


Still need to talk about

Still need to talk about …

  • Static BSTs (we looked at some example)

  • Optimal BSTs

  • Probability-balanced trees

  • Median split trees

    • but in the meantime, let’s jump forward to AVL trees.


  • Login