Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

HEIGHT BALANCED TREES -

2-3 TREES

EXTENDED TREE.

Tree in which all empty sub trees are


replaced by new nodes that are
called external nodes
Original nodes are called internal
nodes.

EXTENDED BINARY TREE

external
node

internal
node

2-3 TREE DEFINITION

A 2-3 tree is a tree in which each


internal node(nonleaf) has either 2 or
3 children, and all leaves are at the
same level.

Every internal node is either a 2-node


or a 3-node.

2-3 TREE DEFINITION

A 2-node has one key and 2


children/subtrees.
All keys in left subtree are smaller than this
key.
All keys in right subtree are bigger than this
key.
key

Left

Right

Left

Right

2-3 TREE DEFINITION

A 3-node has 2 keys and 3 children/subtrees;


first key is smaller than second key.

All keys in left subtree are smaller than first


key.
All keys in middle subtree are bigger than
first key and smaller than second key.
All keys in right subtree are bigger than
second
key.
Key 1 key 2
20
40

Left

Middle

Right

Left

Middle

Right

EXAMPLE 2-3 TREE


8
4

1 3

15 20
5 6

2-node

17

30 40

3-node

2-3 TREE DEFINITION


struct twoThreeNode {
int first, second;
// Two data fields
twoThreeNode *left, *mid, *right; // the three children
int nodeType;
// 2 or 3 node
};

NODE STRUCTURE
No. of Children
D1
LC

D2
MC

RC

2-node uses LC, D1, and MC.


3-node uses all fields.
2
27

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

Happens when all internal nodes are 2-nodes.

MINIMUM # OF PAIRS/ELEMENTS

Number of nodes = 2h 1, where h is


tree height (excluding external nodes).
Each node has 1 (key, value) pair.
So, minimum # of pairs = 2h 1

MAXIMUM # OF PAIRS/ELEMENTS

Happens when all


internal nodes are
3-nodes.

MAXIMUM # OF PAIRS/ELEMENTS

Full degree 3 tree.


# of nodes = 30 + 31 + 32 + 33 + + 3h1

= (3h 1)/2.
Each node has 2 pairs.
So, # of pairs = 3h 1.

2-3 TREE HEIGHT BOUNDS

Number of elements:

2h 1 <= n <= 3h 1.

Height:

log3(n+1) <= h <= log2(n+1).

2-3 TREE
8
4

1 3

15 20
5 6

External nodes not


shown.

17

30 40

CASE 1: INSERT INTO A (LEAF) 2NODE


2

D1

Case 1a:

Case 1b:

v < D1

v > D1

D1

D1

CASE 2: INSERT INTO A (LEAF) 3NODE


Insert X

3
D1

D2

Let u = min(X, D1, D2)


Let v = mid(X, D1, D2)
Let w = max(X, D1, D2)

CASE 2: INSERT INTO A (LEAF) 3NODE


Split
2
v

Insert X
Let u = min(X, D1, D2)
Let v = mid(X, D1, D2)
Let w = max(X, D1, D2)

CASE 2: INSERT INTO A (LEAF) 3NODE


Insert into the
parent of the
original node
2
v

Insert X
Let u = min(X, D1, D2)
Let v = mid(X, D1, D2)
Let w = max(X, D1, D2)

CASE 2A: INSERT INTO A (LEAF) 3-NODE

(PARENT IS 2-NODE)
Case 2a(i):

v<P
2
P

3
v

3
D1

D2

2
u

2
w

CASE 2A: INSERT INTO A (LEAF) 3NODE (PARENT IS 2-NODE)


Case 2a(ii):

v>P

Insert X

Let u = min(X, D1, D2)

Let v = mid(X, D1, D2)


Let w = max(X, D1, D2)

3
D1

D2

2
u

2
w

CASE 2B: INSERT INTO A (LEAF) 3NODE (PARENT IS 3-NODE) Insert into the

parent of the node

Case 2b(i):
Insert X
Let u = min(X, D1, D2)

v<P

Let v = mid(X, D1, D2)

Let w = max(X, D1, D2)


3
P

3
D1

D2

2
u

2
w

CASE 2B: INSERT INTO A (LEAF) 3NODE (PARENT IS 3-NODE)


Insert into the

parent of the node

Case 2b(ii):
Insert X
Let u = min(X, D1, D2)

P< v < Q

2
v

Let v = mid(X, D1, D2)


Let w = max(X, D1, D2)
3
P

3
D1

D2

2
u

2
w

CASE 2B: INSERT INTO A (LEAF)Insert


