CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures

Slides:



Advertisements
Similar presentations
Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Advertisements

Rotating Nodes How to balance a node that has become unbalanced after the addition of one new node.
CS 261 – Data Structures AVL Trees. Binary Search Tree: Balance Complexity of BST operations: proportional to the length of the path from the root to.
AVL Tree Rotations Daniel Box. Binary Search Trees A binary search tree is a tree created so that all of the items in the left subtree of a node are less.
CS 261 – Recitation 9 & 10 Graphs & Final review
Chapter 10 Efficient Binary Search Trees
4.5 AVL Trees  A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ.
1 AVL Trees. 2 Consider a situation when data elements are inserted in a BST in sorted order: 1, 2, 3, … BST becomes a degenerate tree. Search operation.
CS2420: Lecture 24 Vladimir Kulyukin Computer Science Department Utah State University.
Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
CS 261 – Winter 2010 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting.
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation. Why AVL Trees? Rotations. Insertion into an AVL tree Deletion from an AVL tree.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
AVL Trees ITCS6114 Algorithms and Data Structures.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
1 AVL Trees II Implementation. 2 Download: 2011_03_28_AVL_Tree/ File AVL_Tree_Demo.zip
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
CS261 Data Structures Binary Search Trees II Bag Implementation.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
CS 261 – Fall 2009 Binary Search Trees. Can we do something useful? How can we make a collection using the idea of a binary tree? How about starting with.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
AVL Tree: Balanced Binary Search Tree 9.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
UNIT III TREES.
Binary and AVL Trees in C
CSIT 402 Data Structures II
Balanced Binary Search Trees
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 201 Data Structures and Algorithms
AVL Search Trees Introduction What is an AVL Tree?
Chapter 29 AVL Trees.
Additional Tree Traversal Example #1
CS 261 – Recitation 7 Fall 2013 Oregon State University
Dissection of AVL tree operations
AVL Tree Mohammad Asad Abbasi Lecture 12
Trees (Chapter 4) Binary Search Trees - Review Definition
Representation of a Threaded Tree
Binary Search Tree AVL Tree
AVL Trees Lab 11: AVL Trees.
CS223 Advanced Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
CS223 Advanced Data Structures and Algorithms
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
ITCS6114 Algorithms and Data Structures
Binary and AVL Trees in C Hao Ma
CSE 373 Data Structures Lecture 8
CS 261 – Data Structures Binary Search Trees.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
CS 261 – Data Structures AVL Trees.
Presentation transcript:

CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures Oregon State University School of Electrical Engineering and Computer Science CS 261 – Recitation 7 Fall 2010 CS 261 – Data Structures

Outline AVL Trees Tree Traversals CS 261 – Data Structures

Worst case for Binary Search Tree 1 2 3 4 CS 261 – Data Structures

