SlideShare a Scribd company logo
Multi-Way search Trees
1. 2-3 Trees:
a. Nodes may contain 1 or 2 items.
b. A node with k items has k + 1 children
c. All leaves are on same level.
Example
• A 2-3 tree storing 18 items.
20 80
30 70

5
2 4

10

25

40 50 75

90 100
85

95 110 120
Updating
• Insertion:
• Find the appropriate leaf. If there is only
one item, just add to leaf.
• Insert(23); Insert(15)
• If no room, move middle item to parent and
split remaining two items among two
children.
• Insert(3);
Insertion
• Insert(3);
20 80
5
2 3 4

10 15

30 70
23 25 40 50 75

90 100
85

95 110 120
Insert(3);
• In mid air…
20 80
5

30 70

90 100

3
2

4

10 15 23 25 40 50 75

85

95 110 120
Done….
20 80
3 5
2

30 70

4 10 15 23 25

40 50 75

90 100
85

95 110 120
Tree grows at the root…
• Insert(45);
20 80
3 5
2

4

30 70
10

25 40 45 50 75

90 100
85

95 110 120
• New root:

45
20

3 5
2

4

80
30

10

25 40

70
50

90 100
75

85

95 110 120
Delete
• If item is not in a leaf exchange with inorder successor.
• If leaf has another item, remove item.
• Examples: Remove(110);
• (Insert(110); Remove(100); )
• If leaf has only one item but sibling has two
items: redistribute items. Remove(80);
Remove(80);

• Step 1: Exchange 80 with in-order
successor.
45
20

3 5
2

4

85
30

10

25 40

70
50

90 100
75

80

95

110 120
• RedistributeRemove(80);
45
20

3 5
2

4

85
30

10

25 40

70
50

95 110
75

90

100

120
Some more removals…
• Remove(70);
Swap(70, 75);
Remove(70);
“Merge” Empty node with sibling;
Join parent with node;
Now every node has k+1 children except that one
node has 0 items and one child.
Sibling 95 110 can spare an item: redistribute.
Delete(70)
45
20

3 5
2

4

85
30

10

25 40

75
50

95 110
90

100

120
New tree:
• Delete(85) will “shrink” the tree.
45
20

3 5
2

4

95
30

10

85

25 40 50

110
90

100

120
Details
•
•
•
•
•

1. Swap(85, 90) //inorder successor
2. Remove(85) //empty node created
3. Merge with sibling
4. Drop item from parent// (50,90) empty Parent
5. Merge empty node with sibling, drop item from
parent (95)
• 6. Parent empty, merge with sibling drop item.
Parent (root) empty, remove root.
“Shorter” 2-3 Tree
20 45

3 5
2

4

30
10

25 40

95 110
50 90

100

120
Deletion Summary
• If item k is present but not in a leaf, swap
with inorder successor;
• Delete item k from leaf L.
• If L has no items: Fix(L);
• Fix(Node N);
• //All nodes have k items and k+1 children
• // A node with 0 items and 1 child is
possible, it will have to be fixed.
Deletion (continued)
• If N is the root, delete it and return its child
as the new root.
• Example: Delete(8);
5

5

1

3

2

8

3

3

Return

35

35
Deletion (Continued)
• If a sibling S of N has 2 items distribute
items among N, S and the parent P; if N is
internal, move the appropriate child from S
to N.
• Else bring an item from P into S;
• If N is internal, make its (single) child the
child of S; remove N.
• If P has no items Fix(P) (recursive call)
(2,4) Trees
• Size Property: nodes may have 1,2,3 items.
• Every node, except leaves has size+1
children.
• Depth property: all leaves have the same
depth.
• Insertion: If during the search for the leaf
you encounter a “full” node, split it.
(2,4) Tree
10

3 8

45

25

60

50 55

70 90 100
Insert(38);
Insert(38);
45
10
60

3 8

25 38

50 55

70 90 100
Insert(105)
• Insert(105);
45
10

3 8

25 38

60 90

50 55

70

100 105
Removal
• As with BS trees, we may place the node to
be removed in a leaf.
• If the leaf v has another item, done.
• If not, we have an UNDERFLOW.
• If a sibling of v has 2 or 3 items, transfer an
item.
• If v has 2 or 3 siblings we perform a
transfer
Removal
• If v has only one sibling with a single item
we drop an item from the parent to the
sibling, remove v. This may create an
underflow at the parent. We “percolate” up
the underflow. It may reach the root in
which case the root will be discarded and
the tree will “shrink”.
Delete(15)
35
20
6

60

15

40 50

70 80 90
Delete(15)
35
20
6

60

40 50

70 80 90
Continued
• Drop item from parent
35
60
6 20

40 50

70 80 90
Fuse
35
60
6 20

40 50

70 80 90
Drop item from root
• Remove root, return the child.

35 60
6 20

40 50

