Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
111 views64 pages

Splay Tree

Uploaded by

poudelshyam844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views64 pages

Splay Tree

Uploaded by

poudelshyam844
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

DATA STRUCTURES

UNIT 5
TREES

Dr. NAGARATHNA N
PROFESSOR
B.M.S. COLLEGE OF ENGINEERING
UNIT 5
Balanced Trees: AVL Trees, Splay trees, Red- Black
Trees - Definitions, Rotation and other basic operations.
Taxonomy of Searching Algorithms

Elementary searching algorithms


sequential search
binary search
binary tree search

Balanced tree searching


AVL trees
red-black trees
multiway balanced trees (2-3 trees, 2-3-4 trees, B trees)

Hashing
separate chaining
open addressing

3
Balanced trees: AVL trees
For every node, difference in height between left and right subtree is at most 1

AVL property is maintained through rotations, each time the tree becomes unbalanced

Disadvantage: needs extra storage for maintaining node balance

A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)

4
AVL Trees

DEFINITION: An AVL tree is a binary search tree in which the balance


factor of every node, which is defined as the difference between the
heights of the node’s left and right subtrees, is either 0 or +1 or −1.
AVL Trees
AVL trees were invented in 1962 by two Russian scientists, G. M. Adelson-Velsky and
E. M. Landis [Ade62], after whom this data structure is named.

a) AVL Tree b) Binary Search Tree that is not an AVL tree


The numbers above the nodes indicate the nodes’ balance factors
Example Insert
• Inserting in order 1,2,3,…,8
• Without self-adjustment

1
2 O(n) time for n Inserts
3
4
5
6
7
8
Balance factor
Algorithm maintains balance factor for each node. For example:

8
AVL tree rotations

Small examples:
1, 2, 3
3, 2, 1

9
AVL tree rotations
Small examples:
3, 1, 2
1, 3, 2

10
General case: single R-rotation

11
Double LR-rotation

The double right-left rotation (RL-rotation) is the mirror image of the double LR-rotation.

12
Imbalances: Four cases

1. LL Rotation: New node is inserted in the left subtree of the left subtree of the
critical node. (Perform right rotation)

2. RR Rotation: New node is inserted in the right subtree of the right subtree of the
critical node. (Perform left rotation)

3. LR Rotation: New node is inserted in the right subtree of the left subtree of the
critical node. (Left rotate w.r.t. the 1st child of the imbalanced node along the
path and then right rotate the critical node)

4. RL Rotation : New node is inserted in the left subtree of the right subtree of the
critical node. (Right rotate w.r.t. the 1st child of the imbalanced node along the
path and then left rotate the critical node)
Construction of AVL tree
Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive
insertions.
Construction of AVL tree
Construction of an AVL tree for the list 5, 6, 8, 3, 2, 4, 7 by successive
insertions.
AVL Tree
#include<stdio.h>
#include<stdlib.h>

// An AVL tree node


struct Node
{
int key;
struct Node *left;
struct Node *right;
int height;
};

https://www.geeksforgeeks.org/insertion-in-an-avl-tree/
AVL Tree
// Function to get the height of the tree
int height(struct Node *N)
{
if (N == NULL)
return 0;
return N->height;
}

// Function to get maximum of two integers


int max(int a, int b)
{
return (a > b)? a : b;
}
AVL Tree

/* Allocates a new node with the given key. */


