Balanced Binary Trees

Similar documents
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees CMPSC 122

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

Fundamental Algorithms

TREE BASIC TERMINOLOGIES

How To Create A Tree From A Tree In Runtime (For A Tree)

Converting a Number from Decimal to Binary

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

A Comparison of Dictionary Implementations

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

From Last Time: Remove (Delete) Operation

Analysis of Algorithms I: Binary Search Trees

Binary Search Trees (BST)

Full and Complete Binary Trees

Data Structures Fibonacci Heaps, Amortized Analysis

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Ordered Lists and Binary Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Persistent Binary Search Trees

Symbol Tables. Introduction

GRAPH THEORY LECTURE 4: TREES

Heaps & Priority Queues in the C++ STL 2-3 Trees

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Laboratory Module 6 Red-Black Trees

Data Structure [Question Bank]

Data Structures and Algorithms

Lecture Notes on Binary Search Trees

Lecture 4: Balanced Binary Search Trees

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Big Data and Scripting. Part 4: Memory Hierarchies

M(0) = 1 M(1) = 2 M(h) = M(h 1) + M(h 2) + 1 (h > 1)

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Trees and Huffman Encoding Binary Search Trees

schema binary search tree schema binary search trees data structures and algorithms lecture 7 AVL-trees material

Data Structure with C

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

IMPLEMENTING CLASSIFICATION FOR INDIAN STOCK MARKET USING CART ALGORITHM WITH B+ TREE

Binary Search Trees 3/20/14

Chapter 14 The Binary Search Tree

PES Institute of Technology-BSC QUESTION BANK

Algorithms Chapter 12 Binary Search Trees

M-way Trees and B-Trees

Binary Heaps. CSE 373 Data Structures

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Data Structures. Level 6 C Module Descriptor

The ADT Binary Search Tree

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Heap Algorithms

Algorithms and Data Structures

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

Lecture Notes on Binary Search Trees

An Immediate Approach to Balancing Nodes of Binary Search Trees

International Journal of Advanced Research in Computer Science and Software Engineering

1 Introduction to Internet Content Distribution

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Any two nodes which are connected by an edge in a graph are called adjacent node.

ECON 459 Game Theory. Lecture Notes Auctions. Luca Anderlini Spring 2015

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

An Evaluation of Self-adjusting Binary Search Tree Techniques

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

BALTIC OLYMPIAD IN INFORMATICS Stockholm, April 18-22, 2009 Page 1 of?? ENG rectangle. Rectangle

Exam study sheet for CS2711. List of topics

Rotation Operation for Binary Search Trees Idea:

The Goldberg Rao Algorithm for the Maximum Flow Problem

CSE 326: Data Structures B-Trees and B+ Trees

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

DATABASE DESIGN - 1DL400

COMPSCI 105 S2 C - Assignment 2 Due date: Friday, 23 rd October 7pm

Persistent Data Structures

Why Use Binary Trees?

3.1. Solving linear equations. Introduction. Prerequisites. Learning Outcomes. Learning Style

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Randomized Binary Search Trees

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

CMPSCI611: Approximating MAX-CUT Lecture 20

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Binary Search Trees. Ric Glassey

DYNAMIC DOMAIN CLASSIFICATION FOR FRACTAL IMAGE COMPRESSION

3. What is the difference between variance and standard deviation? 5. If I add 2 to all my observations, how variance and mean will vary?

Algorithms and Data Structures

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

DATA STRUCTURES USING C

10CS35: Data Structures Using C

The Basics of FEA Procedure

Cpt S 223. School of EECS, WSU

Introduction to Data Structures and Algorithms

Equations, Inequalities & Partial Fractions

Transcription:

Reminders about Trees Balanced Binary Trees With pictures by John Morris (ciips.ee.uwa.edu.au/~morris) A binary tree is a tree with exactly two sub-trees for each node, called the left and right sub-trees. A binary search tree is a binary tree where, for each node m, the left sub-tree only has nodes with keys smaller than (according to some total order) the key of m, while the right sub-tree only has nodes with keys larger than the key of m. The height of a tree is the number of nodes on its longest branch (a path from the root to a leaf). Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 1 of 11

Observations The search time in a binary search tree depends on the form of the tree, that is on the order in which its nodes were inserted. A pathological case: The n nodes were inserted by increasing order on the keys, yielding something like a linear list (but with a worse space consumption), with O(n) search time. Node search, insertion, deletion, all take time proportional to the height of the binary search tree. The height h of a binary tree of n nodes is such that log 2 n < h n. The height of a randomly built binary search tree of n nodes is O(log 2 n). Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 2 of 11

