• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 9 pages
Summary

Summary Detailed Breakdown of Balancing Trees

Document preview thumbnail
Preview 2 out of 9 pages

Further Discussion and Insight not covered in lecture slides.

Content preview

Balancing Trees


Height balanced: Difference in height of subtrees for every node is 0 or 1
Perfectly Balanced: Height balanced and all leaves are on 1, max 2 levels
Height: Number of “levels” or nodes under a specific node


Calculations:


Number of nodes on a level: 2h-1
 Where h is the level you want to check
Number of nodes on all levels for a specified tree height: n = 2h – 1
 Where h is the height of the tree


Balancing Algorithm using an array then adding elements to tree:

void balance ( T data[ ], int first, int last )
{
if(first <= last) {
int middle = (first + last)/2;
insert(data[middle]);
balance(data, first, middle-1);
balance(data, middle+1, last);
}
}



Steps (Recursive):
1. Sort Data
2. Find Middle
3. Insert Middle element
4. Repeat (2) on left half
5. Repeat (2) on right half


Complexity: O(lg n)
- Eliminates half of search space with each iteration
- Better an linked list: O(n)

, DSW Algorithm
No external data structure as tree rotations are used


Rotations:
- Left rotation: Parent becomes left child of its right child
- Right rotation: Parent becomes right child of its left child

Parent
D



Left A E Right
Child Child
E A


D D

E
A


rotateRight ( Gr, par, Ch )

if Par is not the root of the tree //Gr!=null
grandparent Gr of child Ch becomes Ch’s parent;
right subtree of Ch becomes left subtree of Ch’s parent Par;
node Ch acquires Par as its right child;




Right rotation with leaf nodes attached:
- Parent becomes right child of left child
- Left child’s right branch becomes old parent’s left branch




Left rotation with leaf nodes attached:
- Parent becomes left child of right child
- Right child’s left branch becomes old parent’s right branch

Connected book
 image
Publisher: 2012 ISBN: 9789814392785 Edition: Unknown

Document information

Summarized whole book?
No
Which chapters are summarized?
Balancing trees
Uploaded on
June 7, 2022
Number of pages
9
Written in
2020/2021
Type
Summary
$9.24

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
0
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions