data structure

59
HEIGHT BALANCED TREES - 2-3 TREES

description

understanding trees in data structure

Transcript of data structure

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

6

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

LeftMiddle

Right

20 40

LeftMiddle

Right

EXAMPLE 2-3 TREE

15 20

8

4

1 3 5 6 30 409 17

2-node 3-node

2-3 TREE DEFINITION

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

};

NODE STRUCTURE

2-node uses LC, D1, and MC. 3-node uses all fields.

No. of Children

D1 D2

LC MC RC

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 + … + 3h-

1

= (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

15 20

17

8

4

1 3 5 6 30 409

External nodes not shown.

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

2

D1

v < D1

3

v D1

v > D1

3

D1 v

Case 1a: Case 1b:

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

3

D1 D2

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) 3-NODE

2

u

2

w

2

v

Split

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) 3-NODE

2

u

2

w

2

v

Insert into the parent of the original node

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)

3D1 D2

2P

m2

u

3v P

m

v < P

2w

Case 2a(i):

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

3D1 D2

2P

l 2u

3P v

v > P

2w

l

Insert X

Let u = min(X, D1, D2)

Let v = mid(X, D1, D2)

Let w = max(X, D1, D2)

Case 2a(ii):

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

3D1 D2

3P Q

2u

2v

v < P

2w

2Q

m n m n

2P

Insert into the parent of the node

Insert X

Let u = min(X, D1, D2)

Let v = mid(X, D1, D2)

Let w = max(X, D1, D2)

Case 2b(i):

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

3D1 D2

3P Q

2u

2P

P< v < Q

2w

2Q

l n n

2v

Insert into the parent of the node

l

Insert X

Let u = min(X, D1, D2)

Let v = mid(X, D1, D2)

Let w = max(X, D1, D2)

Case 2b(ii):

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

3D1 D2

3P Q

2u

2P

P< Q < v

2w

2v

l m m

2Q

Insert into the parent of the node

l

Insert X

Let u = min(X, D1, D2)

Let v = mid(X, D1, D2)

Let w = max(X, D1, D2)

Case 2b(iii):

INSERTInsert 8Insert 4Insert 9Insert 15Insert 1Insert 5

84 8

94 9 151 41 5 Insert 3&61 3 5 6

Insert 17

INSERT

1 3 5 630 40

Insert 17

4

179

15

8 Insert 20

17 20

Insert 30

15 20

30

Insert 40Insert 18 &Insert 16

INSERT

• Now, insert a pair with key = 7.

1

2 4

5 6 30 4093 16

15

18

20

8 17

INSERT

• Height increases by 1.

30 40

1 93 16

15

18

2062

5 7

4 17

8

3

D1 D2

2

D1

2

D2

v == D2 v == D1

DELETE FROM A (LEAF) 3-NODE

2

v

DELETE FROM A (LEAF) 2-NODE

Propagate the hole up until it

can be eliminated

DELETE FROM A (LEAF) 2-NODECASE 1: THE HOLE HAS A 2 NODE AS A PARENT AND A 2-NODE AS SIBLING

2P

2Q

l m n

3P Q

l m n

2Q

2P

nl m

I = φ if leaf nodeI ≠ φ if non leaf node

n = φ if leaf node=> I,m = φ n ≠ φ if non leaf node => l,m ≠ φ

DELETE FROM A (LEAF) 2-NODECASE 2: THE HOLE HAS A 2 NODE AS A PARENT AND A 3-NODE AS SIBLING

2P

3Q R

l m n

2R

3P Q

ol mo n

2Q

2P

2R

l m n o

DELETE FROM A (LEAF) 2-NODECASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 3-NODE AS SIBLING

3Q T

3R S

l m n

p

o

3R T

2Q

l m n

p2S

o

3S T

3Q R

ol m

p

n

3Q T

3R S

pm n

l

o

3Q R

3S T

o

l

m pn

3Q S

2Tl

pm o

2R

n

DELETE FROM A (LEAF) 2-NODECASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS SIBLING

3P R

2Q

l m n

o

2R

3P Q

l m n

o

DELETE FROM A (LEAF) 2-NODECASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS SIBLING

3Q R

2P

nl m

o

2R

3P Q

l m n

o

DELETE FROM A (LEAF) 2-NODECASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS SIBLING

3P Q

2R

mn o

l

2P

3Q R

m n o

l

DELETE FROM A (LEAF) 2-NODECASE 3: THE HOLE HAS A 3 NODE AS A PARENT AND A 2-NODE AS SIBLING

3P R

2Q

om n

l

2P

3Q R

om n

