1 AVL Trees II Implementation. 2 Download: 2011_03_28_AVL_Tree/ File AVL_Tree_Demo.zip

Slides:



Advertisements
Similar presentations
CSE 373 Data Structures and Algorithms
Advertisements

AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
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 Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
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.
AVL Search Trees Inserting in an AVL tree Insertion implementation Deleting from an AVL tree.
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.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Binary Search Trees Chapter 6.
Building Java Programs Binary Search Trees reading: 17.3 – 17.4.
1 Binary Search Trees III Delete Chapter 6. 2 Objectives You will be able to write code to delete a node from a Binary Search Tree.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
Building Java Programs Binary Search Trees; TreeSet.
BSTImp: Insert, Delete. Inserting an Element into a BST Search for the position in the tree where the element would be found Insert the element in the.
Balanced Trees AVL Trees Red-Black Trees 2-3 Trees Trees.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Computer Science 112 Fundamentals of Programming II Binary Search Trees.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
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,
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
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.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
Data Structures for Java William H. Ford William R. Topp
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Balancing Binary Search Trees, Rotations
Recursive Objects (Part 4)
Fundamentals of Programming II Binary Search Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
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.
AVL Search Trees Introduction What is an AVL Tree?
Dissection of AVL tree operations
AVL Tree Mohammad Asad Abbasi Lecture 12
Cinda Heeren / Geoffrey Tien
Trees (Chapter 4) Binary Search Trees - Review Definition
Binary Search Trees.
Building Java Programs
Building Java Programs
AVL Trees CENG 213 Data Structures.
Binary Search Tree AVL Tree
CS202 - Fundamental Structures of Computer Science II
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Building Java Programs
Building Java Programs
CS202 - Fundamental Structures of Computer Science II
Lecture 21: Binary Search Trees; TreeSet
CSE 373 Data Structures and Algorithms
AVL Search Trees Inserting in an AVL tree Insertion implementation
AVL Search Trees Inserting in an AVL tree Insertion implementation
Building Java Programs
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Lecture 21: Binary Search Trees; TreeSet
Podcast Ch27b Title: AVLTree implementation
Lecture 10: BST and AVL Trees
AVL-Trees (Part 2).
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Search Trees What is an AVL Tree? AVL Tree Implementation.
AVL Search Trees Inserting in an AVL tree Insertion implementation
Presentation transcript:

1 AVL Trees II Implementation

2 Download: _03_28_AVL_Tree/ File AVL_Tree_Demo.zip _03_28_AVL_Tree/ Project contains: AVL_BST.h Our most recent BST template updated to implement an AVL tree. main.cpp Uses the AVL BST to replicate the states example from last class. Extract, build, and run

3 Explore the Code Enums used to specify direction of descent and kind of rotation needed In AVL_BST.h enum direction {dir_left, dir_right, dir_none}; enum rotation_type { rotate_type_left, rotate_type_right, rotate_type_left_right, rotate_type_right_left };

4 New Node Class template class AvlNode { public: AvlNode() { level = position = left = right = 0; } AvlNode(const T& el, AvlNode *l = 0, AvlNode *r = 0) { data = el; left = l; right = r; level = position = 0; } T data; int balance_factor; int position; int level; AvlNode *left, *right; };