70 80 90
Summary
• Both 2-3 trees and 2-4 trees make it very
easy to maintain balance.
• Insertion and deletion easier for 2-4 tree.
• Cost is waste of space in each node. Also
extra comparison inside each node.
• Does not “extend” binary trees.
Red-Black Trees
• Root property: Root is BLACK.
• External Property: Every external node is
BLACK (external nodes: null nodes)
• Internal property: Children of a RED node
are BLACK.
• Depth property: All external nodes have the
same BLACK depth.
A RedBlack tree.
Black depth 3.
30
15
10

70
20

85

60

5

50
40

65
55

80

90
RedBlack
Insertion
Red Black Trees, Insertion
1. Find proper external node.
2. Insert and color node red.
3. No black depth violation but may violate
the red-black parent-child relationship.
4. Let: z be the inserted node, v its parent
and u its grandparent. If v is red then u
must be black.
Color adjustments.

• Red child, red parent. Parent has a black
sibling (Zig-Zag).
a
b

u
w

v
z
Vl
Zl

Zr
Rotation
• Z-middle key. Black height does not
change! No more red-red.
a
b

z
u

v

Vl

Zl

Zr

w
Color adjustment II
a
b

u
w

v
Vr
z

Zl

Zr
Rotation II
• v-middle key

a

b

v
u

z

Zl

Zr

Vr

w
Recoloring
• Red child, red parent. Parent has a red
sibling.
a

b

u
w

v
z
Vl

Zr
Color adjustment
• Red-red may move up…
a
b

u
w

v
z
Vl
Zl

Zr
Red Black Tree
• Insert 10 – root
10
Red Black Tree
• Insert 10 – root (external nodes not shown)
10
Red Black Tree
• Insert 85
10
85
Red Black Tree
• Insert 15
10
85
15
Red Black Tree
• Rotate – Change colors
15
10

85
Red Black Tree
• Insert 70
15
10

85
70
Red Black Tree
• Change Color
15
10

85
70
Red Black Tree
• Insert 20 (sibling of parent is black)
15
10

85
70
20
Red Black Tree
• Rotate
15
10

70
20

85
Red Black Tree
• Insert 60 (sibling of parent is red)
15
10

70
85

20
60
Red Black Tree
• Change Color
15
10

70
85

20
60
Red Black Tree
• Insert 30 (sibling of parent is black)
15
10

70
85

20
60
30
Red Black Tree
• Rotate
15
10

70
85

30
20

60
Red Black Tree
• Insert 50 (sibling ?)
15
10

70
85

30
20

60
50
Red Black Tree
• Insert 50 (sibling of 70 is black!)
15

gramps 15

10

70
Child  30

Oops, red-red.
ROTATE!

85

30

20

60
50

 Parent 70
Red Black Tree
• Double Rotate – Adjust colors
30
15
10
Child-Parent-Gramps

70
20

60
50

Middle goes to “top”
Previous top becomes child. Its right

85
Red Black Tree
• Insert 65
30
15
10

70
20

85

60
50

65
Red Black Tree
• Insert 80
30
15
10

70
20

85

60
50

65

80
Red Black Tree
• Insert 90
30
15
10

70
20

85

60
50

65

80

90
Red Black Tree
• Insert 40
30
15
10

70
20

85

60
50
40

65

80

90
Red Black Tree
• Adjust color
30
15
10

70
20

85

60
50
40

65

80

90
Red Black Tree
• Insert 5
30
15
10

70
20

85

60

5

50
40

65

80

90
Red Black Tree
• Insert 55
30
15
10

70
20

85

60

5

50
40

65
55

80

90
Delete
• We first note that a red node is either a leaf or
must have two children.
• Also, if a black node has a single child it must be a
red leaf.
• Swap X with inorder successor.
• If inorder successor is red, (must be a leaf) delete.
If it is a single child parent, delete and change its
child color to black. In both cases the resulting
tree is a legit red-black tree.
Delete demo
• Delete 30: Swap with 40 and delete red leaf.
30
15
10

70
20

85

60

5

50
40

65
55

80

90
40
15
10
5

70
20

85

60
50

65
55

80

90
Inorder successor is Black
Change colors along the traverse path so that the leaf to
be deleted is RED.
Delete 15.

30
15

10

70
20

85

60

5

50
40

65
55

80

90
General strategy
• As you traverse the tree to locate the
inorder successor let X be the current node,
T its sibling and P the parent.
• Color the root red.
• Retain: “the color of P is red.”
• If all children of X and T are black:
• P  Black, X  Red, T  Red
P
X
A

Both children of X and T are black:
P  Black X  Red, T  Red

T

B
P
X

A

T

B

If X is a leaf we are done. Recall: x is the inorder
successor!
Even though we
Even though we
want to proceed
want to proceed
with X we have aa
with X we have
red-red violation
red-red violation
that needs to be
that needs to be
fixed.
fixed.

