0% found this document useful (0 votes)
20 views110 pages

DSA 03 Tree Structures Eng

Uploaded by

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

DSA 03 Tree Structures Eng

Uploaded by

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

Tree Structures

Contents
o Terminologies o Tree traversals o Tree
representation o Binary tree o Binary search tree
o AVL tree
o 2-3 tree, 2-3-4 tree

fit@hcmus | DSA | 2024

1
fit@hcmus | DSA | 2024 3

Some Examples

fit@hcmus | DSA | 2024

2
Organizational chart Directory tree
4

Trees
o Used to represent relationships o hierarchical in
nature
• “Parent-child” relationship exists between nodes in tree.
• Generalized to ancestor and descendant
• Lines between the nodes are called edges

o A subtree in a tree is any node in the tree together


with all of its descendants

fit@hcmus | DSA | 2024 5

fit@hcmus | DSA | 2024

3
Trees
r root

r1 r2 rk

T1 T2 Tk

subtree

Terminologies
o node: an item/element in a tree. o parent (of node n): The node
directly above node n in the tree. o child (of node n): The node
directly below node n in the tree. o root: The only node in the
tree with no parent. o leaf: A node with no children.
o path: A sequence of nodes and edges connecting a node with the
nodes below it.

fit@hcmus | DSA | 2024

4
fit@hcmus | DSA | 2024 7

Terminologies
o siblings: Nodes with common parent.
o ancestor (of node n): a node on the path from the root to n.
o descendant (of node n): a node on the path from node n to a leaf.
o subtree (of node n): A tree that consists of a child (if any) of n and
the child’s descendants.

fit@hcmus | DSA | 2024

5
fit@hcmus | DSA | 2024 9

Terminologies
o degree/order
• Order of node n: number of children of node n.
• Order of a tree: the maximum order of nodes in that tree.
o depth/level (of node n)
• If n is the root of T , it is at level 1.
• If n is not the root of T , its level is 1 greater than the level of its parent. if
node n is root:
level(n) = 1
Otherwise:
level(n) = 1 + level(parent(n))

fit@hcmus | DSA | 2024

6
10

10

Terminologies
o Height of tree: number of nodes in the longest path from the root to
a leaf.

o Height of a tree T in terms of the levels of its nodes


• If T is empty, its height is 0.
• If T is not empty, its height is equal to the maximum level of its nodes.

fit@hcmus | DSA | 2024 11

11

Terminologies
o Height of tree T:
if T is empty:
height(T) = 0
Otherwise:
height (T) = max{level(Ni)}, NiT

fit@hcmus | DSA | 2024

7
o Height of tree T:
if T is empty:
height(T) = 0
Otherwise:
height(T) = 1 + max{height(Ti)}, Ti is a subtree of T

12

12

13

fit@hcmus | DSA | 2024

8
Kinds of Trees

14

14

General Tree
o Set T of one or more nodes such that T is partitioned into disjoint
subsets
• A single node r , the root
• Sets that are general trees, called subtrees of r

fit@hcmus | DSA | 2024 15

15

fit@hcmus | DSA | 2024

9
n-ary Tree
o set T of nodes that is either empty or partitioned into disjoint
subsets:
• A single node r , the root
• n possibly empty sets that are n-ary subtrees of r

16

16

Binary Tree
o Set T of nodes that is either empty or partitioned into disjoint
subsets
• Single node r , the root
• Two possibly empty sets that are binary trees, called left and right subtrees
of r

fit@hcmus | DSA | 2024 17

fit@hcmus | DSA | 2024

10
17

Traversals

18

18

Traversal
o Visit each node in a tree exactly once.

o Many operations need using tree traversals.

o The basic tree traversals:


• Pre-order
fit@hcmus | DSA | 2024

11
• In-order
• Post-order

fit@hcmus | DSA | 2024 19

19

Pre-order Traversal
PreOrder(root)
{ if root is empty
Do_nothing;
Visit root; //Print, Add, ...
//Traverse every Childi.
PreOrder(Child0);
PreOrder(Child1);

PreOrder(Childk-1);
}
20

20

