0% found this document useful (0 votes)
18 views29 pages

L4 - Balanced Binary Trees (Moodle Version)

The document covers advanced data structures, specifically focusing on balanced binary search trees (BBST) and their various types such as AVL trees and splay trees. It discusses the need for balancing to ensure logarithmic-time performance for operations like search, insert, and remove, and outlines restructuring algorithms and rules for maintaining balance. Additionally, it provides examples and strategies for insertion and removal in AVL trees, as well as splay operations in splay trees.

Uploaded by

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

L4 - Balanced Binary Trees (Moodle Version)

The document covers advanced data structures, specifically focusing on balanced binary search trees (BBST) and their various types such as AVL trees and splay trees. It discusses the need for balancing to ensure logarithmic-time performance for operations like search, insert, and remove, and outlines restructuring algorithms and rules for maintaining balance. Additionally, it provides examples and strategies for insertion and removal in AVL trees, as well as splay operations in splay trees.

Uploaded by

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

WRAV301/WHAV401

Advanced Data Structures 3.1

Lecture 4: Balanced Binary Search Trees


Chapter 4

5 March 2025
Admin
 Quiz 3?
 Assignment 2 – include code (4 methods) as appendix
 WHAV401 students – challenge coming soon…
Admin: Assignment 1 feedback
 What is an academic reflection?
• Opportunity to evaluate yourself, in the context of the module
content/assignment
• Show evidence of learning
• Requires critical thinking – ask yourself:
- Do I understand the content?
- Was my solution a good solution?
- Is there a better solution?
 It is not…
• A description of the solution (or problem)
• Module feedback
3. Binary Search Trees: Recap
 For each node, n
• childleft < n and
• childright > n

Figure 3.4
3. Binary Search Trees: Recap
 h = height of tree
• Best case is h = log n
• Worst case is h = n

 find is O(h)
 insert is O(h)
 remove is O(h)

  want h as small as possible


Figure 3.7
  need way to limit h
3. Binary Search Trees: Recap
 h = height of tree
• Best case is h = log n
• Worst case is h = n

 find is O(h)
 insert is O(h)
Figure 3.8
 remove is O(h)

  want h as small as possible


  need way to limit h

Figure 3.9
4. Balanced Binary Search
Trees: Introduction
 Need improvement on BST
• Limit height

 Restructured BST while being used


= balanced binary search tree
• Guarantee logarithmic-time performance
for searches and updates

Figure 3.10
4.1 BBST: Ranks and Rotations
 Want the heights of sub-trees to be more or less the same
• i.e. balanced
 Rank, r(v) for a node v in binary tree T
• Where r(v) is either the height of T or a value related to the height of
T

 Balance is enforced by adhering to certain rules


• if rules broken (insertion, removals), then tree needs to be
restructured

 Restructuring consists of one or more rotations


4.1 BBST: Restructure Algorithm
 restructure(x):
• x has parent y and
grandparent z - (x, y, z) b

• (a, b, c) = in-order
listing of (x, y, z)
• (T0, T1, T2, T3) = in-order a c

listing of subtrees of x, y
&z

• Replace subtree at z T0 T1 T2 T3

with:
4.1 BBST: Ranks and Rotations

Figure 4.3
4.1 BBST: Ranks and Rotations

Figure 4.3
4.1 BBST: Restructure example