3-into the
parent of the nod
NODE (PARENT IS 3-NODE)
Case 2b(iii):
Insert X
Let u = min(X, D1, D2)

P< Q < v

Let v = mid(X, D1, D2)

Let w = max(X, D1, D2)


3
P

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

Now, insert a pair with key = 7.

30 40

INSERT
8

17

15

Height increases by 1.

20
16

18

30
40

DELETE FROM A (LEAF) 3-NODE


3
D1

D2

v == D2

v == D1

D1

D2

DELETE FROM A (LEAF) 2-NODE

2
v

Propagate the
hole up until it
can be
eliminated

DELETE FROM A (LEAF) 2-NODE


CASE 1: THE HOLE HAS A 2 NODE AS A PARENT AND A 2-NODE AS SIBLING

l m

I = if leaf node
I if non leaf node

2
Q

n = if leaf node=> I,m =


n if non leaf node => l,m

DELETE FROM A (LEAF) 2-NODE


CASE 2: THE HOLE HAS A 2 NODE AS A PARENT AND A 3-NODE AS SIBLING

l m

n o

DELETE FROM A (LEAF) 2-NODE


CASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 3-NODE AS
SIBLING
3
3
3
Q T
S T
R T

l m
Q

n
3

m
Q

op

no

m n

m n

DELETE FROM A (LEAF) 2-NODE


CASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS
SIBLING

l m

o
P

DELETE FROM A (LEAF) 2-NODE


CASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS
SIBLING

3
Q

DELETE FROM A (LEAF) 2-NODE


CASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS
SIBLING

3
P

P
2

mn

m n

DELETE FROM A (LEAF) 2-NODE


CASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS
SIBLING

DELETE

Case 1: The leaf node is a 3-node

Case 2: The leaf node is a 2-node and left or right sibling is a


3-node

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

Case 3a: the right sibling is a 2-node

Case 3b: the left sibling is a 2-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.

DELETE FROM A NON-LEAF NODE

Exchange with its


predecessor or successor and
then delete

DELETE FROM A LEAF


8
2 4
1

15 20
5 6

16 1730 40

Delete the pair with key = 16.


3-node becomes 2-node.

DELETE FROM A LEAF


8
2 4
1

15 20
5 6

1
7

30 40

Delete the pair with key = 17.


Deletion from a 2-node.
Check one sibling and determine if it is a 3node.

DELETE FROM A LEAF


8
2 4
1

15 30
5 6

2
0

4
0

Delete the pair with key = 20.


Deletion from a 2-node.
Check one sibling and determine if it is a 3node.
If not, combine with sibling and parent pair.

DELETE FROM A LEAF


8
2 4
1

1
5
5 6

Delete the pair with key = 30.


Deletion from a 3-node.
3-node becomes 2-node.

30 40

DELETE FROM A LEAF


8
2 4
1

1
5
5 6

4
0

Delete the pair with key = 3.


Deletion from a 2-node.
Check one sibling and determine if it is a 3node.
If so borrow a pair and a subtree via parent

DELETE FROM A LEAF


8
2 5
1

1
5
6

4
0

Delete the pair with key = 6.


Deletion from a 2-node.
Check one sibling and determine if it is a 3node.
If not, combine with sibling and parent pair.

DELETE FROM A LEAF


8
2
1

1
5
4 5

4
0

Delete the pair with key = 40.


Deletion from a 2-node.
Check one sibling and determine if it is a 3node.
If not, combine with sibling and parent pair.

DELETE FROM A LEAF


8
2
1

4 5

9 15

Parent pair was from a 2-node.


Check one sibling and determine if it is a 3node.
If not, combine with sibling and parent pair.

DELETE FROM A LEAF


2 8

4 5

9 15

Parent pair was from a 2-node.


Check one sibling and determine if it is a 3node.
No sibling, so must be the root.
Discard root. Left child becomes new root.

DELETE FROM A LEAF


2 8

4 5

Height reduces by 1.

9 15

DELETE FROM AN INTERMEDIATE NODE


8
2 4
1

15 20
5 6

16 1730 40

Replace with in-order predecessor or


successor and delete

DELETE FROM AN INTERMEDIATE NODE


8
2 4
1

15 20
5 6

16 1730 40

Replace with in-order predecessor or


successor and delete

DELETE FROM AN INTERMEDIATE NODE


6
2 4
1

15 20
5 8

16 1730 40

Replace with in-order predecessor or


successor and delete

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

sibling is three node


parent is two node

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

You might also like