struct Node* newNode(int key)
{
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
}
AVL Tree
/ Function to right rotate subtree rooted with y
struct Node *rightRotate(struct Node *y)
{
struct Node *x = y->left;
struct Node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left),height(y->right)) + 1;
x->height = max(height(x->left),height(x->right)) + 1;
// Return new root
return x;
}
AVL Tree
// Function to left rotate subtree rooted with x
struct Node *leftRotate(struct Node *x)
{
struct Node *y = x->right;
struct Node *T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left),height(x->right)) + 1;
y->height = max(height(y->left),height(y->right)) + 1;
// Return new root
return y;
}
AVL Tree
// Get Balance factor of node N
int getBalance(struct Node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
AVL Tree
// Recursive function to insert a key in the subtree rooted
// with node and returns the new root of the subtree.
struct Node* insert(struct Node* node, int key)
{
/* 1. Perform the normal BST insertion */
if (node == NULL)
return(newNode(key));

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;

/* 2. Update height of this ancestor node */


node->height = 1 + max(height(node->left),height(node->right));
AVL Tree
/* 3. Get the balance factor of this ancestor node to check
whether this node became unbalanced */
int balance = getBalance(node);

// If this node becomes unbalanced, then there are 4 cases

// Left Left Case


if (balance > 1 && key < node->left->key)
return rightRotate(node);

// Right Right Case


if (balance < -1 && key > node->right->key)
return leftRotate(node);
AVL Tree
// Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Case


if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}

/* return the (unchanged) node pointer */


return node;
}
AVL Tree
// Function to print preorder traversal of the tree.
// The function also prints height of every node
void preOrder(struct Node *root)
{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
AVL Tree
/* Driver program to test above function*/
int main()
{
struct Node *root = NULL;

/* Constructing tree given in the above figure */


root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
printf("Preorder traversal of the constructed AVL"
" tree is \n");
preOrder(root);

return 0;
}
AVL Tree

/* The constructed AVL Tree would be


30
/ \
20 40
/ \ \
10 25 50
*/
RED-BLACK Trees
Red-Black Trees
• “Balanced” binary search trees guarantee an O(lgn) running time
• Red-black-tree
– Binary search tree with an additional attribute for its nodes: color which
can be red or black
– Constrains the way nodes can be colored on any path from the root to a
leaf:

Ensures that no path is more than twice as long as any other


path ⇒ the tree is balanced

29
Red-Black Trees
• Definition: A red-black tree is a binary search tree where:
– Every node is either red or black.
– Each NIL pointer is considered to be a black node
– If a node is red, then both of its children are black.
– Every path from a node to a leaf contains the same number of black
nodes.
• The black-height of a node, n, in a red-black tree is the
number of black nodes on any path to a leaf, not counting n.

30
Example: RED-BLACK-TREE
2
6
1 4
7 1
NI NI
L L
3 4
0 7

NI 3 NI 5
L 8 L 0
NI NI NI NI
L L L L
• For convenience we use a sentinel NIL[T] to represent all
the NIL nodes at the leafs
– NIL[T] has the same fields as an ordinary node
– Color[NIL[T]] = BLACK
– The other fields may be set to arbitrary values
31
Black-Height of a Node
h=4
2 bh = 2
6
h=1 h=3
1 4
bh = 1 bh = 2
7 1
NI NI h=2
L L
3 h=2 4
bh = 1 bh = 1
0 h=1 7

NI 3 bh = 1NI 5 h=1
L 8 L 0 bh = 1
NI NI NI NI
L L L L
• Height of a node: the number of edges in the longest
path to a leaf
• Black-height of a node x: bh(x) is the number of
black nodes (including NIL) on the path from x to a leaf,
not counting x
32
A valid Red-Black Tree
Black-Height = 2

33
34
35
Red-Black-Trees Properties
(**Satisfy the binary search tree property**)
1. Every node is either red or black
2. The root is black
3. Every leaf (NIL) is black
4. If a node is red, then both its children are black
• No two consecutive red nodes on a simple path
from the root to a leaf
5. For each node, all paths from that node to descendant leaves
contain the same number of black nodes

36
INSERT
INSERT: what color to make the new node?
• Red? Let’s insert 35!
– Property 4 is violated: if a node is red, then both its
children are black
• Black? Let’s insert 14!
– Property 5 is violated: all paths from a node to its leaves
contain the same number of black nodes
2
6
1 4
7 1
3 4
0 7
3 5
8 0 37
Insertion
• Insert node as usual in BST
• Color the Node RED
• What Red-Black property may be violated?
– Every node is Red or Black
– Leaf nodes are Black NULLS
– If node is Red, both children must be Black
– Every path from node to descendant leaf must contain the same number
of Blacks

38
Bottom Up Insertion

• Insert node; Color it RED; X is pointer to it


• Cases
0: X is the root -- color it black
1: Both parent and uncle are red -- color parent and uncle black, color
grandparent red, point X to grandparent, check new situation
2 (zig-zag): Parent is red, but uncle is black. X and its parent are
opposite type children -- color grandparent red, color X black, rotate
left on parent, rotate right on grandparent
3 (zig-zig): Parent is red, but uncle is black. X and its parent are both
left or both right children -- color parent black, color grandparent red,
rotate right on grandparent

39
G

P X
U
G
X
P U

Case 1 – U is Red
Just Recolor and move up

40
G

P U

S X X

P G
Case 2 – Zig-Zag
Double Rotate
X around P; X around G S
Recolor G and X U
41
G

P U
S P
X

X G
Case 3 – Zig-Zig
Single Rotate P around G
Recolor P and G

S U
42
Problem
• Insert the following numbers (one by one) into a Red Black Tree.
Draw the tree after each insertion. Place an ‘R’ or circle red
nodes.
• 1,2,3,4,10,14,7,6,12
Answer
• Here is the final tree.
4
/ \
2R 10R
/ \ / \
1 3 7 14
/ /
6R 12R
Red-Black Trees

• Refer to the notes.

45
Splay Trees
Self adjusting Trees
• Ordinary binary search trees have no balance conditions

• Balanced trees like AVL trees enforce a balance condition when nodes change
› tree is always balanced after an insert or delete

• Self-adjusting trees get reorganized over time as nodes are accessed


› Tree adjusts after insert, delete, or find

2
Splay Trees
• Splay trees are tree structures that:
› Are not perfectly balanced all the time
› Data most recently accessed is near the root.
• The procedure:
› After node X is accessed, perform “splaying” operations to bring
X to the root of the tree.
› Do this in a way that leaves the tree more balanced as a whole

3
Splay Tree Terminology
• Let X be a non-root node with ≥ 2 ancestors.
› P is its parent node.
› G is its grandparent node.

G G G G

P P P P

X X X X

4
Zig-Zig and Zig-Zag
Parent and grandparent Parent and grandparent
in same direction. in different directions.

Zig-zig
4
G
G 5
P 5
1 P Zig-zag
X 2
X

5
Splay Tree Operations
1. Helpful if nodes contain a parent pointer.
parent
element
left right

2. When X is accessed, apply one of six rotation routines.


• Single Rotations (X has a P (the root) but no G)
ZigFromLeft, ZigFromRight
• Double Rotations (X has both a P and a G)
ZigZigFromLeft, ZigZigFromRight
ZigZagFromLeft, ZigZagFromRight
6
Zig at depth 1 (root)
• “Zig” is just a single rotation, as in an AVL tree
• Let R be the node that was accessed (e.g. using
Find)
root

ZigFromLeft

• ZigFromLeft moves R to the top →faster access


next time
7
Zig at depth 1
• Suppose Q is now accessed using Find
root

ZigFromRight

• ZigFromRight moves Q back to the top


8
Zig-Zag operation
• “Zig-Zag” consists of two rotations of the
opposite direction (assume R is the node that
was accessed)

(ZigFromRight) (ZigFromLeft)

ZigZagFromLeft

9
Zig-Zig operation
• “Zig-Zig” consists of two single rotations
of the same direction (R is the node that
was accessed)

(ZigFromLeft) (ZigFromLeft)

ZigZigFromLeft

55
Decreasing depth -
"autobalance"

Find(T) Find(R)

56
Splay Tree Insert and Delete
• Insert x
› Insert x as normal then splay x to root.
• Delete x
› Splay x to root and remove it. (note: the node does
not have to be a leaf or single child node like in
BST delete.) Two trees remain, right subtree and
left subtree.
› Splay the max in the left subtree to the root
› Attach the right subtree to the new root of the left
subtree.

57
With Self-Adjustment

1 1

ZigFromRight
2 1 2
2 1

3
3 2 ZigFromRight
2
1 3
1

58
With Self-Adjustment

4 3
4
2 4 ZigFromRight
3
1
2
1

Each Insert takes O(1) time therefore O(n) time for n Insert!!

59
Example Deletion
1
splay (Zig-Zag) 8
0
1 1
5 5
5 0
1 2 1
2 8 3 0 2 6 9 5
1 2
6 9 3 0
Splay (zig) remove
6
1 attach 1
5 5
0 0
1 1
2 9 2 6 9 5
5
1 2 1 2
3 0 3 0
60
Analysis of Splay Trees
• Splay trees have good “locality” properties
› Recently accessed items are near the root of the tree.
› Items near an accessed one are pulled toward the root.

61
Applications of Splay Trees

• Useful for problems with frequent repetitions of the same lookup


• Routing tables
• Memory caching and allocation

https://www.youtube.com/watch?v=D9BZk1giMws
Refence : Sartaj Sahni

• https://www.cise.ufl.edu/~sahni/cop5536/presentations.htm
Wishing you all the best!!

You might also like