CS112_36_slides-moreTrees

CS112_36_slides-moreTrees - More on Trees Yinglin Wang CS...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
CS112 Fundamentals of Programming Abstractions More on Trees Yinglin Wang CS Dept., SJTU CS112 Fundamentals of Programming Abstractions Balanced Binary Search Trees This binary search tree has a "balanced" shape.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Unbalanced--- Binary Search Trees This tree holds the same values and has the binary search tree property, but its shape is not as well balanced as the one above. The left subtree of the root has a much greater height than the right subtree. CS112 Fundamentals of Programming Abstractions The Worst Case This is also a binary search tree holding the same values, but it is way out of balance. It is really unfortunate that:
Background image of page 2
CS112 Fundamentals of Programming Abstractions The Worst Case (1)If we use our normal InsertNode function to add the nodes, and if we happen to add them in increasing order, this is the tree we will get! CS112 Fundamentals of Programming Abstractions The Worst Case (2)This is really just a linked list, and searching for a value will have O(N) performance, rather than the o(log N) that we are hoping for. What we need is a way to keep our trees in balance as we add nodes.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Balanced Binary Search Trees : Definition First we need to define balanced. According to the definition shown, this tree is certainly balanced. CS112 Fundamentals of Programming Abstractions Another Example So is this one. We have to allow a slight difference in heights in a "balanced" tree because the ideal configuration is possible only if the number of nodes is one less than a power of two. After all, the 24 had to go somewhere!
Background image of page 4
CS112 Fundamentals of Programming Abstractions Balance Factors This one is not balanced. We will define the balance factor of a node to be the height of its right subtree minus the height of its left subtree (we could just as well subtract in the other order). Balance factors are shown for all the nodes in this tree that have factors other than zero. CS112 Fundamentals of Programming Abstractions Balanced Binary Search Trees According to our definition of balanced, balance factors of +1 or -1 or OK, but +2 or -2 indicates an unbalanced tree. To fix the problem, we find the branch where the factor goes from 2 to 1.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS112 Fundamentals of Programming Abstractions Restore Balance Operation We can restore balance, while maintaining the binary search tree property, by rotating the nodes that are connected by the branch we just identified. CS112 Fundamentals of Programming Abstractions Restore Balance Operation There is no problem rotating 20 to be below and to the left of 24. Since 24 was in the right subtree of 20, we know it is larger than 20. Everything in this subtree is larger than 15, so we can rejoin it to the main tree.
Background image of page 6
CS112 Fundamentals of Programming Abstractions Restore Balance Operation Now our tree is balanced. The only non-zero balance factors are +1. CS112 Fundamentals of
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

CS112_36_slides-moreTrees - More on Trees Yinglin Wang CS...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online