cs-co-412 ads 10 trees 3 - height-balanced trees.pptx

66
Algorithms and Data Structures 1 Copyright © D. Vernon 2014 Algorithms and Data Structures CS-CO-412 David Vernon Professor of Informatics University of Skövde Sweden [email protected] www.vernon.eu Algorithms and Data Structures 2 Copyright © D. Vernon 2014 Trees Lecture 10

Upload: dinhhanh

Post on 14-Feb-2017

226 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 1 Copyright © D. Vernon 2014

Algorithms and Data Structures CS-CO-412

David Vernon Professor of Informatics

University of Skövde Sweden

[email protected] www.vernon.eu

Algorithms and Data Structures 2 Copyright © D. Vernon 2014

Trees

Lecture  10    

Page 2: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 3 Copyright © D. Vernon 2014

Topic Overview

•  Types of trees •  Binary Tree ADT •  Binary Search Tree •  Optimal Code Trees •  Huffman’s Algorithm •  Height Balanced Trees

–  AVL Trees –  Red-Black Trees

Algorithms and Data Structures 4 Copyright © D. Vernon 2014

Height-Balanced Trees

AVL Trees

Page 3: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 5 Copyright © D. Vernon 2014

AVL Trees

•  We know from our study of Binary Search Trees (BST) that the average search and insertion time is O(log n)

–  If there are n nodes in the binary tree it will take, on average, log2n comparisons/probes to find a particular node (or find out that it isn’t there)

•  However, this is only true if the tree is ‘balanced’

–  Such as occurs when the elements are inserted in random order

Algorithms and Data Structures 6 Copyright © D. Vernon 2014

AVL Trees

JULY

FEB MAY

AUG JAN MAR OCT

APR DEC JUN NOV SEPT

A  Balanced  Tree  for  the  Months  of  the  Year  

Page 4: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 7 Copyright © D. Vernon 2014

AVL Trees

•  However, if the elements are inserted in lexicographic order (i.e. in sorted order) then the tree degenerates into a skinny tree

Algorithms and Data Structures 8 Copyright © D. Vernon 2014

AVL Trees

JULY

FEB

MAY

AUG

JAN

MAR

OCT

APR

DEC

JUN

NOV

SEPT

A  Degenerate  Tree  for  the  Months  of  the  Year  

Page 5: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 9 Copyright © D. Vernon 2014

AVL Trees

•  If we are dealing with a dynamic tree

•  Nodes are being inserted and deleted over time

–  For example, directory of files –  For example, index of university students

•  we may need to restructure - balance - the tree so that we keep it –  Fat –  Full –  Complete

Algorithms and Data Structures 10 Copyright © D. Vernon 2014

AVL Trees

•  Adelson-Velskii and Landis in 1962 introduced a binary tree structure that is balanced with respect to the heights of its subtrees

•  Insertions (and deletions) are made such that the tree –  starts off –  and remains

•  Height-Balanced

Page 6: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 11 Copyright © D. Vernon 2014

AVL Trees

•  Definition of AVL Tree

•  An empty tree is height-balanced

•  If T is a non-empty binary tree with left and right sub-trees T1 and T2, then

T is height-balanced iff

–  T1 and T2 are height-balanced, and –  |height(T1) - height(T2)| ≤ 1

Algorithms and Data Structures 12 Copyright © D. Vernon 2014

AVL Trees

•  So, every sub-tree in a height-balanced tree is also height-balanced

Page 7: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 13 Copyright © D. Vernon 2014

Recall: Binary Tree Terminology

•  The height of T is defined recursively as

0 if T is empty and

1 + max(height(T1), height(T2)) otherwise, where T1 and T2 are the subtrees of the root.

•  The height of a tree is the length of a longest chain of descendents

Algorithms and Data Structures 14 Copyright © D. Vernon 2014

Recall: Binary Tree Terminology

•  Height Numbering –  Number all external nodes 0 –  Number each internal node to be one more than the maximum

of the numbers of its children –  Then the number of the root is the height of T

•  The height of a node u in T is the height of the subtree rooted at u

Page 8: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 15 Copyright © D. Vernon 2014

AVL Trees

JULY

FEB

MAY

AUG

JAN

MAR

OCT

APR

DEC

JUN

NOV

SEPT

Algorithms and Data Structures 16 Copyright © D. Vernon 2014