Observations (continued) In practice, one can however not always guarantee that binary search trees are built randomly. Binary search trees are thus only interesting if they are relatively complete. So we must look for specialisations of binary search trees whose worst-case performance on the basic tree operations can be guaranteed to be good, that is O(log 2 n) time. A balanced tree is a tree where every leaf is not more than a certain distance away from the root than any other leaf. The various balancing schemes give actual definitions for not more than a certain distance and require different efforts to keep the trees balanced: AVL trees Red-black trees. Inserting into, and deleting from, a balanced binary search tree involves transforming the tree if its balancing property which is to be kept invariant is violated. These re-balancing transformations should also take O(log 2 n) time, so that the effort is worth it. These transformations are built from operators that are independent from the balancing scheme. Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 3 of 11

Operations on Binary Trees A walk of a tree is a way of visiting each of its nodes exactly once. For binary trees, we distinguish preorder walk (visit the root, then the left sub-tree, and last the right sub-tree), inorder walk (left, root, right), and postorder walk (left, right, root). Remark: The inorder walk of a binary search trees gives its nodes in sorted (increasing) key order. A rotation of a binary tree transforms it so that its inorder-walk key ordering is preserved. We distinguish left rotation and right rotation: inorder walk = A x B y C preorder walk = y x A B C preorder walk = x A y B C postorder walk = A B x C y postorder walk = A B C y x Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 4 of 11

Definition AVL Trees Named after their inventors G.M. Adel son-velskii and E.M. Landis (USSR) AVL trees were the first dynamically balanced trees to be proposed, namely in 1962. They are not perfectly balanced, but maintain O(log 2 n) search, insertion, and deletion times, when n is the number of nodes in the tree. An AVL tree is a binary search tree where the sub-trees of every node differ in height by at most 1. and conversely, when the left and right sub-trees are exchanged, or when both are of height h 1 Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 5 of 11

Two Examples and One Counter-Example Let us annotate each node with a balance factor: when the tree rooted at this node is stable when the tree rooted at this node is left-heavy + when the tree rooted at this node is right-heavy when the tree rooted at this node is left-unbalanced ++ when the tree rooted at this node is right-unbalanced Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 6 of 11

What Performance Can We Expect from AVL Trees? Key question: What is the maximal height hmax(n) of an AVL tree with n nodes? Conversely: What is the minimal number of nodes nmin(h) of an AVL tree of height h? The latter question is easier to answer! nmin(0) = 0 nmin(1) = 1 nmin(h) = 1 + nmin(h 1) + nmin(h 2), when h >1 Compare this with the Fibonacci series: h 0 1 2 3 4 5 6 7 8 nmin(h) 0 1 2 4 7 12 20 33 54 fib(h) 0 1 1 2 3 5 8 13 21 Hence: nmin(h) = fib(h +2) 1. (Homework: Prove this!) Conversely, the maximal height hmax(n) of an AVL tree with n nodes is the largest h such that: fib(h +2) 1 n which simplifies into hmax(n) 1.44 log 2 (n +1) 1.33 so that search in an AVL tree indeed takes O(log 2 n) time! Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 7 of 11

Insertion into AVL Trees How to insert in O(log 2 n) time a node into an AVL tree such that it remains an AVL tree? After locating the insertion place and performing the insertion, there are three cases: (1) The tree remains balanced (,, or +): do nothing. (2) A tree was left-heavy ( ) and became left-unbalanced ( ): see below. (3) A tree was right-heavy (+) and became right-unbalanced (++): symmetric to the second case. Regarding case (2), there are two sub-cases: The left sub-tree became left-heavy: right-rotate the left sub-tree to the root. Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 8 of 11

Example: Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 9 of 11

The left sub-tree became right-heavy: first left-rotate the right sub-tree of the left sub-tree to its parent, and then right-rotate the left sub-tree to the root. Property: An insertion requires re-balancing It does not modify the height of the tree. Insertion requires at most two walks of the path from the root to the added node, hence indeed takes O(log 2 n) time. Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 10 of 11

Conclusion AVL trees are interesting when: the number n of nodes is large (say n 50), and the keys are suspected of not appearing randomly, and the ratio s / i of searches to insertions is large enough (say s / i 5) to justify the cost of re-balancing. Balanced Binary Trees Pierre Flener, IT Dept, Uppsala University Page 11 of 11