CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.

Slides:



Advertisements
Similar presentations
AVL Trees When bad trees happen to good programmers.
Advertisements

Lecture 9 : Balanced Search Trees Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
CS202 - Fundamental Structures of Computer Science II
CS Data Structures Chapter 10 Search Structures (Selected Topics)
CS2420: Lecture 24 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 19 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
C++ Programming:. Program Design Including
CS 206 Introduction to Computer Science II 11 / 19 / 2008 Instructor: Michael Eckmann.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
CS2420: Lecture 30 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 22 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
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.
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
CS Data Structures Chapter 10 Search Structures.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
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.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Data Structures AVL Trees.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee.
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
CE 221 Data Structures and Algorithms Chapter 4: Trees (AVL Trees) Text: Read Weiss, §4.4 1Izmir University of Economics.
AVL Tree: Balanced Binary Search Tree 9.
CSE332: Data Abstractions Lecture 7: AVL Trees
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
Binary search tree. Removing a node
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
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.
CS 201 Data Structures and Algorithms
Tree.
Discrete Structures for Computer Science
CS202 - Fundamental Structures of Computer Science II
Binary Search Tree AVL Tree
CS202 - Fundamental Structures of Computer Science II
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
CS223 Advanced Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
Lecture No.20 Data Structures Dr. Sohail Aslam
CS223 Advanced Data Structures and Algorithms
Binary Search Trees Chapter 7 Objectives
Dr.Surasak Mungsing CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ.
CS202 - Fundamental Structures of Computer Science II
BST Insert To insert an element, we essentially do a find( ). When we reach a NULL pointer, we create a new node there. void BST::insert(const Comp &
การวิเคราะห์และออกแบบขั้นตอนวิธี
CSE 373 Data Structures Lecture 8
1 Lecture 13 CS2013.
CS203 Lecture 14.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University

Outline Balanced Binary Trees (AVL Trees) –Section 4.4.

Review: AVL Tree An empty binary tree is an AVL tree. If T is a non-empty binary tree, then T is an AVL tree if and only if: 1) Both sub-trees are AVL trees. 2) The heights of the sub-trees differ by at most 1 (AVL Tree Property). The height of an empty tree is defined to be -1, whereas the height of a tree with exactly one node is 0.

Review: Balance Factors The balance factor (BF) of a node is the difference b/w the height of the node’s left sub-tree (HL) and the height of the node’s right sub-tree (HR). BF = HL – HR. By the AVL Tree Property, the balance factor of each node in an AVL tree must be -1, 0, or 1.

Review : Balance Factors HL = 0HR = 0 HL = height of the left sub-tree. HR = height of the right sub-tree. BF = balance factor = HL – HR. BF = HL – HR = 0 – 0 = 0.

Review: Balance Factors (BFs) 12 8 HL = 0HR = -1 HL = height of the left sub-tree. HR = height of the right sub-tree. BF = balance factor = HL – HR. BF = HL – HR = 0 – (-1) = 1.

Review: AVL Tree with BFs BF = 0 BF = 1

Example: AVL Tree with BF’s BF = 0 BF = -1 BF = 1 BF = 0

Definition: AVL Search Tree An AVL Search Tree is a collection that satisfies the following two properties: 1) Binary Search Tree Property: An AVL Search Tree is a binary search tree. 2) AVL Tree Property: For every node in the tree, the height of the node’s left sub-tree and the height of the node’s right sub-tree differ by at most 1.

AVL Search Tree Node AVL Search Tree node has the same elements (member variables in C++) as the BST node plus the balance factor (integer) element. If space is a concern, the BF can be implemented as three Boolean (1-bit) variables whose values, taken collectively, will encode -1, 0, and 1. For example, 000 for 0, 001 for 1, and 010 for -1. Thus, the space overhead per AVL node is 3 bits.

AVL Tree Height Let N h be the minimum number of nodes in an AVL tree of height h. Why are we concerned with the minimum and not the maximum? Because we already know that the answer for the maximum – it is the number of nodes in a complete binary search tree. When h = 0, N h = 1. When h = 1, N h = 2. N h = N h-1 + N h Why? Because the minimum number of nodes in an AVL tree is the minimum number of nodes in the left sub-tree plus the minimum number of nodes in the right sub-tree plus 1 for the root.

Recall Fibonacci (Pingala) Numbers n FnFn

AVL Tree Height Recall the formula for the Fibonacci numbers: –F n = F n-1 + F n-2, F 0 = 0, F 1 = 1. Claim: N h = F h+3 – 1. –Proof: By induction on h. –Remember that N h is the minimum number of nodes in an AVL Search Tree.

Examples: N h = F h h = 0 N 0 = 1 = F 0+3 – 1 = 2 – 1 h = 1 N 1 = 2 = F 1+3 – 1 = 3 – 1

Examples: N h = F h h = 2 N 2 = 4 = F 2+3 – 1

Fibonacci Number Approximation

AVL Tree’s Height

AVL Search Tree: Find Since AVL search trees are binary search trees, finding an element in an AVL search tree is the same as finding an element in a binary search tree. The complexity is O(logN).