AVL Trees

JULY

FEB

MAY

AUG

JAN

MAR

OCT

APR

DEC

JUN

NOV

SEPT

Page 9: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 17 Copyright © D. Vernon 2014

AVL Trees

JULY

FEB MAY

AUG JAN MAR OCT

APR DEC JUN NOV SEPT

A  Balanced  Tree  for  the  Months  of  the  Year  

Algorithms and Data Structures 18 Copyright © D. Vernon 2014

AVL Trees

JULY

FEB MAY

AUG JAN MAR OCT

APR DEC JUN NOV SEPT

A  Balanced  Tree  for  the  Months  of  the  Year  

Page 10: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 19 Copyright © D. Vernon 2014

AVL Trees

•  Let’s construct a height-balanced tree

•  Order of insertions:

March, May, November, August, April, January, December, July, February, June, October, September

•  Before we do, we need a definition of a balance factor

Algorithms and Data Structures 20 Copyright © D. Vernon 2014

AVL Trees

•  Balance Factor BF(T) of a note T in a binary tree is defined to be

height(T1) - height(T2)

where T1 and T2 are the left and right subtrees of T

•  For any node T in an AVL tree BF(T) = -1, 0, +1

Page 11: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 21 Copyright © D. Vernon 2014

MAR MARCH

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 22 Copyright © D. Vernon 2014

MAR MARCH NO REBALANCING NEEDED

New After After Identifier Insertion Rebalancing

Page 12: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 23 Copyright © D. Vernon 2014

MAR MARCH NO REBALANCING NEEDED

MAR MAY

MAY

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 24 Copyright © D. Vernon 2014

MAR MARCH NO REBALANCING NEEDED

MAR MAY NO REBALANCING NEEDED

MAY

New After After Identifier Insertion Rebalancing

Page 13: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 25 Copyright © D. Vernon 2014

MARCH NO REBALANCING NEEDED

MAY NO REBALANCING NEEDED

NOVEMBER MAR

MAY

NOV

MAR

MAR

MAY

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 26 Copyright © D. Vernon 2014

MARCH NO REBALANCING NEEDED

MAY NO REBALANCING NEEDED

MAR NOVEMBER

MAY

NOV

MAR

MAR

MAY

New After After Identifier Insertion Rebalancing

Page 14: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 27 Copyright © D. Vernon 2014

MARCH NO REBALANCING NEEDED

MAY NO REBALANCING NEEDED

NOVEMBER

RR rebalancing

MAR

MAY

NOV

MAR

MAR

MAY

MAR

MAY

NOV

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 28 Copyright © D. Vernon 2014

AUGUST

MAR

MAY

NOV

AUG

New After After Identifier Insertion Rebalancing

Page 15: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 29 Copyright © D. Vernon 2014

AUGUST NO REBALANCING NEEDED MAR

MAY

NOV

AUG

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 30 Copyright © D. Vernon 2014

AUGUST NO REBALANCING NEEDED MAR

MAY

NOV

AUG

APRIL

MAR

MAY

NOV

AUG

APR

New After After Identifier Insertion Rebalancing

Page 16: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 31 Copyright © D. Vernon 2014

AUGUST NO REBALANCING NEEDED MAR

MAY

NOV

AUG

APRIL

MAR

MAY

NOV

AUG

APR

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 32 Copyright © D. Vernon 2014

NO REBALANCING NEEDED

LL rebalancing

MAR

MAY

NOV AUG

APR

AUGUST

MAR

MAY

NOV

AUG

APRIL

MAR

MAY

NOV

AUG

APR

New After After Identifier Insertion Rebalancing

Page 17: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 33 Copyright © D. Vernon 2014

JANUARY

MAR

MAY

NOV AUG

APR

JAN

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 34 Copyright © D. Vernon 2014

JANUARY

MAR

MAY

NOV AUG

APR

JAN

New After After Identifier Insertion Rebalancing

Page 18: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 35 Copyright © D. Vernon 2014

JANUARY

LR rebalancing

MAR

MAY

NOV

AUG

APR JAN MAR

MAY

NOV AUG

APR

JAN

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 36 Copyright © D. Vernon 2014

DECEMBER MAR

MAY

NOV

AUG

APR JAN

DEC

New After After Identifier Insertion Rebalancing