Post-order Traversal
PostOrder(root)
{ if root is empty
Do_nothing;
//Traverse every Childi

fit@hcmus | DSA | 2024

12
PostOrder(Child0);
PostOrder(Child1);

PostOrder(Childk-1);
Visit at root; //Print, Add, ...
}

fit@hcmus | DSA | 2024 21

21

In-order Traversal
InOrder(root)
{ if root is empty
Do_nothing;
//Traverse the child at the first position
InOrder(Child0);
Visit at root;
//Traverse other children
InOrder(Child1);
InOrder(Child2);

InOrder(Childk-1);
}

22

22

fit@hcmus | DSA | 2024

13
Traversals

fit@hcmus | DSA | 2024 23

23

fit@hcmus | DSA | 2024

14
24

24

Examples

In-order

Pre-order
• abdeijcfgkh

• dbiejafckgh

Post-order
•dijebfkghca

fit@hcmus | DSA | 2024

15
fit@hcmus | DSA | 2024 25

25

Examples

26

26

fit@hcmus | DSA | 2024

16
Tree Representation

fit@hcmus | DSA | 2024 27

27

28

28

fit@hcmus | DSA | 2024

17
fit@hcmus | DSA | 2024 29

29

Tree Representation
Info Eldest Child Next Sibling

1 a 2 0
2 b 4 3
3 c 6 0
4 d 0 5
5 e 9 0
6 f 0 7
7 g 11 8
8 h 0 0
9 i 0 10
10 j 0 0

fit@hcmus | DSA | 2024

18
11 k 0 0
30

30

Tree Representation
A
Root

B C

D E F G H

I J K

fit@hcmus | DSA | 2024 31

31

fit@hcmus | DSA | 2024

19
Tree Representation
Info Parent

1 a 0
2 b 1

3 c 1

4 d 2

5 e 2

6 f 3

7 g 3

8 h 3

9 i 5

10 j 5

11 k 7

32

32

Binary Tree

fit@hcmus | DSA | 2024 33

33

Binary Tree
o Set T of nodes that is either empty or
partitioned into disjoint subsets.
• Single node r, the root
fit@hcmus | DSA | 2024

20
• Two possibly empty sets that are binary trees, called left and right subtrees of
r.

o Other definition: A rooted binary tree has a root node and every
node has at most two children.

34

34

Types of Binary

Tree o Complete binary

tree

o Full binary tree

o Perfect binary tree

o Heap

fit@hcmus | DSA | 2024 35

35

fit@hcmus | DSA | 2024

21
Perfect Binary Tree
o A perfect binary tree is a binary tree in which
• all interior nodes have two children
• and all leaves have the same depth or same level.

o In a perfect binary tree of height h, all nodes that are at a level less
than h have two children each.

36

36

fit@hcmus | DSA | 2024

22
Perfect Binary Tree
o If T is empty, T is a perfect binary tree of height 0.
o If T is not empty and has height h > 0, T is a perfect binary tree if its
root’s subtrees are both perfect binary trees of height h – 1.
a

b c

d e f g

fit@hcmus | DSA | 2024 37

37

Complete Binary Tree


o A complete binary tree of height h is a binary tree that is perfect
down to level h – 1, with level h filled in from left to right.

o In a complete binary tree every level, except possibly the last, is


completely filled, and all nodes in the last level are as far left as
possible.

o Other definition: A complete binary tree is a perfect binary tree


whose rightmost leaves (perhaps all) have been removed.
fit@hcmus | DSA | 2024

23
38

38

Complete Binary Tree


o A binary tree is complete if
• All nodes at level h – 2 and above have two
children each, and
• When a node at level h – 1 has children, all
nodes to its left at the same level have two
children each, and
• When a node at level h – 1 has one child, it
is a left child

fit@hcmus | DSA | 2024 39

39

Full Binary Tree


o A full binary tree (sometimes referred to as
a proper binary tree or a plane binary tree)
is a binary tree in which every node has
either 0 or 2 children.

fit@hcmus | DSA | 2024

24
o A full binary tree is either:
• A single vertex.
• A tree whose root node has two subtrees, both of which are
full binary trees.

40

40

Heap
o A heap is a complete binary tree that
either is empty or
• Its root
• (Max-heap): Contains a value greater than or
equal to the value in each of its children, and
• (Min-heap): Contains a value less than or
equal to the value in each of its children, and
• Has heaps as its subtrees