AVL Trees height-balanced binary search trees Balance factor of a node height(right subtree) - height(left subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node CS 261 – Data Structures

Rotations Single Rotation Double Rotation CS 261 – Data Structures

Balancing Factor and Height height of node = h balance factor = hright-hleft empty height = -1 1 2 3 h=0 bf = 0 h=1 bf = 0 – 0 = 0 2 3 h = 1 bf = 0 – (-1)= 1 h =0 bf = 0 CS 261 – Data Structures

Single Rotation Now insert 16. First insert 14 and 15: 1(1) 2(2) 14 14 0(0) 14 15 16 2(2) 1(1) 0(0) Height(Balancing Factor ) CS 261 – Data Structures

Single Rotation – Left Rotation Before Rotation After Rotation 14 15 16 2(2) 1(1) 0(0) 14 15 16 0(0) 1(0) Left Rotation CS 261 – Data Structures

Single Rotation – Right Rotation Insert 13 12 Before Rotation After Rotation 13 1(-1) 14 15 16 2(-2) 3(-2) 0(0) 12 13 0(0) 15 16 1(0) 2(-1) 12 Right Rotation 14 0(0) CS 261 – Data Structures

Single Rotation - Element Shift Insert 11 Before Rotation After Rotation 14 0(0) 13 1(-1) 15 16 2(-1) 3(-2) 12 11 14 0(0) 13 1(-1) 15 16 2(0) 1(0) 12 11 Right Rotation 14 is now left of 15 CS 261 – Data Structures

Double Rotation Now insert 2. First insert 3 and 1: 1(-1) 2(-2) 0(0) 3 1 2(-2) 1(1 ) 2 bf is -ve bf is +ve 0(0) CS 261 – Data Structures

Double Rotation Before rotation After single rotation After double rotation 0(0) 3 2 2(-2) 1(-1) 1 3 1 2(-2) 1(1) 2 0(0) 2 1 1(0) 3 LR RR 0(0) CS 261 – Data Structures

Double Rotation Add 5 then 4 Before rotation After single rotation 2(2) 2 1 3(2) 0(0) 3 1(-1) 5 4 2(2) 2 1 3(2) 0(0) 3 1(1) 4 5 RR CS 261 – Data Structures

Double Rotation After first rotation After second rotation 2(2) 2 1 3(2) 0(0) 3 1(1) 4 5 1(0) 2 1 2(1) 0(0) 4 5 3 LR CS 261 – Data Structures

Double Rotation – Element Shift Before rotation After single rotation 1(0) 10 7 4(-2) 3(-2) 13 0(0) 15 2 4 5 1(-1) 3 2(1) 8 12 LR 1(0) 10 5 4(-2) 2(-2) 13 0(0) 15 2 8 4 3 7 3(-2) 12 Still not balanced CS 261 – Data Structures

Double Rotation – Element Shift After first rotation After double rotation 1(0) 10 5 4(-2) 2(-2) 13 0(0) 15 2 8 4 3 7 3(-2) 12 RR 1(0) 10 5 3(-1) 2(0) 13 0(0) 15 2 8 4 3 7 1(1) 12 CS 261 – Data Structures

Try Create AVL tree Example 1. 55, 66, 77, 44, 50, 70, 75, 60, 65 CS 261 – Data Structures

AVLnodeadd struct AVLnode * AVLnodeadd( struct AVLnode * current, TYPE newValue) { struct AVLnode * newNode; if ( current == 0) newNode = ( struct AVLnode *) malloc(sizeof( struct AVLnode)); assert(newNode != 0); newNode->value = newValue; newNode->left = newNode ->right = 0; return newNode; } CS 261 – Data Structures

AVLnodeadd(contd) else if ( LT(newValue, current->value) ) { current->left = AVLnodeadd(current->left, newValue); } else { current->right = AVLnodeadd(current->right, newValue); } return balance(current); CS 261 – Data Structures

balance struct AVLnode * balance(struct AVLnode * current) { //check bf of current int cbf = bf (current); //bf < 0 if ( cbf < -1 ) { if ( bf(current->left) > 0 ){ current->left = rotateLeft(current->left); } // single rotation return rotateRight(current); CS 261 – Data Structures

Balance(contd) else if ( cbf > 1) { if ( bf(current->right) > 0 ){ current->right = rotateRight(current->right); } // single rotation return rotateLeft(current); setHeight(current); return (current); CS 261 – Data Structures

rotateRight struct AVLnode * rotateRight (struct AVLnode * current) { struct AVLnode * newtop = current->left; current->left = newtop->right; newtop->right = current; setHeight(current); setHeight(newtop); return newtop; } CS 261 – Data Structures

rotateLeft struct AVLnode * rotateLeft (struct AVLnode * current) { struct AVLnode * newtop = current->right; current->right = newtop->left; newtop->left = current; setHeight(current); setHeight(newtop); return newtop; } CS 261 – Data Structures

removeAVLTrees void removeAVLTree(struct AVLTree *tree, TYPE val) { if (containsAVLTree(tree, val)) { tree->root = _removeNode(tree->root, val); tree->cnt--; } CS 261 – Data Structures

_removeNode struct AVLNode *_removeNode(struct AVLNode *cur, TYPE val) { struct AVLNode *temp; if(EQ(val, cur->val)) { if(cur->rght != 0) cur->val = _leftMost(cur->rght); cur->rght =_removeLeftmost(cur->rght); return _balance(cur); } else { temp = cur->left; free(cur); return temp; } CS 261 – Data Structures

_removeNode else if(LT(val, cur->val)) { cur->left = _removeNode(cur->left, val); else cur->rght = _removeNode(cur->rght, val); return _balance(cur); } CS 261 – Data Structures

_leftMost TYPE _leftMost(struct AVLNode *cur) { while(cur->left != 0) { cur = cur->left; } return cur->val; CS 261 – Data Structures

_removeLeftmost struct AVLNode *_removeLeftmost(struct AVLNode *cur) { struct AVLNode *temp; if(cur->left != 0) { cur->left = _removeLeftmost(cur->left); return _balance(cur); } temp = cur->rght; free(cur); return temp; CS 261 – Data Structures

Tree Traversals Pre-order b a c In-order a b c Post-order a c b b c a CS 261 – Data Structures

Tree Traversals What is the pre-order, in-order and post-order traversal ? Pre-order j h d a f I m l k n In-order a d f h I j k l m n Post-order a f d I h k l n m j j h m n a k f d i l CS 261 – Data Structures

Thanks !! CS 261 – Data Structures