Page 19: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 37 Copyright © D. Vernon 2014

DECEMBER

NO REBALANCING NEEDED

MAR

MAY

NOV

AUG

APR JAN

DEC

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 38 Copyright © D. Vernon 2014

JULY MAR

MAY

NOV

AUG

APR JAN

DEC JUL

New After After Identifier Insertion Rebalancing

Page 20: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 39 Copyright © D. Vernon 2014

JULY

NO REBALANCING NEEDED

MAR

MAY

NOV

AUG

APR JAN

DEC JUL

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 40 Copyright © D. Vernon 2014

FEBRUARY

MAR

MAY

NOV

AUG

APR JAN

DEC JUL

FEB

New After After Identifier Insertion Rebalancing

Page 21: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 41 Copyright © D. Vernon 2014

FEBRUARY

MAR

MAY

NOV

AUG

APR JAN

DEC JUL

FEB

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 42 Copyright © D. Vernon 2014

FEBRUARY

RL rebalancing

MAR

MAY

NOV JAN

DEC

JUL FEB

AUG

APR

MAR

MAY

NOV

AUG

APR JAN

DEC JUL

FEB

New After After Identifier Insertion Rebalancing

Page 22: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 43 Copyright © D. Vernon 2014

JUNE

MAR

MAY

NOV JAN

DEC

JUL FEB

AUG

APR

JUN

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 44 Copyright © D. Vernon 2014

JUNE

MAR

MAY

NOV JAN

DEC

JUL FEB

AUG

APR

JUN

New After After Identifier Insertion Rebalancing

Page 23: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 45 Copyright © D. Vernon 2014

JUNE

LR rebalancing

MAR

MAY

NOV

JAN

DEC

JUL FEB AUG

APR JUN

MAR

MAY

NOV JAN

DEC

JUL FEB

AUG

APR

JUN

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 46 Copyright © D. Vernon 2014

OCTOBER

MAR

MAY

NOV

JAN

DEC

JUL FEB AUG

APR JUN

OCT

New After After Identifier Insertion Rebalancing

Page 24: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 47 Copyright © D. Vernon 2014

OCTOBER

MAR

MAY

NOV

JAN

DEC

JUL FEB AUG

APR JUN

OCT

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 48 Copyright © D. Vernon 2014

RR rebalancing

MAR

MAY

NOV

JAN

DEC

JUL FEB AUG

APR JUN OCT

OCTOBER

MAR

MAY

NOV

JAN

DEC

JUL FEB AUG

APR JUN

OCT

New After After Identifier Insertion Rebalancing

Page 25: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 49 Copyright © D. Vernon 2014

SEPTEMBER

MAR

MAY

NOV

JAN

DEC

JUL FEB AUG

APR JUN OCT

SEPT

New After After Identifier Insertion Rebalancing

Algorithms and Data Structures 50 Copyright © D. Vernon 2014

New After After Identifier Insertion Rebalancing

SEPTEMBER NO REBALANCING NEEDED

MAR

MAY

NOV

JAN

DEC

JUL FEB AUG

APR JUN OCT

SEPT

Page 26: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 51 Copyright © D. Vernon 2014

AVL Trees

•  All re-balancing operations are carried out with respect to the closest ancestor of the new node having balance factor +2 or -2

•  There are 4 types of re-balancing operations (called rotations)

–  RR –  LL (symmetric with RR) –  RL –  LR (symmetric with RL)

Algorithms and Data Structures 52 Copyright © D. Vernon 2014

AVL Trees

•  Let’s refer to the node inserted as Y

•  Let’s refer to the nearest ancestor having balance factor +2 or -2 as A

Page 27: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 53 Copyright © D. Vernon 2014

AVL Trees

•  LL: Y is inserted in the Left subtree of the Left subtree of A

–  LL: the path from A to Y –  Left subtree then Left subtree

•  LR: Y is inserted in the Right subtree of the Left subtree of A

–  LR: the path from A to Y –  Left subtree then Right subtree

Algorithms and Data Structures 54 Copyright © D. Vernon 2014

AVL Trees

•  RR: Y is inserted in the Right subtree of the Right subtree of A

–  RR: the path from A to Y –  Right subtree then Right subtree

•  RL: Y is inserted in the Left subtree of the Right subtree of A

–  LL: the path from A to Y –  Right subtree then Left subtree

