multi way search trees l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Multi-Way search Trees PowerPoint Presentation
Download Presentation
Multi-Way search Trees

Loading in 2 Seconds...

play fullscreen
1 / 82

Multi-Way search Trees - PowerPoint PPT Presentation


  • 162 Views
  • Uploaded on

Multi-Way search Trees. 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. 5. 30 70. 90 100. 25. 40 50. 75. 85. 110 120. 2 4. 10. 95. Updating.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Multi-Way search Trees' - arawn


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
multi way search trees
Multi-Way search Trees
  • 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
Example
  • A 2-3 tree storing 18 items.

20 80

5

30 70

90 100

25

40 50

75

85

110 120

2 4

10

95

updating
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
Insertion
  • Insert(3);

20 80

5

30 70

90 100

40 50

75

85

110 120

2 3 4

10 15

23 25

95

insert 3
Insert(3);
  • In mid air…

20 80

5

30 70

90 100

3

23 25

40 50

75

85

110 120

2

10 15

95

4

slide6
Done….

20 80

3 5

30 70

90 100

4

23 25

40 50

75

85

110 120

2

10 15

95

tree grows at the root
Tree grows at the root…
  • Insert(45);

20 80

3 5

30 70

90 100

4

25

40 45 50

75

85

110 120

2

10

95

slide8
New root:

45

20

80

3 5

30

70

90 100

4

25

40

50

75

85

110 120

2

10

95

delete
Delete
  • If item is not in a leaf exchange with in-order 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
Remove(80);
  • Step 1: Exchange 80 with in-order successor.

45

20

85

3 5

30

70

90 100

110 120

4

25

40

50

75

80

2

10

95

remove 8011
Remove(80);
  • Redistribute

45

20

85

3 5

30

70

95 110

120

4

25

40

50

75

90

2

10

100

some more removals
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 can spare an item: redistribute.

  • 110
delete 70
Delete(70)

45

20

85

3 5

30

75

95 110

120

4

25

40

50

90

2

10

100

new tree
New tree:
  • Delete(85) will “shrink” the tree.

45

20

95

3 5

30

85

110

120

4

25

40

50

90

100

2

10

details
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
“Shorter” 2-3 Tree

20 45

3 5

30

95 110

50 90

120

4

25

40

100

2

10

deletion summary
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
Deletion (continued)
  • If N is the root, delete it and return its child as the new root.
  • Example: Delete(8);

5

5

1

2

3

3

3 5

3

8

Return

3 5