fit@hcmus | DSA | 2024 41

41

fit@hcmus | DSA | 2024

25
Number of Nodes
Given a binary tree T height of h. o What is

the maximum number of nodes? o What is

the minimum number of nodes?

42

42

Height of Tree
Given a binary tree T with n nodes. o What

is the maximum height of that tree? o What

is the minimum height of that tree?

fit@hcmus | DSA | 2024 43

fit@hcmus | DSA | 2024

26
43

Binary Search Tree

45

45

Binary Search Tree


o A binary search tree is a binary tree in which each node n has
properties:
• n ’s value greater than all values in left subtree TL • n ’s value
less than all values in right subtree TR
• Both TR and TL are binary search trees.

fit@hcmus | DSA | 2024

27
fit@hcmus | DSA | 2024 46

46

47

47

fit@hcmus | DSA | 2024

28
Binary Search Tree

fit@hcmus | DSA | 2024 48

48

Operations
o Insert (a key) o Search (a key) o
Remove (a key) o Traverse o Sort
(based on key value) o Rotate (Left
rotation, Right rotation)

49

fit@hcmus | DSA | 2024

29
49

Insertion
Insert (root, Data)
{ if (root is NULL) {
Create a new_Node containing Data
This new_Node becomes root of the tree
}
//Compare root's key with Key if
root's Key is less than Data's Key
Insert Key to the root's RIGHT subtree
else if root's Key is greater than Data's Key
Insert Key to the root's LEFT subtree
else
Do nothing //Explain why?
}

fit@hcmus | DSA | 2024 50

50

Insertion
Beginning with an empty binary search tree, what binary search tree is
formed when you insert the following values in the order given?
15, 5, 12, 8, 23, 1, 17, 21

15, 20, 40, 25, 70, 90, 80, 55, 60, 65, 30, 75

fit@hcmus | DSA | 2024

30
9, 1, 4, 2, 3, 9, 5, 8, 6, 7, 4

51

51

Insertion
Beginning with an empty binary search tree, what binary search tree is
formed when you insert the following values in the order given?
• W, T, N, J, E, B, A
• W, T, N, A, B, E, J
• A, B, W, J, N, T, E
• B, T, E, A, N, W, J

fit@hcmus | DSA | 2024 52

52