5 Class AVL_BST The insert method has been changed (radically) in order to implement AVL tree rebalancing. Now a recursive method. Public method declaration unchanged: void insert(const T&); New internal method: void insert(const T& item, // Input AvlNode ** incoming_ptr, // Input bool& subtree_height_increased, // Output direction& dir); // Output

6 Public Insert Method // Public insert method template void AVL_BST ::insert(const T& item) { // Return values from internal insert method. Not used here. bool childs_subtree_height_increased; direction childs_direction; if (root == 0) { // Inserting into an empty tree. root = new AvlNode (item); } else { insert(item, &root, childs_subtree_height_increased, childs_direction); }

7 Internal insert method // Internal (protected) insert method template void AVL_BST ::insert(const T& item, // Input AvlNode ** incoming_ptr, // Input bool& subtree_height_increased, // Output direction& dir) // Output { AvlNode * node = *incoming_ptr; // Current node if (item data) { dir = dir_left; if (node->left == 0) { insert_left(item, incoming_ptr, subtree_height_increased, dir); } else { descend_left(item, incoming_ptr, subtree_height_increased, dir); }...

8 Internal insert method (continued) else if (node->data < item) { dir = dir_right; if (node->right == 0) { insert_right(item, incoming_ptr, subtree_height_increased, dir); } else { descend_right(item, incoming_ptr, subtree_height_increased, dir); } else // Item found. (Shouldn't happen) { throw "Attempt to add an item already in the tree"; } Symmetrical to > case

9 insert_right template void AVL_BST ::insert_right(const T & item, // Input AvlNode ** incoming_ptr, // Input bool& subtree_height_increased, // Output direction& dir) // Output { AvlNode * node = *incoming_ptr; // Current node cout data << " as right child" << endl; assert(node->right == 0); node->right = new AvlNode (item);... Insert new node into tree.

10 insert_right (continued) dir = dir_right; if (node->left == 0) { // This node was previously a leaf. subtree_height_increased = true; node->balance_factor = -1; } else { // Already had a left child, so no increase. subtree_height_increased = false; node->balance_factor = 0; } Report back to caller.

11 descend_right template void AVL_BST ::descend_right(const T& item, // Input AvlNode ** incoming_ptr, // Input bool& subtree_height_increased, // Output direction& dir) // Output { bool childs_subtree_height_increased; // Set by call to insert direction childs_direction; // Set by call to insert AvlNode *node = *incoming_ptr; // Current node assert(node->right != 0); insert(item, &(node->right), childs_subtree_height_increased, childs_direction); dir = dir_right; if (!childs_subtree_height_increased) { subtree_height_increased = false; return; } Recursive call Report back to caller

12 descend_right (continued) // Child's subtree height increased due to inserting the new node. --node->balance_factor; if (node->balance_factor < -1) { cout data << " requires rebalance" << endl; display_v(cout, 4); if (childs_direction == dir_right) { rotate_left(incoming_ptr); } else { rotate_right_left(incoming_ptr); } cout << endl << "After rotation: " << endl << endl; display_v(cout, 4); // After rotation, subtree height has not increased subtree_height_increased = false; return; } Report back to caller

13 descend_right (continued) // No rotation at this level. if (node->balance_factor == -1) { subtree_height_increased = true; } else { subtree_height_increased = false; } Report back to caller

14 rotate_right template void AVL_BST ::rotate_right(AvlNode ** parent_link) { AvlNode * node = *parent_link; cout data << " rotate_right " << endl; assert(node->left != 0); AvlNode * temp = node->left->right; AvlNode * pivot = node->left; pivot->right = *parent_link; *parent_link = pivot; node->left = temp; update_balance_factor(pivot); update_balance_factor(node); } "pivot" is the left child of the node being rotated to the right. It will take the place of the node being rotated, and the node being rotated will become its right child. If it previously had a right child, that node will become the new left child of the node being rotated.

15 update_balance_factor template void AVL_BST ::update_balance_factor(AvlNode * node) { if (node->right == 0) { if (node->left == 0) { node->balance_factor = 0; } else { node->balance_factor = 1; } else... Update the balance factor of a node that has just been rotated. There are only three possible values

16 update_balance_factor else { if (node->left == 0) { node->balance_factor = -1; } else { int l = height(node->left); int r = height(node->right); assert (abs(l-r) < 2); node->balance_factor = l - r; } node has a right child. node has a right child and a left child.

17 Compute height of a subtree template int AVL_BST ::height(AvlNode * node) { int left_child_height = node->left ? height(node->left) : 0; int right_child_height = node->right ? height(node->right) : 0; return max(left_child_height, right_child_height) + 1; }

18 Assignment Study the AVL_BST code carefully. Try it with your own test cases. Try to find errors. Major bragging rights to a student who reports a significant bug. Be prepared to answer questions about it.