COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi - - PowerPoint PPT Presentation

col106 data structures and algorithms
SMART_READER_LITE
LIVE PREVIEW

COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi - - PowerPoint PPT Presentation

COL106: Data Structures and Algorithms Ragesh Jaiswal, IIT Delhi Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms Data Structures: Balanced Binary Search Trees Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms


slide-1
SLIDE 1

COL106: Data Structures and Algorithms

Ragesh Jaiswal, IIT Delhi

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-2
SLIDE 2

Data Structures: Balanced Binary Search Trees

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-3
SLIDE 3

Data Structures

Binary Search Trees

Consider the following implementation: Code class Node{ public int key; public String value; public Node leftChild; public Node rightChild; public Node parent; } public class BST{ public int size; public Node root; public BST(){ size = 0;root = null; } public boolean isLeaf(Node N){//To be written} public String get(int k){//To be written} public void put(int k, String v){//To be written} public void remove(int k){//To be written} }

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-4
SLIDE 4

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #1

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-5
SLIDE 5

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #2

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-6
SLIDE 6

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #3

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-7
SLIDE 7

Data Structures

Balanced Binary Search Trees

Tri-node restructuring for a node x, its parent y, and its grandparent z.

Figure : Case #4

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-8
SLIDE 8

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Claim: The height of any AVL tree storing n nodes is O(log n).

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-9
SLIDE 9

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Question: How do we perform get(k) operation on an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-10
SLIDE 10

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Question: How do we perform get(k) operation on an AVL tree? The same as BST

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-11
SLIDE 11

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Question: How do we perform put(k, v) operation on an AVL tree?

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-12
SLIDE 12

Data Structures

Balanced Binary Search Trees → AVL Trees AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Question: How do we perform put(k, v) operation on an AVL tree?

Same as in BST. However, you also have to make sure that after insertion, the height balance property is maintained. Consider inserting an entry with key 32 in the Tree below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-13
SLIDE 13

Data Structures

Balanced Binary Search Trees → AVL Trees

AVL Tree: An AVL tree is a binary search tree that satisfies the following property: Height balance property: For every internal node of the tree, the heights of its children differ by at most 1. Question: How do we perform put(k, v) operation on an AVL tree?

Same as in BST. However, you also have to make sure that after insertion, the height balance property is maintained. Consider inserting an entry with key 32 in the Tree below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-14
SLIDE 14

Data Structures

Balanced Binary Search Trees → AVL Trees

Question: How do we perform put(k, v) operation on an AVL tree? Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-15
SLIDE 15

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-16
SLIDE 16

Data Structures

Balanced Binary Search Trees → AVL Trees Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-17
SLIDE 17

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Figure : Suppose the insertion happens in the right sub-tree of node labeled x.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-18
SLIDE 18

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Figure : Suppose the insertion happens in T3 and x, y, z are as defined in the pseudocode.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-19
SLIDE 19

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Figure : Suppose the insertion happens in T3 and x, y, z are as defined in the pseudocode.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-20
SLIDE 20

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Figure : Suppose the insertion happens in T3 and x, y, z are as defined in the pseudocode. For some h the height of the nodes before insertion will be as shown above.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-21
SLIDE 21

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Figure : The height of the nodes after inserting the new node are as shown above.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-22
SLIDE 22

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm //p denotes the node that is inserted. BalanceAfterPut(Node p)

  • While going up from p, let z denote the first node for which

the height balance property is not satisfied.

  • Let y be the child of z with greater height.
  • Let x be the child of y with greater height.
  • Perform a tri-node restructuring w.r.t. nodes x, y, z.

Figure : The height of the nodes after inserting and performing tri-node restructuring.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-23
SLIDE 23

Data Structures

Balanced Binary Search Trees → AVL Trees

Question: How do we perform remove(k) operation on an AVL tree?

Same as in BST. However, you also have to make sure that after deletion, the height balance property is maintained. Consider deleting the entry with key 20 in the Tree below.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-24
SLIDE 24

Data Structures

Balanced Binary Search Trees → AVL Trees

Question: How do we perform remove(k) operation on an AVL tree?

Same as in BST. However, you also have to make sure that after deletion, the height balance property is maintained. Consider deleting the entry with key 20 in the Tree below. Figure : The tree needs to be balanced.

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-25
SLIDE 25

Data Structures

Balanced Binary Search Trees → AVL Trees

Question: How do we perform remove(k) operation on an AVL tree? Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-26
SLIDE 26

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Figure : The tree needs to be balanced. Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-27
SLIDE 27

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-28
SLIDE 28

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Figure : Suppose a node is deleted from T1. Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-29
SLIDE 29

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Figure : Suppose a node is deleted from T1. Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-30
SLIDE 30

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Figure : Suppose a node is deleted from T1. One possible scenario for heights before deletion. Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-31
SLIDE 31

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Figure : Suppose a node is deleted from T1. Heights after deletion. Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-32
SLIDE 32

Data Structures

Balanced Binary Search Trees → AVL Trees

Algorithm Sketch //Initially p denotes the parent of the removed node BalanceAfterRemove(Node p)

  • Let z be the first unbalanced node going up from p
  • If no such z exists then return
  • Let y be the child of z of greater height.
  • Let x be the child of y defined as follows:

If one child of y is taller than the other then x is the taller child, otherwise x is the child of y with the same side as y is of z.

  • Perform Tri-node restructuring w.r.t. x, y, z
  • Let b denote the tallest node (among the nodes involved

in restructuring) after the restructuring.

  • If b is not the root, then BalanceAfterRemove(b.parent)

Figure : Suppose a node is deleted from T1. Heights after tri-node restructuring. Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms

slide-33
SLIDE 33

End

Ragesh Jaiswal, IIT Delhi COL106: Data Structures and Algorithms