Loading in 5 sec....

AVL tree self-adjusting treePowerPoint Presentation

AVL tree self-adjusting tree

- By
**brit** - Follow User

- 259 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about ' AVL tree self-adjusting tree' - brit

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

### AVL treeself-adjusting tree

Lai Ah Fur

AVL tree

- discovers: Adelson-Velskii and Landis
- balanced Binary search tree
- the depth of the tree: O(lg N)
- definition: An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can be differ by at most 1.

Balance factor (BF)

- 如果node T之the height of left subtree is Hl, the height of right subtree is Hr, then the BF of node T is Hl- Hr
- 高度平衡之BST: BF(t)<2 …for any node t
- AVL tree: BF(t)<2
- Full binary tree: BF=?
- complete binary tree: BF=?

78

70

70

95

63

95

63

78

90

63

73

90

73

95

51

70

51

64

51

64

36

90

55

65

36

55

64

73

65

36

55

65

插入652

0

-1

0

-1

1

-1

1

-1

0

0

0

第二種LR型

78

78

63

78

63

95

70

95

51

65

73

95

51

70

90

63

73

90

36

55

75

90

36

55

65

73

75

51

65

75

36

55

插入752

-1

1

-1

-1

第三種LR型

78

78

78

95

63

95

63

83

105

63

85

79

95

51

79

105

51

83

100

200

51

80

80

85

105

79

85

100

200

100

200

80

插入80-2

1

1

-1

第二種RL型

78

78

95

63

95

105

105

63

85

79

51

83

88

100

200

79

85

100

200

51

88

插入88-2

0

1

0

1

-1

0

-1

1

-1

第三種RL型

5

5

5

2

2

8

8

2

2

8

8

10

10

1

1

3

3

10

10

1

1

7

7

3

3

5

9

9

11

11

9

9

11

11

2

10

11

1

8

3

9

1.刪除7

RR

Del 7

或是

5

Del 7

RL

2

9

10

1

8

3

11

Insertion

- case 1: an insertion into the left subtree of the left child of X
- case 2: an insertion into the right subtree of the left child of X
- case 3: an insertion into the left subtree of the right child of X
- case 4: an insertion into the right subtree of the right child of X

case 1: single rotation left subtree較高

- .A single rotation switches the role of the parent and the child while maintaining the search order
- .rotate binary tree with left child
- staticBinaryNode withLeftChild (BinaryNode k2)
- {
BinaryNode k1=k2.left;

k2.left=k1.right;

k1.right=k2;

return k1;

}

case 2: double rotation (double L-R rotation)

- double rotate binary tree node: first left child with its right child; then, node k3 with new left child.
static BinaryNode doubleWithLeftChild(BinaryNode k3)

{

k3.left=withRightChild(k3.left);

return withLeftChild(k3);

}

case 2: double rotation

12

12

k3

K2

8

16

6

16

k1

14

8

14

4

D

10

k1

4

K3

k2

10

5

2

6

2

A

D

B

C

A

5

B

C

Insert “5”

case 3:double rotation (double R-L rotation)

- double rotate binary tree node: first right child with its left child; then, node k1 with new right child.
static BinaryNode doubleWithRightChild(BinaryNode k1)

{

k1.right=withLeftChild(k1.right);

return withRightChild(k1);

}

90

90

70

80

95

85

95

99

92

70

85

99

92

R-L type-2

80

+1

0

95

70

99

90

+1/-1

85

92

Insert 85 or 92

case 4: single rotation rightsubtree較高

- rotate binary tree with right child
- static BinaryNode withRightChild (BinaryNode k1)
{

BinaryNode k2=k1.right;

k1.right=k2.left;

k2.left=k1;

return k2;

}

exercise

- Insert the following data into the empty AVL tree, 90 80 70 60 50 40

exercise

- Insert the following data into the empty AVL tree, “Mar,May,Nov,Aug,Apr,Jane,Dec,July,Feb,June,Oct,Sept”

1

2

3-1

+2

Mar

May

May

-1

-1

+2

May

Aug

Nov

Mar

Nov

Nov

+1

Apr

Mar

Aug

+1

Insert Mar, May, Nov

Insert Aug, Apr

Jan

Apr

Insert Jan

Mar

4-1

3-2

May

-2

May

Aug

Mar

Nov

Nov

Apr

Jan

+1

Aug

Dec

July

-1

Insert Dec, July, Feb

Apr

Jan

Feb

4-2

5-1

+2

Mar

May

Aug

-1

May

Dec

Nov

Apr

Dec

Aug

-1

Nov

Jan

Jan

Apr

Feb

July

-1

Feb

July

Insert June

June

5-2

6

Mar

Jan

Jan

May

Dec

Mar

-2

Dec

July

Nov

Aug

May

Feb

July

Aug

Feb

June

-1

Nov

Apr

June

Apr

Oct

Insert Oct

How a newly arriving element enters…

- If a newly arriving element endangers the tree balance, how to rectify the problem immediately?

- By restructuring the tree locally (the AVL method) or by re-creating the tree (the DSW method)

self-adjusting tree

- But, not all elements are used with the same frequency. 在低層之node若infrequently accessed,則對於程式效能影響不大
- The strategy in self-adjusting tree is to restructure trees only by moving up the tree those elements that are used more often, creating a kind of “priority tree”

Self-restructuring tree

- Proposed by Brian Allen and Ian Munro and James Bitner
- Strategy:
- Single rotation: rotate a child about its parent if an element in a child is accessed unless it is the root
- Moving to the root: repeat the child-parent rotation until the element being accessed is in the root

splaying

- A modification of the “move to the root”
- Apply single rotation in pairs in an order depending on the links between the child, parent and grandparent. (node R is accessed)
- Case 1:node R’s parent is the root
- Case 2:homogeneous configuration: node R is the left child of its parent Q and Q is the left child of its parent P, or R and Q are both right children.
- Case 3: heterogeneous configuration: node R the right child of its parent Q and Q is the left child of its parent P, or R is the left child of Q and Q is the right child of P

3 cases of splaying

Accessing node R

Algorithm of splaying

Splaying(P,Q,R)

while R is not the root

if the R’s parent is the root

perform a singular splay, rotate R about its parent;

else if R is in homogeneous configuration with its predecessor

perform a homoegeous splay, first rotate Q about P

and then R about Q;

else if R is in heterogeneous configuration with its predecessor

perform a heterogeneous splay, first rotate R about Q

and then about P;

The problem of splaying

- Splaying is a strategy focusing upon the elements rather than the shape of the tree. It may perform well in situation in which some elements are used much more frequently than others
- If the elements near the root are accessed with about the same frequency as elements on the lowest levels, then splaying may not be the best choice.

semisplaying

- A modification that requires only one rotation for a homogeneous splay and continues splaying with the parent of the accessed node.

Example of semisplaying

(a)-(c) accessing T and restructuring the tree with semisplaying

(c ) -(d)accessing T again

Download Presentation

Connecting to Server..