COMP9024 ♢ Week 03b ♢Search Tree Algorithms ♢ (22T0)

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [0/55]
❖ Tree Review

Binary search trees

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [1/55]
❖ Balanced BSTs

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [2/55]
❖ Balanced Binary Search Trees

Goal: build binary search trees which have

Best balance you can achieve for tree with N nodes:

[Diagram:Pic/binary-search-trees.png]

Three strategies to improving worst case search in BSTs:

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [3/55]
❖ Operations for Rebalancing

To assist with rebalancing, we consider new operations:

Left rotation

Right rotation Insertion at root
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [4/55]
❖ Tree Rotation

In tree below:   t1  <  n2  <  t2  <  n1  <  t3

[Diagram:Pic/left-right-rotation.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [5/55]
❖ ... Tree Rotation

Method for rotating tree T right:

Left rotation: swap left/right in the above.

Cost of tree rotation: O(1)

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [6/55]
❖ ... Tree Rotation

Algorithm for right rotation:

rotateRight(n1):
|  Input  tree n1
|  Output n1 rotated to the right
|
|  if n1 is empty or left(n1) is empty then
|     return n1
|  end if
|  n2=left(n1)
|  left(n1)=right(n2)
|  right(n2)=n1
|  return n2

[Diagram:Pic/left-right-rotation.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [7/55]
❖ Exercise : Tree Rotation

Consider the tree t:

[Diagram:Pic/joinTreesSoln.png]

Show the result of rotateRight(t)

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [8/55]

[Diagram:Pic/rotate.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [9/55]
❖ Exercise : Tree Rotation

Write the algorithm for left rotation

[Diagram:Pic/left-right-rotation.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [10/55]

rotateLeft(n2):
|  Input  tree n2
|  Output n2 rotated to the left
|
|  if n2 is empty or right(n2) is empty then
|     return n2
|  end if
|  n1=right(n2)
|  right(n2)=left(n1)
|  left(n1)=n2
|  return n1

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [11/55]
❖ Insertion at Root

Previous description of BSTs inserted at leaves.

Different approach: insert new item at root.

Potential disadvantages:

Potential advantages:
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [12/55]
❖ ... Insertion at Root

Method for inserting at root:

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [13/55]
❖ ... Insertion at Root

[Diagram:Pic/insert-at-root.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [14/55]
❖ Exercise : Insertion at Root

Consider the tree t:

[Diagram:Pic/insertRoot.png]

Show the result of insertAtRoot(t,24)

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [15/55]

[Diagram:Pic/joinTreesSoln.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [16/55]
❖ ... Insertion at Root

Analysis of insertion-at-root:

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [17/55]
❖ Rebalancing Trees

An approach to balanced trees:

Question: how frequently/when/how to rebalance?

NewTreeInsert(tree,item):
|  Input  tree, item
|  Output tree with item randomly inserted
|
|  t=insertAtLeaf(tree,item)
|  if #nodes(t) mod k = 0 then
|     t=rebalance(t)
|  end if
|  return t

E.g. rebalance after every 20 insertions ⇒ choose k=20

Note: To do this efficiently we would need to change tree data structure and basic operations:


typedef struct Node {
   int  data;
   int  nnodes;      // #nodes in my tree
   Tree left, right; // subtrees
} Node;

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [18/55]
❖ ... Rebalancing Trees

How to rebalance a BST?   Move median item to root.

[Diagram:Pic/rebalance.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [19/55]
❖ ... Rebalancing Trees

Implementation of rebalance:

rebalance(t):
|  Input  tree t with n nodes
|  Output t rebalanced
|
|  if n≥3 then
|  |  t=partition(t,n/2)         // put node with median key at root
|  |  left(t)=rebalance(left(t))  // then rebalance each subtree
|  |  right(t)=rebalance(right(t))
|  end if
|  return t

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [20/55]
❖ ... Rebalancing Trees

New operation on trees:

[Diagram:Pic/tindex.png]

For tree with N nodes, indices are 0 .. N-1

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [21/55]
❖ ... Rebalancing Trees

Partition: moves i th node to root

[Diagram:Pic/partition-on-i.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [22/55]
❖ ... Rebalancing Trees

Implementation of partition operation:

partition(tree,i):
|  Input  tree with n nodes, index i
|  Output tree with item #i moved to the root
|
|  m=#nodes(left(tree))
|  if i < m then
|     left(tree)=partition(left(tree),i)
|     tree=rotateRight(tree)
|  else if i > m then
|     right(tree)=partition(right(tree),i-m-1)
|     tree=rotateLeft(tree)
|  end if
|  return tree

Note:  size(tree) = n,   size(left(tree)) = m,   size(right(tree)) = n-m-1   (why -1?)

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [23/55]
❖ Exercise : Partition

Consider the tree t:

[Diagram:Pic/insertRoot.png]

Show the result of partition(t,3)

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [24/55]

[Diagram:Pic/partitionSoln.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [25/55]
❖ ... Rebalancing Trees

Analysis of rebalancing: visits every node ⇒ O(N)

Cost means not feasible to rebalance after each insertion.

When to rebalance? … Some possibilities:

Either way, we tolerate worse search performance for periods of time.

Does it solve the problem? … Not completely   ⇒ Solution: real balanced trees  (later)

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [26/55]
❖ Real Balanced Trees

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [27/55]
❖ Better Balanced Binary Search Trees

So far, we have seen …

Ideally, we want both average/worst case to be O(log n)
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [28/55]
❖ AVL Trees

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [29/55]
❖ AVL Trees

Invented by Georgy Adelson-Velsky and Evgenii Landis

Approach:

A tree is unbalanced when: abs(height(left)-height(right)) > 1

This can be repaired by at most two rotations:

Problem: determining height/depth of subtrees may be expensive.
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [30/55]
❖ ... AVL Trees

Implementation of AVL insertion


insertAVL(tree,item):
|  Input  tree, item
|  Output tree with item AVL-inserted
|
|  if tree is empty then
|     return new node containing item
|  else if item=data(tree) then
|     return tree
|  else
|  |  if item<data(tree) then
|  |     left(tree)=insertAVL(left(tree),item)
|  |  else if item>data(tree) then
|  |     right(tree)=insertAVL(right(tree),item)
|  |  end if
|  |  if height(left(tree))-height(right(tree)) > 1 then
|  |     if item>data(left(tree)) then
|  |        left(tree)=rotateLeft(left(tree))
|  |     end if
|  |     tree=rotateRight(tree)
|  |  else if height(right(tree))-height(left(tree)) > 1 then
|  |     if item<data(right(tree)) then
|  |        right(tree)=rotateRight(right(tree))
|  |     end if
|  |     tree=rotateLeft(tree)
|  |  end if
|  |  return tree
|  end if

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [31/55]
❖ Exercise : AVL Trees

Insert 27 into the AVL tree

[Diagram:Pic/tree2.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [32/55]

[Diagram:Pic/tree4.png]

What would happen if you now insert 28?

You may like the animation at www.cs.usfca.edu/~galles/visualization/AVLtree.html

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [33/55]
❖ ... AVL Trees

Analysis of AVL trees:

[Diagram:Pic/height-weight.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [34/55]
❖ 2-3-4 Trees

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [35/55]
❖ 2-3-4 Trees

2-3-4 trees have three kinds of nodes

[Diagram:Pic/2-3-4-tree.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [36/55]
❖ ... 2-3-4 Trees

2-3-4 trees are ordered similarly to BSTs

[Diagram:Pic/2-3-4-order.png]

In a balanced 2-3-4 tree:

2-3-4 trees grow "upwards" by splitting 4-nodes.
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [37/55]
❖ ... 2-3-4 Trees

Possible 2-3-4 tree data structure:

typedef struct node {
   int          order;     // 2, 3 or 4
   int          data[3];   // items in node
   struct node *child[4];  // links to subtrees
} node;

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [38/55]
❖ ... 2-3-4 Trees

Searching in 2-3-4 trees:

Search(tree,item):
|  Input  tree, item
|  Output address of item if found in 2-3-4 tree
|         NULL otherwise
|
|  if tree is empty then
|     return NULL
|  else
|  |  i=0
|  |  while i<tree.order-1 and item>tree.data[i] do
|  |     i=i+1   // find relevant slot in data[]
|  |  end while
|  |  if item=tree.data[i] then   // item found
|  |     return address of tree.data[i]
|  |  else       // keep looking in relevant subtree
|  |     return Search(tree.child[i],item)
|  |  end if
|  end if

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [39/55]
❖ ... 2-3-4 Trees

2-3-4 tree searching cost analysis:

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [40/55]
❖ Insertion into 2-3-4 Trees

Starting with the root node:

repeat

until Item inserted
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [41/55]
❖ ... Insertion into 2-3-4 Trees

Insertion into a 2-node or 3-node:

[Diagram:Pic/2-3-4-add.png]

Insertion into a 4-node (requires a split):

[Diagram:Pic/2-3-4-split.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [42/55]
❖ ... Insertion into 2-3-4 Trees

Splitting the root:

[Diagram:Pic/2-3-4-split-root.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [43/55]
❖ ... Insertion into 2-3-4 Trees

Building a 2-3-4 tree … 7 insertions:

[Diagram:Pic/2-3-4-insert.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [44/55]
❖ Exercise : Insertion into 2-3-4 Tree

Show what happens when D, S, F, U are inserted into this tree:

[Diagram:Pic/2-3-4-exercise.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [45/55]

[Diagram:Pic/2-3-4-exerciseSoln.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [46/55]
❖ ... Insertion into 2-3-4 Trees

Insertion algorithm:


insert(tree,item):
|  Input  2-3-4 tree, item
|  Output tree with item inserted
|
|  node=root(tree), parent=NULL
|  repeat
|  |  if node.order=4 then
|  |  |  promote = node.data[1]     // middle value
|  |  |  nodeL   = new node containing node.data[0]
|  |  |  nodeR   = new node containing node.data[2]
|  |  |  if parent=NULL then
|  |  |     make new 2-node root with promote,nodeL,nodeR
|  |  |  else
|  |  |     insert promote,nodeL,nodeR into parent
|  |  |     increment parent.order
|  |  |  end if
|  |  |  node=parent
|  |  end if
|  |  if node is a leaf then
|  |     insert item into node
|  |     increment node.order
|  |  else
|  |  |  parent=node
|  |  |  if item<node.data[0] then
|  |  |     node=node.child[0]
|  |  |  else if item<node.data[1] then
|  |  |     node=node.child[1]
|  |  |  else
|  |  |     node=node.child[2]
|  |  |  end if
|  |  end if
|  until item inserted

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [47/55]
❖ ... Insertion into 2-3-4 Trees

Variations on 2-3-4 trees …

Variation #1: why stop at 4? why not 2-3-4-5 trees? or M-way trees?

Variation #2: don't have "variable-sized" nodes
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [48/55]
❖ Red-Black Trees

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [49/55]
❖ Red-Black Trees

Red-black trees are a representation of 2-3-4 trees using BST nodes.

Link types: Advantages:
COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [50/55]
❖ Red-Black Trees

Definition of a red-black tree

Balanced red-black tree Insertion algorithm: avoids worst case O(n) behaviour

Search algorithm: standard BST search

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [51/55]
❖ ... Red-Black Trees

Representing 4-nodes in red-black trees:

[Diagram:Pic/234-rb-nodes.png]


Some texts colour the links rather than the nodes.

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [52/55]
❖ ... Red-Black Trees

Representing 3-nodes in red-black trees (two possibilities):

[Diagram:Pic/234-rb-nodes2.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [53/55]
❖ ... Red-Black Trees

Equivalent trees (one 2-3-4, one red-black):

[Diagram:Pic/234-rb-tree2.png]

COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [54/55]
❖ Summary


COMP9024 (22T0) ♢ Week 03b ♢ Search Tree Algorithms ♢ [55/55]


Produced: 16 Jan 2022