Page 28: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 55 Copyright © D. Vernon 2014

AVL Trees

+1  

A  

0  

B  

BL   BR  

AR  

Balanced  Subtree  

Algorithms and Data Structures 56 Copyright © D. Vernon 2014

AVL Trees

+2  

A  

+1  

B  

BL   BR  

AR  

Height  of  BL  increases  to  h+1  

Page 29: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 57 Copyright © D. Vernon 2014

AVL Trees - LL rotation

+2  

A  

+1  

B  

BL   BR  

AR  

0  

B  

0  

A  BL  

BR   AR  

Algorithms and Data Structures 58 Copyright © D. Vernon 2014

AVL Trees

-­‐1  

A  

0  

B  

BR  BL  

AL  

Balanced  Subtree  

Page 30: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 59 Copyright © D. Vernon 2014

AVL Trees

Unbalanced  following  inserCon  

-­‐2  

A  

-­‐1  

B  

BR  BL  

AL  

Height  of  BR  inceases  to  h+1  

Algorithms and Data Structures 60 Copyright © D. Vernon 2014

AVL Trees - RR Rotation

Unbalanced  following  inserCon  

Height  of  BR  inceases  to  h+1  

0  

B  

0  

A  

AL   BL  

BR  

Rebalanced  subtree  

-­‐2  

A  

-­‐1  

B  

BR  BL  

AL  

Page 31: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 61 Copyright © D. Vernon 2014

AVL Trees

+1  

A  

Balanced  Subtree  

0  

B  

Algorithms and Data Structures 62 Copyright © D. Vernon 2014

AVL Trees

+2  

A  

0  

C  

-­‐1  

B  

Page 32: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 63 Copyright © D. Vernon 2014

AVL Trees - LR rotation (a)

+2  

A  

0  

C  

-­‐1  

B  0  

A  

0  

C  

0  

B  

Algorithms and Data Structures 64 Copyright © D. Vernon 2014

AVL Trees

+1  

A  

0  

C  

CR  CL  

BL  

Balanced  Subtree  

0  

B  AR  

Page 33: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 65 Copyright © D. Vernon 2014

AVL Trees

+2  

A  

+1  

C  

CR  CL  

BL  

Unbalanced  following  inserCon  

-­‐1  

B  AR  

Algorithms and Data Structures 66 Copyright © D. Vernon 2014

AVL Trees - LR rotation (b)

+2  

A  

+1  

C  

CR  CL  

BL  

-­‐1  

B  AR  

 0  

A  

0  

C  

CR  CL  BL  

0  

B  

AR  

Page 34: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 67 Copyright © D. Vernon 2014

AVL Trees

+1  

A  

0  

C  

CR  CL  

BL  

Balanced  Subtree  

0  

B  AR  

Algorithms and Data Structures 68 Copyright © D. Vernon 2014

AVL Trees

+2  

A  

-­‐1  

C  

CR  CL  

BL  

Unbalanced  following  inserCon  

-­‐1  

B  AR  

Page 35: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 69 Copyright © D. Vernon 2014

AVL Trees - LR rotation (c)

+2  

A  

-­‐1  

C  

CR  CL  

BL  

-­‐1  

B  AR  

0  

A  

0  

C  

CR  CL  BL  

+1  

B  

AR  

Algorithms and Data Structures 70 Copyright © D. Vernon 2014

AVL Trees

Balanced  Subtree  

-­‐1  

A  

0  

C  

CL   CR  

BR  

0  

B  AL  

Page 36: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 71 Copyright © D. Vernon 2014

AVL Trees

Unbalanced  following  inserCon  

-­‐2  

A  

-­‐1  

C  

CR   CL  

BR  

+1  

B  AL  

Algorithms and Data Structures 72 Copyright © D. Vernon 2014

AVL Trees - RL rotation

-­‐2  

A  

-­‐1  

C  

CL   CR  

BL  

+1  

B  AL  

+1  

A  

0  

C  

CL   CR   BL  

0  

B  

AL  

Page 37: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 73 Copyright © D. Vernon 2014

AVL Trees

Algorithms and Data Structures 74 Copyright © D. Vernon 2014

AVL Trees

•  Note in passing that, since A is the nearest ancestor to Y whose balance factor was +1 or -1 before insertion, the balance factor of all other nodes on the path from A to Y must be 0

