- By
**brit** - Follow User

- 260 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=?

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

}

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