Search
Search (root, Data)
{ if (root is NULL) { return
NOT_FOUND;
}

fit@hcmus | DSA | 2024

31
//Compare root's key with Key if
root's Key is less than Data's Key
Search Data in the root's RIGHT subtree
else if root's Key is greater than Data's Key
Search Data in the root's LEFT subtree
else return FOUND //Explain why? }

53

53

Deletion
o When we delete a node, three possibilities arise.
o Node to be deleted:
• is leaf:
• Simply remove from the tree.
• has only one child:
• Copy the child to the node and delete the child • has two
children:
• Find in-order successor (predecessor) S_Node of the node. •
Copy contents of S_Node to the node and delete the S_Node.

fit@hcmus | DSA | 2024 54

54

fit@hcmus | DSA | 2024

32
Traversals
o Pre-order:
Node - Left - Right

o In-order:
Left - Node - Right

o Post-order:
Left - Right - Node

55

55

Traversals
What are the pre-order, in-order and post-order traversals of this binary

search tree?
fit@hcmus | DSA | 202456

fit@hcmus | DSA | 2024

33
56

57

57

fit@hcmus | DSA | 2024

34
Left Rotation
P Left rotation at P P

A B

B A
T1 T3

T2 T3 T1 T2

fit@hcmus | DSA | 2024 58

58

Left Rotation

fit@hcmus | DSA | 2024

35
59

59

Right Rotation
Right rotation at P

P P

B A

A B
T3 T1

T1 T2 T2 T3

fit@hcmus | DSA | 2024 60

60

fit@hcmus | DSA | 2024

36
40

37 50

36 55
45

65

61

61

fit@hcmus | DSA | 2024 62

fit@hcmus | DSA | 2024

37
62

Efficiency of Binary Search Tree


Operations

63

63

Very Bad Binary Search Tree


Beginning with an empty binary search tree, what binary search tree is
formed when inserting the following values in the order given?
2, 4, 6, 8, 10, 12, 14, 18, 20

fit@hcmus | DSA | 2024

38
fit@hcmus | DSA | 2024 64

64

AVL Tree

65

65

AVL Tree
o Named for its inventors, (Georgii) Adelson-Velsky and (Evgenii) Landis

o Invented in 1962 (paper “An algorithm for organization of information”).

fit@hcmus | DSA | 2024

39
o AVL Tree is a self-balancing binary search tree where
• for ALL nodes, the difference between height of the left subtrees and the right
subtrees cannot be more than one. (height invariant, or balance invariant).

fit@hcmus | DSA | 2024 66

66

AVL Tree

fit@hcmus | DSA | 2024

40
Which is the AVL Tree?

67

67

AVL Tree
o A balanced binary search tree
• Maintains height close to the minimum

fit@hcmus | DSA | 2024

41
• After insertion or deletion, check the tree is still AVL tree – determine
whether any node in tree has left and right subtrees whose heights differ by
more than 1

o Can search AVL tree almost as efficiently as minimum-height binary


search tree.

fit@hcmus | DSA | 2024 68

68

Cases of Height Invariant


Violation
o Left-Left case

o Left-Right case

o Right-Right case

o Right-Left case

69

69

fit@hcmus | DSA | 2024

42
Cases of Height Invariant
Violation
o Left-Left case
12

8 18

5 17

fit@hcmus | DSA | 2024 70

70

Cases of Height Invariant


Violation
o Left -Right case
12

8 18

5 17

fit@hcmus | DSA | 2024

43
71

71

fit@hcmus | DSA | 2024 72

72

fit@hcmus | DSA | 2024

44
73

73

fit@hcmus | DSA | 2024

45
fit@hcmus | DSA | 2024 74

74

Violation Resolving
o Right-Right case:
• Left rotation at un-balanced node.

o Right-Left case:
• Right rotation at un-balanced node’s right child.
• Left rotation at un-balanced node.

75

75

fit@hcmus | DSA | 2024

46
Violation

Resolving o Right-

Right case:
P Left rotation at P P

a b
Q

b a
T3 h+1
T1 h

T2 h T3 h+1 T1 h T2 h

fit@hcmus | DSA | 2024 76

76

fit@hcmus | DSA | 2024

47
Violation

Resolving o Right-

77

Left case:
• Right rotation at Q P

• Left rotation at P
a
Q

b
T1 h

c h
T4

fit@hcmus | DSA | 2024


T2 h T3 h-1

48
Violation

Resolving o Right-

fit@hcmus | DSA | 2024 78

78

79

Left case:

fit@hcmus | DSA | 2024

49
Violation

Resolving o Right-

fit@hcmus | DSA | 2024 80

80

fit@hcmus | DSA | 2024

50
Violation

Resolving o Right-

81

81

fit@hcmus | DSA | 2024

51
fit@hcmus | DSA | 2024 82

82

Violation Resolving
o Left-Left case:
Single rotation
• Right rotation at un-balanced node.

o Left-Right case:
Double rotation
• Left rotation at un-balanced node’s left child.
• Right rotation at un-balanced node.

fit@hcmus | DSA | 2024

52
83

83

Violation
Resolving

Source: Wikipedia

fit@hcmus | DSA | 2024 84

84

Insertion
Beginning with an empty AVL tree, perform step-by-step the insertion
of the following values in the order given
15, 5, 12, 8, 23, 1, 17, 21

9, 1, 4, 2, 3, 9, 5, 8, 6, 7, 4

fit@hcmus | DSA | 2024

53
85

85

Red-Black Tree

fit@hcmus | DSA | 2024 87

87

Red-Black Tree
o Invented in 1972 by Rudolf Bayer.
o Red-Black tree is a binary search tree with the following rules:
• Every node has a color either red or black.
• The root of the tree is always black.
• There are no two adjacent red nodes (A red node cannot have a red parent
or red child).
• Every path from a node (including root) to any of its descendants NULL
nodes has the same number of black nodes.
• All leaf nodes are black nodes.

88

88

fit@hcmus | DSA | 2024

54
Red
-Black Tree

fit@hcmus | DSA | 2024 89

89

fit@hcmus | DSA | 2024

55
AA Tree

90

90

fit@hcmus | DSA | 2024

56
AA Tree
o Invented by Arne Anderson in a work published in 1993 (Balanced
Search Tree Made Simple).

o Two concepts:
• Level:
• Number of LEFT links from that nodes to a NULL node.

• Horizontal link:
• The link between parent and its child node having the same level.

fit@hcmus | DSA | 2024 91

91

AA Tree

15
Level 2

Level 1 5 10 20

Horizontal link

fit@hcmus | DSA | 2024

57
92

92

AA Tree
o AA tree is a binary search tree with the following rules:
• The level of every leaf node is one.
• The level of every left child is exactly one less than that of its parent.
• The level of every right child is equal to or one less than that of its parent.
Horizontal link must be a RIGHT link.
• The level of every right grandchild is strictly less than that of its grandparent.
There is no two consecutive horizontal links.
• Every node of level greater than one has two children.

fit@hcmus | DSA | 2024 93

93

AA Tree
30 70

15 50 60 85

35 40 55 65 80 90
5 10 20

fit@hcmus | DSA | 2024

58
94

94

AA Tree and -Red


Black Tree

6 27

3 4 7 8 15 40

fit@hcmus | DSA | 2024 95

95

fit@hcmus | DSA | 2024

59
AA Tree and -Red
Black Tree

30 70

15 50 60 85

35 40 55 65 80 90
5 10 20

96

96

fit@hcmus | DSA | 2024

60
2-3 Tree and 2-3-4 Tree

fit@hcmus | DSA | 2024 98

98

2-3 Tree

99

99

fit@hcmus | DSA | 2024

61
2-3-4 Tree

fit@hcmus | DSA | 2024 100

100

2-node, 3-node, 4-node


o A 2-node (has two children) must contain single data item greater than left
child’s item(s) and less than right child’s item(s).

o A 3-node (has three children) must contain two data items, S and L, such
that
• S is greater than left child’s item(s) and less than middle child’s item(s);
• L is greater than middle child’s item(s) and less than right child’s item(s).

o A 4-node (has our children) must contain three data items S, M, and L that
satisfy:
• S is greater than left child’s item(s) and less than middle-left child’s item(s)
• M is greater than middle-left child’s item(s) and less than middle-right child’s item(s); •
L is greater than middle-right child’s item(s) and less than right child’s item(s).

fit@hcmus | DSA | 2024

62
101

101

2-node,-node,-node
4
3
2-node 3-node

4-node

fit@hcmus | DSA | 2024 102

102

2-3 Tree, 2-3-4 Tree


o A 2-3 tree is not a binary tree. Neither is a 2-3-4 tree.

o A 2-3 tree, a 2-3-4 tree are never taller than a minimum-height


binary tree.

fit@hcmus | DSA | 2024

63
103

103

2-3 Tree
o Invented by John Hopcroft in 1970.

o 2-3 tree is a tree in which


• Every internal node is either a 2-node or a 3-node.

• Leaves have no children and may contain either one or two data items.

fit@hcmus | DSA | 2024 104

104

2-3-4 Tree
o 2-3-4 tree is a tree in which
• Every internal node is a 2-node, a 3-node or a 4-node.

• Leaves have no children and may contain either one, two or three data
items.

fit@hcmus | DSA | 2024

64
105

105

Insertion (2-3 Tree)

fit@hcmus | DSA | 2024 113

113

Inserting Data into


-3 Tree
a
2

114

114

fit@hcmus | DSA | 2024

65
Inserting Data into
-3 Tree
a
2

fit@hcmus | DSA | 2024 115

115

Inserting Data into a 2-3 Tree


o Splitting a leaf in a 2-3 tree when the leaf is a
• (a) left child; (b) right child

116
fit@hcmus | DSA | 2024

66
116

fit@hcmus | DSA | 2024

67
Inserting Data into a 2-3 Tree
o Splitting an internal node in a 2-3 tree when the node is a (a) left
child; (b) right child

fit@hcmus | DSA | 2024 117

117

Inserting Data into a 2-3 Tree


o Splitting the root of a 2-3 tree

fit@hcmus | DSA | 2024

68
Examples

118

118

o After inserting 39 into the tree

fit@hcmus | DSA | 2024 119

119

Examples
The steps for inserting 38 into the tree
(a) The located node has no room;
(b) the node splits;
(c) the resulting tree

fit@hcmus | DSA | 2024

69
Examples

Next: inserting 37 into the tree


120

120

After inserting 37 into the tree

Original tree
Resulting tree
Next: inserting 36 into the tree
fit@hcmus | DSA | 2024 121

121

fit@hcmus | DSA | 2024

70
Examples

Examples
The steps for inserting 36 into the tree

122

122

the resulting tree

fit@hcmus | DSA | 2024 123

123

fit@hcmus | DSA | 2024

71
Examples

Insertion (2-3-4 Tree)

124

124

fit@hcmus | DSA | 2024

72
Inserting Data into a 2-3-4 Tree
o

Insertion algorithm splits a node by moving one of its items up to its


parent node

o Splits 4-nodes as soon as it encounters them on the way down the tree
from the root to a leaf

fit@hcmus | DSA | 2024 125

125

Inserting Data into a 2-3-4 Tree


o Splitting a 4-node root during insertion into a 2-3-4 tree

fit@hcmus | DSA | 2024

73
Inserting Data into a 2-3-4 Tree
o

126

126

Splitting a 4-node whose parent is a 2-node during insertion into a


2-3-4 tree, when the 4-node is a (a) left child; (b) right child

fit@hcmus | DSA | 2024 127

127

Inserting Data into a 2-3-4 Tree


o Splitting a 4-node whose parent is a 3-node during insertion into a
2-3-4 tree, when the 4-node is a (a) left child

fit@hcmus | DSA | 2024

74
Inserting Data into a 2-3-4 Tree
o

128

128

Splitting a 4-node whose parent is a 3-node during insertion into a


2-3-4 tree, when the 4-node is a (b) middle child

fit@hcmus | DSA | 2024 129

129

fit@hcmus | DSA | 2024

75
Inserting Data into a 2-3-4 Tree
o

Inserting Data into a 2-3-4 Tree


o Splitting a 4-node whose parent is a 3-node during insertion into a
2-3-4 tree, when the 4-node is a (c) right child

130

130

fit@hcmus | DSA | 2024

76
Examples
o

Inserting 20 into a 2-3-4 tree


(a) the original tree;
(b) after splitting the node;
(c) after inserting 20

fit@hcmus | DSA | 2024 131

131

Examples
o After inserting 50 and 40 into the tree

Next: inserting 70 into the tree


132
fit@hcmus | DSA | 2024

77
Examples
o

132

The steps for inserting 70 into the tree


(a) after splitting the 4-node;
(b) after inserting 70

Next: inserting 80, and 15 into the tree


fit@hcmus | DSA | 2024 133

133

Examples
o After inserting 80 and 15 into the tree

fit@hcmus | DSA | 2024

78
Examples
o

Next: inserting 90 into the tree


134

134

The steps for inserting 90 into the tree

Next: inserting 100 into the tree


fit@hcmus | DSA | 2024 135

135

Examples
o The steps for inserting 100 into the tree

fit@hcmus | DSA | 2024

79
Examples
o

136

136

fit@hcmus | DSA | 2024

80
Deletion

fit@hcmus | DSA | 2024 138

138

Removing Data from


-3 Tree
a2

139

139

fit@hcmus | DSA | 2024

81
Removing Data from
-3 Tree
a2

fit@hcmus | DSA | 2024 140

140

Removing Data from


-3 Tree
a2

fit@hcmus | DSA | 2024

82
141

141

Removing Data from a 2-3 Tree


(a) Redistributing values;

(b) merging a leaf;

fit@hcmus | DSA | 2024 142

142

Removing Data from a 2-3 Tree


(c) redistributing values and children
(d) merging internal nodes

fit@hcmus | DSA | 2024

83
143

143

Removing Data from a 2-3 Tree


(e) deleting the root

fit@hcmus | DSA | 2024 144

144

fit@hcmus | DSA | 2024

84
Examples
(a) A 2-3 tree;
(b), (c), (d), (e) the steps for removing 70;

145

145

Examples
o the resulting tree

fit@hcmus | DSA | 2024 146

fit@hcmus | DSA | 2024

85
146

Examples
(a), (b), (c) The steps for removing 100 from the tree; (d)

the resulting tree


147

147

Examples
The steps for removing 80 from the tree

fit@hcmus | DSA | 2024

86
fit@hcmus | DSA | 2024 148

148

Examples
The steps for removing 80 from the tree

149

149

fit@hcmus | DSA | 2024

87
Removing Data from a 2-3-4
Tree
o Removal algorithm has the same beginning as removal algorithm for
a 2-3 tree
o Locate the node n that contains the item I you want to remove.
o Find I ’s in-order successor and swap it with I so that the removal
will always be at a leaf. o If leaf is either a 3-node or a 4-node,
remove I .

fit@hcmus | DSA | 2024 153

153

fit@hcmus | DSA | 2024

88
B-Tree

158

158

B-Tree
o Original B-Tree proposed by R. Bayer and E. McCreigh in 1972.
o A B-Tree is a specialized multi-way tree designed especially for use
on external disk.
o Improved versions of B-Trees were later proposed in 1982 by
Huddleston and Mehlhorn, and by Maier and Salveter.
o B-tree variants are used mostly today as index structures in database
applications.

fit@hcmus | DSA | 2024 159

159

Definition
o (Knuth’s definition) A B-tree of order m is a tree which satisfies the
following properties:
fit@hcmus | DSA | 2024

89
• Every node has at most m children.
• Every non-leaf node (except root) has at least ⌈m/2⌉ child nodes.
• The root has at least two children if it is not a leaf node.
• A non-leaf node with k children contains k − 1 keys.
• All leaves appear in the same level and carry no information.

o The number m should be (always) odd.

162

162

fit@hcmus | DSA | 2024 163

163

fit@hcmus | DSA | 2024

90
Example

Is this a B-
tree?

164

164

Height of B-Tree
• The maximum number of keys in a B-tree of order m and height h:
root m–1
level 2 m(m – 1)
level 3 m2(m – 1)
. . .
level h mh-1(m – 1)

• So, the total number of keys is


(1 + m + m2 + m3 + … + mh-1)(m – 1) = [(mh
– 1)/ (m – 1)] (m – 1) = mh–1

fit@hcmus | DSA | 2024 165

165
fit@hcmus | DSA | 2024

91
Operations
o Insert (a key) o
Remove (a key)

166

166

fit@hcmus | DSA | 2024

92
Insertion

fit@hcmus | DSA | 2024 167

167

Insertion
o B-tree insertion is a generalization of 2-3 tree insertion.
o Insert K into B-tree of order m.
• We find the insertion point (in a leaf) by doing a search.
• If there is room then insert K.
• Else, promote the middle key to the parent, split the node into nodes around the
middle key.

o If the splitting backs up to the root, then


• Make a new root containing the middle key.
o Note
• The tree grows from the leaves, balance is always maintained.

168

168

Example
o Perform step-by-step the insertion of the following values to the
initially empty B-tree order 5
fit@hcmus | DSA | 2024

93
1, 12, 8, 2, 25, 5, 14, 28, 17

fit@hcmus | DSA | 2024 169

169

1 12 8 2 25 5 14 28 17

Example
1 128 12 1
2 8 2
Insert 1 Insert 12 Insert 8 Insert 2

170

170

1 12 8 2 25 5 14 28 17

Example
1 25
8

1 2 12 25

fit@hcmus | DSA | 2024

94
Insert 25

fit@hcmus | DSA | 2024 171

171

1 12 8 2 25 5 14 28 17

Example

1 2 5 12 14 25 28

Insert 5, 14, 28

172

172

fit@hcmus | DSA | 2024

95
1 12 8 2 25 5 14 28 17

Example

1 2 5 12 14 17 25 28

8 17

1 2 5 12 14 25 28
Insert 17

fit@hcmus | DSA | 2024 173

173

fit@hcmus | DSA | 2024

96
Example
Given a B-tree order 5:

Deletion

174

174

Deletion
o If the key to be deleted is not in a leaf, swap it with its successor (or
predecessor). Then delete the key from the leaf.

o If leaf contains more than the minimum number of keys, then one
can be deleted with no further action.

fit@hcmus | DSA | 2024 175

175

Deletion
o If the node contains the minimum number of keys, consider the two
immediate siblings of the parent node:
fit@hcmus | DSA | 2024

97
• If one of these siblings has more than the minimum
number of keys, then redistribute one key from this
sibling to the parent node, and one key from the parent
to the deficient node.
• If both immediate siblings have exactly the minimum number of keys, then merge the
deficient node with one of the immediate sibling node and one key from the parent
node.

o If this leaves the parent node with too few keys, then the process is
propagated upward.

176

176

12 29 52

2 7 9 15 22 31 43 56 69 72

Remove 2

fit@hcmus | DSA | 2024 177

177

Example
Given a B-tree order 5:
fit@hcmus | DSA | 2024

98
Example
Given a B-tree order 5:

12 29 52

7 9 15 22 31 43 56 69 72

After removing 2

178

178

fit@hcmus | DSA | 2024

99
Example
Given a B-tree order 5:

12 29 52

7 9 15 22 31 43 56 69 72

Remove 52

fit@hcmus | DSA | 2024 179

179

Example
Given a B-tree order 5:
12 29 56

7 9 15 22 31 43 52 69 72

Remove 52 (replaced by 56)

fit@hcmus | DSA | 2024

100
Example
Given a B-tree order 5:

12 29 56

7 9 15 22 31 43 69 72

180

180

fit@hcmus | DSA | 2024 181

181

Example
Given a B-tree order 5:

fit@hcmus | DSA | 2024

101
Example
Given a B-tree order 5:

12 29 56

7 9 15 22 31 43 69 72

Remove 72

182

182

fit@hcmus | DSA | 2024 183

183

fit@hcmus | DSA | 2024

102
Example
Given a B-tree order 5:

12 29 56

7 9 15 22 31 43 69

Not enough key. Merge nodes.

Example
Given a B-tree order 5:
12 29

7 9 15 22 31 43 56 69

184

184

fit@hcmus | DSA | 2024

103
Example
Given a B-tree order 5:

12 29

7 9 15 22 31 43 56 69

Remove 22

fit@hcmus | DSA | 2024 185

185

Example
Given a B-tree order 5:
1 29
2

7 9 1 3 4 56 6
5 1 3 9
(Redistribute
) Borrow key from neighboring nodes

fit@hcmus | DSA | 2024

104
Example
Given a B-tree order 5:

186

186

fit@hcmus | DSA | 2024 187

187

fit@hcmus | DSA | 2024

105
Example
Given a B-tree order 5:

12 31

7 9 15 29 43 56 69

Example
Given a B-tree order 5:
16

3 8 28 48

1 2 5 7 12 14 25 26 29 45 52 53

Remove 8 Merge 2 child nodes of 8

188

188

fit@hcmus | DSA | 2024 189

189
fit@hcmus | DSA | 2024

106
Example
Given a B-tree order 5:
16

3 28 48

1 2 5 7 12 14 25 26 29 45 52 53

Not enough keys at node 3.

Example
Given a B-tree order 5
3 16 28 48

1 2 5 7 12 14 25 26 29 45 52 53

A new root node


Decrease height of the tree

190

190

fit@hcmus | DSA | 2024

107
Quiz
Given the following B-tree order 5
• Remove 28, then remove 48
17

3 8 28 48

1 2 5 7 12 14 16 25 26 29 45 52 53 55 68

fit@hcmus | DSA | 2024 191

191

B-Trees & Efficiency


o Used in Mac, NTFS, OS2 for file structure.
o Allow insertion and deletion into a tree structure, based on logmn
property, where m is the order of the tree.
o The idea is that you leave some key spaces open. An insertion of a
new key is done using available space (most cases).
• Less dynamic than our typical Binary Tree
• Ideal for disk-based operations.

fit@hcmus | DSA | 2024

108
193

193

B-Trees & Efficiency


o In practical applications, B-Trees of large order (e.g., m = 128)
are more common than low-order B-Trees such as 2-3 trees.

fit@hcmus | DSA | 2024 194

194

fit@hcmus | DSA | 2024

109
Questions and Answers

195

195

fit@hcmus | DSA | 2024

110

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