DA09 - Balanced Search Trees CSC 3102 1 B.B. Karki, LSU Why...

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

View Full Document Right Arrow Icon
B.B. Karki, LSU 1 CSC 3102 Balanced Search Trees
Background image of page 1

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

View Full DocumentRight Arrow Icon
B.B. Karki, LSU 2 CSC 3102 Why Balancing Search Trees Binary search tree (BST): Its nodes contain the elements of a set of orderable items, one element per node, so that all elements in the left subtree are smaller than the element in the subtree’s root and all the elements in the right tree greater than it. With BST, the time of efficiency of searching, insertion, and deletion are all in Θ (log n ) This is true only in the average case In worst case, the efficiency is Θ ( n ) How to avoid the worst-case degeneracy for BST? Balancing a BST can lead to significant performance improvements. Find a structure that preserves the good properties of the classical BST. Given a tree with 1000 nodes, the worst case is only 10 for a nearly complete tree.
Background image of page 2
B.B. Karki, LSU 3 CSC 3102 Balancing Approaches Two approaches to balance the search trees: Sections 6.3 and 7.4 of the text book. Instance simplification: Transform an unbalanced binary search tree to a balanced one. AVL tree Red-black tree If insertion/deletion violates the balance requirement, the tree is restructured by rotation to restore the balance required. Representation change: Allow more than one element in a node of a search tree. 2-3 trees, 2-3-4 trees, and B-trees They differ in the number of elements admissible in a single node but all are perfectly balanced.
Background image of page 3

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

View Full DocumentRight Arrow Icon
B.B. Karki, LSU 4 CSC 3102 AVL Trees
Background image of page 4
B.B. Karki, LSU 5 CSC 3102 AVL Trees An AVL tree is a height-balanced binary search tree The balance factor ( B ), which is defined as the difference between the heights of the node’s left and right subtrees, of every node is either 0 or +1 or -1. | B | = | h L - h R | <= 1 AVL trees were invented in 1962 by G.M. A delson- V elsky and E.M. L andis. 5 20 4 7 10 2 12 8 B = 1 0 1 -1 0 0 0 1 5 20 4 7 10 2 8 2 0 0 -1 0 0 1 AVL tree Not AVL tree
Background image of page 5

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

View Full DocumentRight Arrow Icon
B.B. Karki, LSU 6 CSC 3102 Balancing AVL Trees Whenever we detect that a tree has become unbalanced, we must rebalance it. Transform tree to a balanced one by a rotation Rotation is a local transformation of the subtree rooted at an out-of-balance node. Four types of rotations (corresponding to the four cases of unbalanced trees) Singe right ( R ) rotation: Left of left Single left ( L ) rotation: Right of right Double left-right ( LR ) rotation: Right of left Double right-left ( RL ) rotation: Left of right 2 1 3 2 1 0 -2 -1 0 1 3 -1 2 0 2 3 -2 1 1 2 0 Left of left Right of right Right of left Left of right 2 3 1
Background image of page 6
B.B. Karki, LSU 7 CSC 3102 R- Rotation Rotates the out-of-balance node to the right Single R -rotation in its more general form. A new node is inserted
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

DA09 - Balanced Search Trees CSC 3102 1 B.B. Karki, LSU Why...

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