Docsity

Advanced Data Structures: AVL Trees and Splay Trees, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Advanced Data Structures: AVL Trees and Splay Trees and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Advanced

Binary Search Trees

AVL Trees & Splay Trees

Binary Search Trees

(BST)

A Search Tree?
 A tree that maintains its data some sorted order and
supports efficient search operations.

By constraining the relative positions of the nodes in

the tree!

All data items are kept in sorted order!

A Binary Search Tree?

 A binary tree + A search tree

 An ordered (sorted) binary tree
 A special kind of binary tree with the ordering
condition

Between every node and the nodes in its left subtree.

Between every node and the nodes in its right subtree.

BST Order Property!

Properties of Binary Search
Trees
 What is the minimum number of nodes that a BST of
height h can have?

h

 What is the maximum number of nodes that a BST of
height h can have?

2

h

- 1

Properties of Binary Search

Trees

 N= The number of nodes in a binary search tree.
 h = The height of a binary search tree.

 h  N  2

h

log N  log (N+1)  h  N

Lower Bound: h =  (log N)

Upper Bound: h = O (N)

N

N

log N

N

BST Operations
 Insert
 Search/Retrieve
 Delete
Insert Operation
 Insert (BST, newitem)

 If BST == NULL (empty tree) then

 Create a new node; let BST point to this new node;copy newitem into new node’s

data portion; set the pointers in the new node to NULL.

 else if newitem.Key < BST->Key then

 Insert (BST->LchildPtr, newitem)

 else

 Insert (BST->RchildPtr, newitem)

 Worst case

O(N)

 Average Case

O(log N)

Delete Operation

J

B

L

R

Z

Q

Three cases:

  1. Node has no children

(leaf node)

  1. Node has one child
  2. Node has two children
Delete Operation

J

B

L

R

Z

Q

J

B

L

R

Z

3. Node has two children
Example: Delete Q by Merging

J

B

L

R

Z

Q

J

B

L

R

Z

Example: Delete Q by Merging

J

B

L

R

Z

Q

J

B L

R

Z

J

B R

Z L

Delete By Merging
 Delete by Merging can increase the height of BST
even though we delete a data item!
Delete Operation
 Delete by Copying

 By copying IOP (In-Order Predecessor)

 By copying IOS (In-Order Successor)

 Case 3 => Case 1 or Case 2

“Transform-and-Conquer”