6 views

Uploaded by karunakarkavuri

- Data Structure Algorithms
- Doc on Implementing and Displaying Standard Hierarchy in Sap Hana
- Cse102 Data-structures-And-Algorithms Eth 2.00 Ac29
- treeA
- MIT6_851S12_ps1s
- Quad Axis Separation Framework for Bounding-Volume Hierarchies Construction
- Binary Tree
- 7trees.ps
- Ryan Riegel, Alexander Gray and Gordon Richards- Massive-Scale Kernel Discriminant Analysis: Mining for Quasars
- Run Time Environments
- MongoDB Data Models Guide
- B-Tree
- Species Delimitation Plugin Manual
- data_structures_in_java.pdf
- icdm08b.pdf
- Computer Chess and Search [Marsland 1991].pdf
- The Techno Man Cer
- AGBU 515_Session 7.pdf
- Data Structure & Algorithm Analysis
- lect3

You are on page 1of 24

The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case We want a tree with small height A binary tree with N node has height at least (log N) Thus, our goal is to keep the height of a binary search tree O(log N) Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree.

AVL tree

Height of a node The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum height of its children plus 1

Note that this definition of height is different from the one we defined previously (we defined the height of a leaf as zero previously).

AVL tree

An

for every node in the tree, the height of the left and right subtrees differ by at most 1.

AVL property violated here

AVL tree

Let x be the root of an AVL tree of height h Let Nh denote the minimum number of nodes in an AVL tree of height h Clearly, Ni Ni-1 by definition We have N N N 1

h h 1 h2

The boundary conditions are: N1=1 and N2 =2. This implies that h = O(log Nh). Thus, many operations (searching, insertion, deletion) on an AVL tree will take O(log N) time.

N h 2i N h 2

Rotations

When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property. This is done using single rotations or double rotations.

x y B A

Before Rotation

y x

After Rotation

Rotations

Since

an insertion/deletion involves adding/deleting a single node, this can only increase/decrease the height of some subtree by 1 Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2. Rotations will be applied to x to restore the AVL tree property.

Insertion

First, insert the new key as a new leaf just as in ordinary binary search tree Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation [next slide]. For insertion, once we perform a rotation at a node x, we wont need to perform any rotation at any ancestor of x.

Insertion

Let

x be the node at which left(x) and right(x) differ by more than 1 Assume that the height of x is h+3 There are 4 cases

of left(left(x)) is h+1 single rotate with left child Height of right(left(x)) is h+1 double rotate with left child

Height

of right(right(x)) is h+1 single rotate with right child Height of left(right(x)) is h+1 double rotate with right child

Height

Note: Our test conditions for the 4 cases are different from the code shown in the textbook. These conditions allow a uniform treatment between insertion and deletion.

Single rotation

The new key is inserted in the subtree A. The AVL-property is violated at x height of left(x) is h+2 height of right(x) is h.

Single rotation

The new key is inserted in the subtree C. The AVL-property is violated at x.

AVL Tree

5

5

x

8

8

4 0.8 1

y

4

A Insert 0.8 3 5

4 0.8 8

After rotation

Double rotation

The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x. x-y-z forms a zig-zag shape

Double rotation

The new key is inserted in the subtree B1 or B2. The AVL-property is violated at x.

x

C

8

AVL Tree

3

y 8

4

3.5

Insert 3.5 4 5

3.5

8

After Rotation

An Extended Example

Insert 3,2,1,4,5,6,7, 16,15,14

3 3 Fig 1 2 Fig 2 2 1 Fig 3 1 Fig 5 4 1 Fig 6 2 2 1 Fig 4

Single rotation

3

2

Single rotation

3

4

2 1 3

Single rotation

1

4

5 Fig 7 4 2 3

4

5 Fig 8

4

6

Single rotation

5

3 Fig 9 2 1 3 5 6 4 1

2 3

5

6 Fig 10 7

6

7 Fig 11

4 2 1 3 5 Fig 12 4

6

7 16

Double rotation

2 1

4 2

6

3

5 Fig 13 15 7 16 1 3

6

5 15 16 Fig 14 7

4 2

Double rotation

4 2 7 3 6 15

6

3 5 15 16 7 Fig 15 14 1

5 Fig 16

14

16

Deletion

Delete a node x as in ordinary binary search tree. Note that the last node deleted is a leaf. Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, perform an appropriate rotation at x. There are 4 cases as in the case of insertion. For deletion, after we perform a rotation at x, we may have to perform a rotation at some ancestor of x. Thus, we must continue to trace the path until we reach the root.

Deletion

On

closer examination: the single rotations for deletion can be divided into 4 cases (instead of 2 cases)

Two cases for rotate with left child Two cases for rotate with right child

