@BiancaGando

Intro to

Binary Search

Trees

Updated 1/18/2017

@BiancaGando

  1. Parent/child relations
  2. Each node can have n children
  3. Sequential access

Trees, a review

  1. Same as n-ary, plus:
  2. Each node can have a max of 2 children

N-ary Tree

Binary Tree

  1.  Same as binary tree above, plus:
  2. Each node is comparable 
  3. Each node's Left subtree < parent value and
  4. Right subtree > parent value

 

Binary Search Tree

@BiancaGando

BST

@BiancaGando

Interface

Constructor


Methods

//Constructor

//Methods
  //insert

  //contains

Exercise Time!

@BiancaGando

@BiancaGando

Fast

Value lookup by key

 

Slow

Inserting new values

Deleting values

Performance

Fast (usually)

Value lookup by key

Inserting new values

Deleting values

Sorted Array

BST

@BiancaGando

  1. Priority Queues
  2. Twenty Questions

Binary Search Trees

@BiancaGando

Implementing

BST

@BiancaGando

Pseudocode

//Constructor

//Methods
  //insert

  //contains

@BiancaGando

Exploring

BSTs

in-order, preorder,

post order

@BiancaGando

Linked List

Array

Object

Stack/Queue

How do we explore the following data structures?

@BiancaGando

In-Order Traversal

@BiancaGando

In-Order Traversal

//Code here

@BiancaGando

Pre-Order Traversal

@BiancaGando

Post-Order Traversal

Exercise Time!

@BiancaGando

@BiancaGando

 

Deleting Nodes

from BST

@BiancaGando

1. Go left until the left is null

2. What do we do when the min or max has a child node?

Delete Min/Max

//Pseudocode!

@BiancaGando

3 Cases:

1. When the node is a leaf node

 

2. When the node has 1 child

 

3. When the node as 2 children

Deleting a node from BST

@BiancaGando

Delete Node

Case 1: a node with no children

@BiancaGando

Delete Node

Case 2: a node with 1 child

Exercise Time!

@BiancaGando

@BiancaGando

Pseudocode

//Code here

@BiancaGando

Delete Node

Case 3: a node with 2 children

@BiancaGando

Binary Search Tree

Analysis

of Time Complexity

@BiancaGando

AVL Tree

Red-Black Tree

Splay Tree

Balanced BSTs