•  When we re-balance the tree, the balance factors change (see diagrams above)

–  But changes only occur in subtree which is being rebalanced

Page 38: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 75 Copyright © D. Vernon 2014

AVL Trees

•  The balance factors also change following an insertion which requires no rebalancing

•  BF(A) is +1 or -1 before insertion

•  Insertion causes height of one of A’s subtrees to increase by 1

•  Thus, BF(A) must be 0 after insertion (since, in this case, it’s not +2 or -2)

Algorithms and Data Structures 76 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

PROCEDURE AVL_insert(e:elementtype; w:windowtype;

T: BINTREE);

(* We assume that variables of element type have two *)

(* data fields: the information field and a balance *)

(* factor *)

(* Assume also existence of two ADT functions to *)

(* examine these fields: *)

(* Examine_BF(w, T) *)

(* Examine_data(w, T) *)

(* and one to modify the balance factor field *)

(* Replace_BF(bf, w, T) *)

var newnode: linktype;

begin

Page 39: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 77 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

IF isEmpty(tree) /* special case */

THEN

insert(e, w, tree); /*insert with window */ replace_BF(0, w, tree)

ELSE

/* Phase 1: locate insertion point *)

/* A keeps track of most recent node with *)

(* balance factor +1 or -1 *)

A := w;

WHILE ((NOT IsExternal(w, T)) AND

(NOT (e.data = Examine_Data(w, T))) DO

IF Examine_BF(w, T) <> 0 (* non-zero BF *)

THEN

A := w;

ENDIF;

Algorithms and Data Structures 78 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

IF (e.data < Examine_Data(w, T) ) THEN Child(0, w, T) ELSE IF (e.data > Examine_Data(w, T) ) Child(1, w, T) ENDIF ENDIF ENDWHILE (* If not found, then embark on Phase 2: *) (* insert & rebalance *) IF IsExternal(w, T) THEN Insert(e, w, T); (*insert as before *) Replace_BF(0, w, T) ENDIF

Page 40: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 79 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

(* adjust balance factors of nodes on path *) (* from A to parent of newly-inserted node *) (* By definition, they will have had BF=0 *) (* and so must now change to +1 or -1 *) (* Let d = this change, *) (* d = +1 ... insertion in A’s left subtree *) (* d = -1 ... insertion in A’s right subtree *)

IF (e.data < Examine_Data(A, T) ) THEN v:= A; Child(0, v, T) B:= v; d := +1 ELSE

Algorithms and Data Structures 80 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

ELSE v:= A; Child(1, v, T) B:= v; d := -1 ENDIF WHILE ((NOT IsEqual(w, v))) DO IF (e.data < Examine_Data(v, T) ) THEN ReplaceBF(+1, v, T); Child(0, v, T) (* height of Left ^ *) ELSE ReplaceBF(-1, v, T); Child(1, v, T) (* height of Right ^ *) ENDIF ENDWHILE

Page 41: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 81 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

(* check to see if tree is unbalanced *)

IF (ExamineBF(A, T) = 0 )

THEN ReplaceBF(d, A, T) (* still balanced *)

ELSE

IF ((ExamineBF(A, T) + d) = 0)

THEN

ReplaceBF(0, A, T)(*still balanced*) ELSE

(* Tree is unbalanced *)

(* determine rotation type *)

Algorithms and Data Structures 82 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

(* Tree is unbalanced *) (* determine rotation type *)

IF d = +1 THEN (* left imbalance *) IF ExamineBF(B) = +1 THEN (* LL Rotation *) (* replace left subtree of A *) (* with right subtree of B *) temp := B; Child(1, temp, T); ReplaceChild(0, A, T, temp);

(* replace right subtree of B with A *) ReplaceChild(1, B, T, A);

Page 42: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 83 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

(* replace right subtree of B with A *)

ReplaceChild(1, B, T, A);

ReplaceBF(0, A, T); ReplaceBF(0, B, T);

ELSE (* LR Rotation *)

C := B; Child(1, C, T);

C_L := C; Child(0, C_L, T);

C_R := C; Child(1, C_R, T); ReplaceChild(1, B, T, C_L);

ReplaceChild(0, A, T, C_R);

ReplaceChild(0, C, T, B);

ReplaceChild(1, C, T, A);

Algorithms and Data Structures 84 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

IF ExamineBF(C) = +1 (* LR(b) *) THEN ReplaceBF(-1, A, T); ReplaceBF(0, B, T); ELSE IF ExamineBF(C) = -1 (* LR(c) *) THEN ReplaceBF(+1, B, T); ReplaceBF(0, A, T); ELSE (* LR(a) *) ReplaceBF(0, A, T); ReplaceBF(0, B, T); ENDIF ENDIF

Page 43: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 85 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

(* B is new root *)

ReplaceBF(0, C, T);

B := C ENDIF (* LR rotation *)

ELSE (* right imbalance *)

(* this is symmetric to left imbalance *)

(* and is left as an exercise! *)

ENDIF (* d = +1 *)

Algorithms and Data Structures 86 Copyright © D. Vernon 2014

Pseudo-code Implementation of AVL_Insert()

(* the subtree with root B has been *) (* rebalanced and it now replaces *) (* A as the root of the originally *) (* unbalanced tree *)

ReplaceTree(A, T, B) (* Replace subtree A with B in T *) (* Note: this is a trivial operation *) (* since we are using a complex *) (* window variable *) ENDIF ENDIF ENDIF END (* AVL_Insert() *)

Page 44: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 87 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 88 Copyright © D. Vernon 2014

Red-Black Trees

•  The goal of height-balanced trees is to ensure that the tree is as complete as possible and that, consequently, it has minimal height for the number of nodes in the tree

•  As a result, the number of probes it takes to search the tree (and the time it takes) is minimized.

Page 45: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 89 Copyright © D. Vernon 2014

Red-Black Trees

•  A perfect or a complete tree with n nodes has height O(log2n)

–  So the time it takes to search a perfect or a complete tree with n nodes is O(log2n)

•  A skinny tree could have height O(n)

–  So the time it takes to search a skinny tree can be O(n)

•  Red-Black trees are similar to AVL trees in that they allow us to construct trees which have a guaranteed search time O(log2n)

Algorithms and Data Structures 90 Copyright © D. Vernon 2014

Red-Black Trees

•  A red-black tree is a binary tree whose nodes can be coloured either red or black to satisfy the following conditions:

–  Black condition: Each root-to-frontier path contains exactly the same number of black nodes

–  Red condition: Each red node that is not the root has a black parent

–  Each external node is black

Page 46: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 91 Copyright © D. Vernon 2014

Red-Black Trees

•  A red-black search tree is a red-black tree that is also a binary search tree

•  For all n>= 1, every red-black tree of size n has height O(log2n)

–  Thus, red-black trees provide a guaranteed worst-case search time of O(log2n)

Algorithms and Data Structures 92 Copyright © D. Vernon 2014

Red-Black Trees

Red-­‐black  tree  (condiCon  3)  

Page 47: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 93 Copyright © D. Vernon 2014

Red-Black Trees

Red-­‐black  tree  (condiCon  3)  

Undetermined  colour  

Algorithms and Data Structures 94 Copyright © D. Vernon 2014

Red-Black Trees

If  root  was  red,  then  right  child  would  have  to    be  black  (if  it  was  red,  it  would  have  to  have  a  black  parent)  but  then  the  black  condiCon  would  be  violated.  

Page 48: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 95 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 96 Copyright © D. Vernon 2014

Red-Black Trees

a  

b  

c  

To  saCsfy  black  condiCon,  either  

(1)  node  a  is  black  and  nodes  b  and              c  are  red,  or    (2)  nodes  a,  b,  and  c  are  red.  

In  both  cases,  a  red  condiCon  is  violated.  

Therefore,  this  is  not  a  red-­‐black  tree  

Page 49: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 97 Copyright © D. Vernon 2014

Red-Black Trees

•  For all n >= 1, every red-black tree of size n has height O(log2n)

•  Thus, red-black trees provide a guaranteed worst-case search time of O(log2n)

Algorithms and Data Structures 98 Copyright © D. Vernon 2014

Red-Black Trees

•  Insertions and deletions can cause red and black conditions to be violated

•  Trees then have to be restructured

•  Restructuring called a promotion (or rotation)

–  Single promotion –  2 promotion

Page 50: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 99 Copyright © D. Vernon 2014

Red-Black Trees

•  Single promotion

•  Also referred to as

–  single (left) rotation –  single (right) rotation

•  Promotes a node one level

Algorithms and Data Structures 100 Copyright © D. Vernon 2014

Red-Black Trees

Promote  v  (LeQ  RotaCon)  

Promote  u  (Right  RotaCon)  

Page 51: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 101 Copyright © D. Vernon 2014

Red-Black Trees

•  A single promotion (Left Rotation or Right Rotation) preserves the binary-search condition

•  Same manner as an AVL rotation

Algorithms and Data Structures 102 Copyright © D. Vernon 2014

Red-Black Trees

Promote  v  (LeQ  RotaCon)  

Promote  u  (Right  RotaCon)  

keys(1)  <  key(v)  <  key(u)  key(v)  <  keys(2)  <  key(u)  key(u)  <  keys(3)  

keys(1)  <  key(v)    key(v)  <  keys(2)  <  key(u)  key(v)  <  key(u)  <  keys(3)  

Page 52: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 103 Copyright © D. Vernon 2014

Red-Black Trees

•  2-Promotion

•  Zig-zag promotion

•  Composed of two single promotions

•  And hence preserves the binary-search condition

Algorithms and Data Structures 104 Copyright © D. Vernon 2014

Red-Black Trees

Page 53: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 105 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 106 Copyright © D. Vernon 2014

Red-Black Trees

Page 54: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 107 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 108 Copyright © D. Vernon 2014

Red-Black Trees

•  Insertions

–  A red-black tree can be searched in logarithmic time, worst case

–  Insertions may violate the red-black conditions necessitating restructuring

–  This restructuring can also be effected in logarithmic time

–  Thus, an insertion (or a deletion) can be effected in logarithmic time

Page 55: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 109 Copyright © D. Vernon 2014

Red-Black Trees

•  Just as with AVL trees, we perform the insertion by

–  first searching the tree until an external node is reached (if the key is not already in the tree)

–  then inserting the new (internal) node

•  We then have to recolour and restructure, if necessary

Algorithms and Data Structures 110 Copyright © D. Vernon 2014

Red-Black Trees

Page 56: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 111 Copyright © D. Vernon 2014

Red-Black Trees

•  Recolouring:

–  Colour new node red –  This preserves the black condition –  but may violate the red condition

•  Red condition can be violated only if the parent of an internal node is also red

•  Must transform this ‘almost red-black tree’ into a red-black tree

Algorithms and Data Structures 112 Copyright © D. Vernon 2014

Red-Black Trees

Page 57: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 113 Copyright © D. Vernon 2014

Red-Black Trees

•  Recolouring and restructuring algorithm

–  The node u is a red node in a BST, T –  u is the only candidate violating node –  Apart from u, the tree T is red-black

Algorithms and Data Structures 114 Copyright © D. Vernon 2014

Red-Black Trees

Page 58: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 115 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 116 Copyright © D. Vernon 2014

Red-Black Trees

Page 59: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 117 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 118 Copyright © D. Vernon 2014

Red-Black Trees

•  Case 3:

–  u is not the root, –  its parent v is not the root, –  v is the left child of its parent w –  (x is the right child of w, i.e. x is v’s sibling)

Page 60: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 119 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 120 Copyright © D. Vernon 2014

Red-Black Trees

Page 61: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 121 Copyright © D. Vernon 2014

Red-Black Trees

•  u must be red because we colour new nodes that way by convention (to preserve the black condition)

•  v must be red because otherwise it would be black and then we wouldn’t have violated the red condition and we wouldn’t be restructuring anything!

•  w must be black because every red node (that isn’t the root) has a black parent

Algorithms and Data Structures 122 Copyright © D. Vernon 2014

Red-Black Trees

Page 62: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 123 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 124 Copyright © D. Vernon 2014

Red-Black Trees

Page 63: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 125 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 126 Copyright © D. Vernon 2014

Red-Black Trees

Page 64: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 127 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 128 Copyright © D. Vernon 2014

Red-Black Trees

Page 65: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 129 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 130 Copyright © D. Vernon 2014

Red-Black Trees

Page 66: CS-CO-412 ADS 10 Trees 3 - Height-Balanced Trees.pptx

Algorithms and Data Structures 131 Copyright © D. Vernon 2014

Red-Black Trees

Algorithms and Data Structures 132 Copyright © D. Vernon 2014

Red-Black Trees