In both figures, a node is deleted in subtree C, causing the height to drop to h. The height of y is h+2. When the height of subtree A is h+1, the height of B can be h or h+1. Fortunately, the same single rotation can correct both cases.

In both figures, a node is deleted in subtree A, causing the height to drop to h. The height of y is h+2. When the height of subtree C is h+1, the height of B can be h or h+1. A single rotation can correct both cases.

Rotations in deletion

There

are 4 cases for single rotations, but we do not need to distinguish among them. There are exactly two cases for double rotations (as in the case of insertion) Therefore, we can reuse exactly the same procedure for insertion to determine which rotation to perform

- Data Structure AlgorithmsUploaded byUmakant Yadav
- Doc on Implementing and Displaying Standard Hierarchy in Sap HanaUploaded bySyed Ali
- Cse102 Data-structures-And-Algorithms Eth 2.00 Ac29Uploaded bynetgalaxy2010
- treeAUploaded byABC
- MIT6_851S12_ps1sUploaded bydjoseph_1
- Quad Axis Separation Framework for Bounding-Volume Hierarchies ConstructionUploaded byJournal of Computing
- Binary TreeUploaded bynabil aldbeeb
- 7trees.psUploaded byscratchname
- Ryan Riegel, Alexander Gray and Gordon Richards- Massive-Scale Kernel Discriminant Analysis: Mining for QuasarsUploaded byManasvfd
- Run Time EnvironmentsUploaded bysindhura2258
- MongoDB Data Models GuideUploaded byYến Nhi Trần
- B-TreeUploaded byAntara Sharma
- Species Delimitation Plugin ManualUploaded byRicardo García
- data_structures_in_java.pdfUploaded bySrivenu Chinna
- icdm08b.pdfUploaded bySergio Santoyo
- Computer Chess and Search [Marsland 1991].pdfUploaded byPrakashRoxx
- The Techno Man CerUploaded byaryionut
- AGBU 515_Session 7.pdfUploaded byNinuca Chanturia
- Data Structure & Algorithm AnalysisUploaded byBadrul Amin
- lect3Uploaded byRodrigo Castillo
- cs301MCQSBYMOAZUploaded byMuhammad Rameez Khalid
- Chapter 1 - Network AnalysisUploaded bySeble Getachew
- paper 274_274Uploaded byUbiquitous Computing and Communication Journal
- 1_a.pdfUploaded bymina_samei
- Application Note EnUS 1461607051Uploaded byFrancisco
- Channel Estimator and Aliasing Canceller for Equalizing and Decoding Non-Cyclic Prefixed Single-Carrier Block Transmission via MIMO-OfDM ModemUploaded byArun Kumar
- Y05-1019Uploaded byKyel Lopez
- PeopleSoft Object ReferenceUploaded byDibakar Saha
- 09Uploaded byZeeshan Shakoor
- RecursiveUploaded byvijayakumarvarma

- HeapUploaded byYousuf Tanvir
- Chapter 10Uploaded byBobDover
- 11.2 Tree Binary TreeUploaded byAndi A Fauzy
- Tree Preorder 2Uploaded byakg299
- B-TreeUploaded byRaghuRamMullapudi
- Tree Traversals.docxUploaded byAditya Gaur
- 43 Minimum Spanning TreesUploaded byThomas Harrison
- daaUploaded byAnkur Gupta
- BST vs. HeapsUploaded bym251563
- 18569_TREES [Read-Only] [Compatibility Mode].pdfUploaded byRickyJohn
- Red Black TreesUploaded byKavya Rastogi
- avl2Uploaded bySakshi Bachhety
- bst treeUploaded bysaiteja1234
- 2-3TreesUploaded byHuy Hóm Hỉnh
- trees-FNLUploaded bypreeti200726
- H12-solution[www.alirezaweb.com].pdfUploaded byVahid Esmaeilzadeh
- unit3-spUploaded byhrsli
- Trees Data StructureUploaded byGoldy Batra
- Chapter 4 TreesUploaded byLogaNathan BalasuBaramaniam
- Computer Notes - Data Structures - 11Uploaded byecomputernotes
- Binary Search TreeUploaded byWellington Lins
- 10 DS and Algorithm Session 14Uploaded byAshish Srivastava
- 28-binarytrees3Uploaded byTan Rathana
- Lecture l Bal TreesUploaded bymaster008
- Binary search tree.pptxUploaded byBikram Kesharee Nayak
- Binary TreeUploaded bySameer Kodilkar
- X-Fast and Y-Fast TriesUploaded byhrishipisal
- Digital Search TreeUploaded byAnonymous 6beD2TsLW
- Data structures and algorithms -HeapsUploaded byNicolas Salcedo
- CSCI 211 Test 2 ReviewUploaded byjaxakroiter