deletion continued19
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
(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
(2,4) Tree

3 8

10 45 60

70 90 100

50 55

25

insert 38
Insert(38);

Insert(38);

45

10

60

70 90 100

3 8

25 38

50 55

insert 105
Insert(105)
  • Insert(105);

45

10

60 90

3 8

50 55

70

100 105

25 38

removal
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
removal25
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
Delete(15)

35

20

60

6

15

40 50

70 80 90

delete 1527
Delete(15)

35

20

60

6

40 50

70 80 90

continued
Continued
  • Drop item from parent

35

60

6 20

40 50

70 80 90

slide29
Fuse

35

60

6 20

40 50

70 80 90

drop item from root
Drop item from root
  • Remove root, return the child.

35 60

6 20

40 50

70 80 90

summary
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
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 red black tree black depth 3
A RedBlack tree.Black depth 3.

30

15

70

10

20

85

60

5

80

90

50

65

40

55

redblack

RedBlack

Insertion

red black trees insertion
Red Black Trees, Insertion
  • Find proper external node.
  • Insert and color node red.
  • No black depth violation but may violate the red-black parent-child relationship.
  • Let: z be the inserted node, v its parent and u its grandparent. If v is red then u must be black.
color adjustments
Color adjustments.
  • Red child, red parent. Parent has a black sibling (Zig-Zag).

a

b

u

w

v

z

Vl

Zr

Zl

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

a

b

z

u

v

w

Zr

Zl

Vl

rotation ii
Rotation II

a

  • v-middle key

b

v

u

z

w

Zl

Zr

Vr

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

a

b

u

w

v

z

Vl

Zr

color adjustment
Color adjustment
  • Red-red may move up…

a

b

u

w

v

z

Vl

Zr

Zl

red black tree
Red Black Tree
  • Insert 10 – root

10

red black tree43
Red Black Tree
  • Insert 10 – root (external nodes not shown)

10

red black tree44
Red Black Tree
  • Insert 85

10

85

red black tree45
Red Black Tree
  • Insert 15

10

85

15

red black tree46
Red Black Tree
  • Rotate – Change colors

15

10

85

red black tree47
Red Black Tree
  • Insert 70

15

10

85

70

red black tree48
Red Black Tree
  • Change Color

15

10

85

70

red black tree49
Red Black Tree
  • Insert 20 (sibling of parent is black)

15

10

85

70

20

red black tree50
Red Black Tree
  • Rotate

15

10

70

85

20

red black tree51
Red Black Tree
  • Insert 60 (sibling of parent is red)

15

10

70

85

20

60

red black tree52
Red Black Tree
  • Change Color

15

10

70

85

20

60

red black tree53
Red Black Tree
  • Insert 30 (sibling of parent is black)

15

10

70

85

20

60

30

red black tree54
Red Black Tree
  • Rotate

15

10

70

85

30

20

60

red black tree55
Red Black Tree
  • Insert 50 (sibling ?)

15

10

70

85

30

20

60

50

red black tree56
Red Black Tree
  • Insert 50 (sibling of 70 is black!)

gramps 15

15

 Parent 70

10

70

85

30

Child  30

20

60

Oops, red-red. ROTATE!

50

red black tree57
Red Black Tree
  • Double Rotate – Adjust colors

30

15

70

10

20

85

60

Child-Parent-Gramps

Middle goes to “top”

Previous top becomes child. Its right child is middles left child.

50

red black tree58
Red Black Tree
  • Insert 65

30

15

70

10

20

85

60

50

65

red black tree59
Red Black Tree
  • Insert 80

30

15

70

10

20

85

60

80

50

65

red black tree60
Red Black Tree
  • Insert 90

30

15

70

10

20

85

60

80

90

50

65

red black tree61
Red Black Tree
  • Insert 40

30

15

70

10

20

85

60

80

90

50

65

40

red black tree62
Red Black Tree
  • Adjust color

30

15

70

10

20

85

60

80

90

50

65

40

red black tree63
Red Black Tree
  • Insert 5

30

15

70

10

20

85

60

5

80

90

50

65

40

red black tree64
Red Black Tree
  • Insert 55

30

15

70

10

20

85

60

5

80

90

50

65

40

55

delete65
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 demo
  • Delete 30: Swap with 40 and delete red leaf.

30

15

70

10

20

85

60

5

80

90

50

65

40

55

slide67

40

15

70

10

20

85

60

5

80

90

50

65

55

inorder successor is black
Inorder successor is Black

Change colors along the traverse path so that the leaf to be deleted is RED.

Delete 15.

30

15

70

10

20

85

60

5

80

90

50

65

40

55

general strategy
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
slide70

P

X

T

A

B

Both children of X and T are black:

P  Black X  Red, T  Red

slide71

P

X

T

A

B

If X is a leaf we are done. Recall: x is the inorder successor!

slide72

Even though we want to proceed with X we have a red-red violation that needs to be fixed.

T has a red child.

P

X

T

C1

A

D

B

C

Zig-Zag, C1 Middle key.

slide75

T

P

C1

X

B

C

D

A

slide76
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:
slide77

Root of C is black

Otherwise, continue

X has a red child

P

X

T

C1

E

C

B

Y

A

B

Left as a drill.

slide78

P

C1

T

X

E

Y

A

C

B

slide79

30

Delete 15

15

70

10

20

85

60

5

80

90

50

65

40

55

slide80

60

Delete 15

30

70

15

85

50

65

10

20

80

90

40

55

60

5

Swap (15, 20)

30

70

20

85

50

65

10

15

80

90

40

55

5

slide81

60

Delete 15

30

70

20

85

50

65

10

15

80

90

40

55

5

Third case: (mirror image) X (15) has two black children (Nulls)

Sibling has one red and one black child.

slide82

60

Delete 15

30

70

10

85

50

65

5

20

80

90

40

55