P

T has aared child.
T has red child.

X

T

C1

A

D
B

Zig-Zag, C1 Middle key.

C
Note: black
Note: black
depth remains
depth remains
unchanged!
unchanged!

C1
P

T

X

A

B

C

D
B will become P’s
B will become P’s
right child. No
right child. No
change in depth.
change in depth.

Third case
P
X

T

C1

A
B
C
T middle key.

D
T
C1

P
X

A

B

C

D
• If both children of T are red select one of
the two rotations.
• If the right child of X is red make it the new
parent (it is on the inorder-successor path).
• If the left child of X is red:
Root of C is black
Root of C is black
Otherwise, continue
Otherwise, continue
X has aared child
X has red child

P
X
C1

C
B
Y

A
B
Left as a drill.

T

E
P
C1

T
X
E

Y
A
B

C
30

Delete 15
15
10

70
20

85

60

5

50
40

65
55

80

90
60

Delete 15
30

70

15
10

50
20

40

80

55

30

70

20
10
5

50
15

90

60

Swap (15, 20)

5

85

65

40

85

65
55

80

90
60

Delete 15
30

70

20
10

50
15

40

85

65
55

80

90

5

Third case: (mirror image) X (15) has two black children (Nulls)
Sibling has one red and one black child.
60

Delete 15
30

70

10
5

50
20

40

85

65
55

80

90

More Related Content

PPT
Advanced data structures and implementation
DOCX
Sci Take Home Test
PPTX
Bag The Beans Indicatior 5 Grade
PPTX
Leaf Collection 1
PPTX
Leaf Collection 1
PPT
Updated summer tree id practice cyber
PPTX
Tree ID and Ecology
PDF
WoodyPlants
Advanced data structures and implementation
Sci Take Home Test
Bag The Beans Indicatior 5 Grade
Leaf Collection 1
Leaf Collection 1
Updated summer tree id practice cyber
Tree ID and Ecology
WoodyPlants

Similar to Trees (20)

PPTX
Balanced Tree(AVL Tree,Red Black Tree)
PPTX
Balanced Tree (AVL Tree & Red-Black Tree)
PPTX
Red black trees
PPTX
Red black trees
PPT
PPTX
Red Black Tree,Red black tree, Cases-insertion, Deletion
PPT
PPT
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
PPT
B tree and application B tree and application B tree and application
PPT
BINARY TREE REPRESENTATION.ppt
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
PPTX
Red black trees1109
PPT
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
PPT
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
PDF
Red black trees
PPTX
Trees in data structure
PPT
lecture 14
PDF
Red black tree
PDF
02_balanced_trees for pitt dsa yayay.pdf
Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
Red black trees
Red black trees
Red Black Tree,Red black tree, Cases-insertion, Deletion
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
B tree and application B tree and application B tree and application
BINARY TREE REPRESENTATION.ppt
Chapter 7 - Binary Search Tree in the context of DSA.pdf
Red black trees1109
RedBlackTrees_2.pptmmmmmmmmmmmmmmmmmmmmmmmmmm
RedBlackTrees_2.pptNNNNNNNNNNNNNNNNNNNNNN
Red black trees
Trees in data structure
lecture 14
Red black tree
02_balanced_trees for pitt dsa yayay.pdf
Ad

More from Susant Sahani (20)

PDF
systemd
PDF
systemd
PDF
How to debug systemd problems fedora project
PDF
Systemd vs-sys vinit-cheatsheet.jpg
PDF
Systemd cheatsheet
PDF
Systemd
PDF
Systemd for administrators
PDF
Pdf c1t tlawaxb
PDF
Systemd mlug-20140614
PDF
Summit demystifying systemd1
PDF
Systemd evolution revolution_regression
PDF
Systemd for administrators
PDF
Systemd poettering
PPT
Interface between kernel and user space
PPT
Week3 binary trees
PDF
Van jaconson netchannels
PPT
Synchronization linux
PPT
Demo preorder-stack
PDF
Bacnet white paper
PDF
Api presentation
systemd
systemd
How to debug systemd problems fedora project
Systemd vs-sys vinit-cheatsheet.jpg
Systemd cheatsheet
Systemd
Systemd for administrators
Pdf c1t tlawaxb
Systemd mlug-20140614
Summit demystifying systemd1
Systemd evolution revolution_regression
Systemd for administrators
Systemd poettering
Interface between kernel and user space
Week3 binary trees
Van jaconson netchannels
Synchronization linux
Demo preorder-stack
Bacnet white paper
Api presentation
Ad

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Modernizing your data center with Dell and AMD
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation theory and applications.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Weekly Chronicles - August'25 Week I
Modernizing your data center with Dell and AMD
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation theory and applications.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Big Data Technologies - Introduction.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
A Presentation on Artificial Intelligence
NewMind AI Monthly Chronicles - July 2025
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

Trees