l

DELETE Case 1: The leaf node is a 3-node

Convert the node into a 2-node by deleting the key and adjust the other data stored in the node.

Case 2: The leaf node is a 2-node and left or right sibling is a 3-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 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.

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 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

• Delete the pair with key = 16.

• 3-node becomes 2-node.

15 20

8

1

2 4

5 6 30 409 16 173

DELETE FROM A LEAF

• Delete the pair with key = 17.

• Deletion from a 2-node.

• Check one sibling and determine if it is a 3-node.

• If so borrow a pair and a subtree via parent node.

15 20

8

1

2 4

5 6 30 4093 17

DELETE FROM A LEAF

• Delete the pair with key = 20.

• Deletion from a 2-node.

• Check one sibling and determine if it is a 3-node.

• If not, combine with sibling and parent pair.

15 30

8

1

2 4

5 6 93 20

40

DELETE FROM A LEAF

• Delete the pair with key = 30.

• Deletion from a 3-node.

• 3-node becomes 2-node.

30 40

8

1

2 4

5 6 93

15

DELETE FROM A LEAF8

1

2 4

5 6 93

15

40

• Delete the pair with key = 3.

• Deletion from a 2-node.

• Check one sibling and determine if it is a 3-node.

• If so borrow a pair and a subtree via parent node.

DELETE FROM A LEAF8

1

2 5

94

15

40

• Delete the pair with key = 6.

• Deletion from a 2-node.

• Check one sibling and determine if it is a 3-node.

• If not, combine with sibling and parent pair.

6

DELETE FROM A LEAF8

1 4 5 9

15

40

• Delete the pair with key = 40.

• Deletion from a 2-node.

• Check one sibling and determine if it is a 3-node.

• If not, combine with sibling and parent pair.

2

DELETE FROM A LEAF8

1 4 5

• Parent pair was from a 2-node.

• Check one sibling and determine if it is a 3-node.

• If not, combine with sibling and parent pair.

2

9 15

DELETE FROM A LEAF

1 4 5

• Parent pair was from a 2-node.

• Check one sibling and determine if it is a 3-node.

• No sibling, so must be the root.

• Discard root. Left child becomes new root.

9 15

2 8

DELETE FROM A LEAF

1 4 5

• Height reduces by 1.

9 15

2 8

DELETE FROM AN INTERMEDIATE NODE

• Replace with in-order predecessor or successor and delete

15 20

8

1

2 4

5 6 30 409 16 173

DELETE FROM AN INTERMEDIATE NODE

• Replace with in-order predecessor or successor and delete

15 20

8

1

2 4

5 6 30 409 16 173

DELETE FROM AN INTERMEDIATE NODE

• Replace with in-order predecessor or successor and delete

15 20

6

1

2 4

5 8 30 409 16 173

EXAMPLE Delete 50swap with largest value on left

40

31 62

22 37 58 65

12 25 55 6134 64 67

50

31 62

22 37 58 65

12 25 55 6134 40 64 67

40

31 62

22 58 65

12 25 55 6134 37

64 67

40

62

22 31

58 65

12 25 55 6134 37

64 67

hole here

hole here

hole here

40 62

22 31

58 65

12 25 55 6134 37

64 67

Delete 22

swap with largest value on left40 62

12 31 58 65

25 55 6134 37

64 67

40 62

31 58 65

12 25

55 6134 37

64 67

sibling is 2 nodeparent is 3 node

40 62

22 31

58 65

12 25 55 6134 37

64 67

parent:3node

sibling:2node

Delete 7750

31 62 70

22 37 58 65

12

25

55

61

34

40

64

67

78

77

79

50

31 62 70

22 37 58 65

12

25

55

61

34

40

64

67

78

79

50

31 62 70

22 37 58 65

12

25

55

61

34

40

64

67

78 79

50

31 62

22 37 58 65 70

12

25

55

61

34

40

64

67

78 79

50

31 62 70

22 37 58 65

12

25

55

61

34

40

64

67

78

77

79

Delete 4050

31 62 70

22 37 58 65

12

25

55

61

34

64

67

78

77

7950

31 62 70

22 58 65

12

25

55

61

34 37

64

67

78

77

79

50

62 70

22 31

58 65

12

25

55

61

34 37

64

67

78

77

79

50

62 70

22 31

58 65

12

25

55

61

34 37

64

67

78

77

79

62

50 70

22 31

58 65

12

25

55

61

34 37

64

67

78

77

79

sibling is three nodeparent is two node

contd….

63

40 50

70

31

58 65

12

55

61

34

64

67

78

77

79

45

44

47