relabel:
in-order:
(z, zy,x)y  x b, c)
 (a,
a b
z label sub-trees rotate y

b a c
y z x
b
T
0
c
x T T T T
a c
T 0 1 2 3
1

T T
T T T T
2 3
0 1 2 3
4.1 BBST: Restructure example
also known as a double rotation
can be thought as 1st rotating x over y then z

relabel:
in-order:
(z, z
x, y)  (a,xb, y
 c)
a
z label sub-trees rotate b
x

c
y a c
b
T z y
0
b
x
a c
T
3
T T T T
0 1 2 3
T T
1 2
T T T T
0 1 2 3
4.2 BBST: AVL Trees
 Rank-based

 Has height-based property


• For every internal node, v, the
height of the child sub-trees may
not differ by more than 1 from
each other

 The height of an AVL Tree is O(log


n) Figure 4.4
4.2 BBST: AVL Trees - Insertion
 Insert as would for a binary search tree
• Possibly increases height of node inserted beneath (and all
ancestors), by 1
• Can cause a violation

 If If a violation
insertion intooccurred, willAVL
a balanced be on path from inserted node to root
Tree causes it to become
unbalanced, need to
restructure
• Note that a balanced AVL
Tree implies all subtrees also
balanced Figure 4.5
 How to fix?
4.2 BBST: AVL Trees - Insertion
 If inserted w, then

 Search and Repair Strategy


• Find first node z (on path from w to root) that is unbalanced
• Choose y as child of z with highest height
• Choose x as child of y with highest height (if tie, choose w’s
ancestor)
• Restructure by calling restructure(x)

 Single restructure restores entire AVL Tree to balanced state


4.2 BBST: AVL Trees – Insertion
example 4
4 4
5
4 4

 balanced  balanced 4  balanced


4 4
1 2
7 3
1 2 7
4
1 2 5 3

z= 8 y=
7 8 7 7 0
1 3 1 1 2 2
3 1
5 2
8
1
3 5 8 3 4 7
y= 2 x = 8 z= 8
2 0 8 2 0 8
1 1 1 1
4 1
6 1
insert 49
4
2
6 restructure 4 6 8
x= (x)
9 2 8
8 2 8 1
2
4
sub-tree heights 9
label
from
insertz,
newusing
largest
node
usual
child
to root,
method
y, largest
find 1stchild sub-tree heights
unbalanced
x
4.2 BBST: AVL Trees - Removal
 Remove node w, do same for regular binary search tree
• May cause tree to become unbalanced
 To repair
• Locate first subtree, starting from parent of removed node w that is
unbalanced
• Label it z
• Choose y as child of z with highest height
• Label a child of y as x as
- The one with the largest height (if not same height); else
- If same height, choose child on same side of y as y was to z
• Restructure by calling restructure(x)
 A single restructure may NOT restore the AVL Tree to balanced state
 We need to continue to traverse from z to the root and search for additional
unbalanced nodes
4.2 BBST: AVL Trees - Removal

Figure 4.6
4.3/4 BBST: Other tree types
 Red-Black
• Colours nodes in the tree as red or black
• Restructures trees based on relationships between nodes’ colours

 Weak AVL
• Similar to AVL, but does not use tree-height
• Uses ranks of a node relative to another
• Moves nodes up/down ranks to balance
• Every AVL Tree is a Weak AVL Tree
• Every Weak AVL Tree can be coloured as a red-black tree
4.5 BBST: Splay Trees
 Does not keep track of height
 Instead performs a splaying operation after each access
• Every time insert, delete or search, perform splaying
• Over time, the tree tends to a balanced state
4.2 BBST: Splay Trees – zig-zig
 Node x and parent y both are
left/right children of
grandparent z

 Restructure:
• Replace z by x;
• Make y child of x;
• Make z child of y; and
• Preserve in-order
relationships

Figure 4.19
4.2 BBST: Splay Trees – zig-zag
 Child x, parent y of
grandparent z. One left child
other right child.

 Restructure:
• Replace z by x;
• Make z and y children of x;
and
• Preserve in-order
relationships

Figure 4.20
4.2 BBST: Splay Trees – zig
 Child x, parent y, no
grandparent.
• Typically when near root.

 Restructure:
• Rotate x over y;
• Make x’s children (one side)
y’s children; and
• Preserve in-order
relationships

Figure 4.21
4.2 BBST: Splay Trees –
operations
After zig-zig and zig-zag
• Decrease depth by 2

 After zig
• Decrease depth by 1

 Splay operation
• O(d)
• d = depth of tree
in-order (z, y, x) Restructure:

Example z = 14
8
Replace z by x;
Make y child of x;
zig-zig Make z child of y; and
Preserve in-order
3 10 15 10 relationships
y=

8 4 12 17 xx
z== 14
11
T2 zig-zig

3 11 6 y = 12 T
13 y = 15 12
travel to root,
1
4 T1 restructuring
5 7Restructure: zas = move
11 13 T T4xz =17
15
14
T3 T4
6 Replace z by x; 2
zig-zag
T3
Make y child Tof x; in-order
T (y, x, z)
Restructure: y = 13 z = 17
15
5 7 Make z child
Restructure: of y; and T
Restructure: Replace
1
z by2
x;
Preserve in-order
Replace z by relationships
x; T3
ReplaceMakez byz x;and y T
4

MakeMake
z and z and
y y insert 14 T x = 14 17
T
children
children of of
x; x;
and and 1 T
children of x; and T4
Preserve in-order 1
2 T 4
Preserve in-order
Preserve in-order
relationships T 3 T
relationships
relationships
4.2 BBST: Splay Trees – when to
splay
 Search(k)
• Find k at x, then Splay(x)
• Else Splay(parent(x))
 Insert(k)
• If k inserted at node, x, Splay(x)
 Delete(k)
• If k at node x, after deletion
• Splay(parent(x))
 Over time, operations are O(log n) – see proof if
interested
Tutorial Questions
Textbook Q’s
• R-4.(1, 3, 4, 6, 11)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy