Professional Documents
Culture Documents
Data Structure
Data Structure
2-3 TREES
EXTENDED TREE.
external
node
internal
node
Left
Right
Left
Right
Left
Middle
Right
Left
Middle
Right
1 3
15 20
5 6
2-node
17
30 40
3-node
NODE STRUCTURE
No. of Children
D1
LC
D2
MC
RC
3
16
25
2-3 TREE
class twoThreeTree {
public:
twoThreeTree(void);
// Constructor
~twoThreeTree(void);
// Destructor
void add(int item);
// Adds an item
void delete(int item); // Adds an item
twoThreeNode * search(int item); // Searches for an item
private:
twoThreeNode *root;
// Pointer to root node
// Private helper functions go here
};
MINIMUM # OF PAIRS/ELEMENTS
MINIMUM # OF PAIRS/ELEMENTS
MAXIMUM # OF PAIRS/ELEMENTS
MAXIMUM # OF PAIRS/ELEMENTS
= (3h 1)/2.
Each node has 2 pairs.
So, # of pairs = 3h 1.
Number of elements:
2h 1 <= n <= 3h 1.
Height:
2-3 TREE
8
4
1 3
15 20
5 6
17
30 40
D1
Case 1a:
Case 1b:
v < D1
v > D1
D1
D1
3
D1
D2
Insert X
Let u = min(X, D1, D2)
Let v = mid(X, D1, D2)
Let w = max(X, D1, D2)
Insert X
Let u = min(X, D1, D2)
Let v = mid(X, D1, D2)
Let w = max(X, D1, D2)
(PARENT IS 2-NODE)
Case 2a(i):
v<P
2
P
3
v
3
D1
D2
2
u
2
w
v>P
Insert X
3
D1
D2
2
u
2
w
CASE 2B: INSERT INTO A (LEAF) 3NODE (PARENT IS 3-NODE) Insert into the
Case 2b(i):
Insert X
Let u = min(X, D1, D2)
v<P
3
D1
D2
2
u
2
w
Case 2b(ii):
Insert X
Let u = min(X, D1, D2)
P< v < Q
2
v
3
D1
D2
2
u
2
w
P< Q < v
3
D1
D2
2
u
2
w
INSERT
4 88
1 14 3
4
5 5 6
9 9 15
Insert 8
Insert 4
Insert 9
Insert 15
Insert 1
Insert 5
Insert 3&6
Insert 17
INSERT
Insert 17
Insert 20
Insert 30
151520
Insert 40
Insert 18
&
Insert 16
171720
30
3040
INSERT
8 17
2 4
1
15
5 6
20
16
18
30 40
INSERT
8
17
15
Height increases by 1.
20
16
18
30
40
D2
v == D2
v == D1
D1
D2
2
v
Propagate the
hole up until it
can be
eliminated
l m
I = if leaf node
I if non leaf node
2
Q
l m
n o
l m
Q
n
3
m
Q
op
no
m n
m n
l m
o
P
3
Q
3
P
P
2
mn
m n
DELETE
Convert the node into a 2-node by deleting the key and adjust the
other data stored in the node.
Apply rotation from the 3-node sibling to the node from which the
data is being deleted.
Case 3: The leaf node and its adjacent sibling are 2-nodes and
the parent is a 3-node
Convert the parent into a 2-node and the sibling into a 3-node by
moving the larger of the two values in the parent to the sibling. Adjust
the pointers accordingly.
Convert the parent into a 2-node and the sibling into a 3-node by
moving the smaller of the two values in the parent to the sibling. Adjust
the pointers accordingly.
Case 4: The leaf node is a 2-node and the parent and the
sibling are also 2-nodes
Merge the parent and the sibling and propagate the hole to the
parent of parent.
15 20
5 6
16 1730 40
15 20
5 6
1
7
30 40
15 30
5 6
2
0
4
0
1
5
5 6
30 40
1
5
5 6
4
0
1
5
6
4
0
1
5
4 5
4
0
4 5
9 15
4 5
9 15
4 5
Height reduces by 1.
9 15
15 20
5 6
16 1730 40
15 20
5 6
16 1730 40
15 20
5 8
16 1730 40
Delete 50
swap with largest value on left
EXAMPLE
50
37
22
12
34
25
31
62
31
58
40
55
65
61
64
12
37
25
34
hole
here
25
34
37
55
65
61
64
67
55
62
62
58
22
12
58
40
40
31
62
hole
here
22
67
40
61
65
64
hole
here
22
31
67
12
25
34
37
58
55
61
65
64
67
40
62
58
22
31
12
40
25
61
55
34
37
58
22
31
65
64
62
67
12
25
55
34
37
65
61
64
67
Delete 22
swap with largest value on left
40
40
62
62
sibling is 2 node
parent is 3 node
58
12 31
25
34
37
55
61
65
64
58
31
67
12
25
34
37
55
61
65
64
67
50
Delete 77
50
62 70
31
62 70
31
37
22
1
2
2
5
3
4
4
0
58
5
5
6
1
65
6
4
7
8
6
7
37
22
7
7
7
9
1
2
3
4
2
5
4
0
5
5
62 70
31
22
1
2
2
5
3
4
4
0
58
5
5
65
6
4
6
1
7
8
7
9
6
7
50
50
37
58
6
1
node
parent:3
node31
sibling:2
6
4
37
22
65
6
7
78
79
1
2
2
5
62
3
4
4
0
58
5
5
6
1
65
70
6
4
6
7
78
79
50
Delete 40
50
62 70
31
62 70
31
37
22
1
2
2
5
3
4
4
0
58
5
5
6
1
65
6
4
7
8
6
7
7
7
7
9
50
58
22
2
5
34
37
1
2
2
5
3
4
58
5
6
5
1
50
62 70
31
1
2
37
22
5
5
6
1
65
6
4
6
4
7
8
6
7
7
7
7
9
62 70
7
8
6
7
65
7
7
58
22 31
7
9
1
2
2
5
34
37
5
5
6
1
65
6
4
7
8
6
7
7
7
7
9
50
contd.
62 70
58
22 31
1
2
2
5
34
37
5
5
6
1
65
6
4
7
8
6
7
7
7
7
9
62
70
50
22
31
1
2
2
5
58
34
37
5
5
6
1
65
6
4
7
8
6
7
7
7
7
9
63
70
40
50
31
1
2
45
3
4
4
4
65
58
4
7
5
5
6
1
6
4
7
8
6
7
7
7
7
9