Tree Properties (size vs height) Balanced Binary Trees

Similar documents
From Last Time: Remove (Delete) Operation

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

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

TREE BASIC TERMINOLOGIES

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

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:

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

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

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

Converting a Number from Decimal to Binary

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

Fundamental Algorithms

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

Binary Heaps. CSE 373 Data Structures

Ordered Lists and Binary Trees

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

Analysis of Algorithms I: Binary Search Trees

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

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

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

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

Algorithms and Data Structures

Why Use Binary Trees?

Full and Complete Binary Trees

Lecture Notes on Binary Search Trees

Chapter 14 The Binary Search Tree

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

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Binary Search Trees (BST)

Binary Trees and Huffman Encoding Binary Search Trees

An Evaluation of Self-adjusting Binary Search Tree Techniques

Data Structures and Algorithms

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

Binary Heap Algorithms

Cpt S 223. School of EECS, WSU

PES Institute of Technology-BSC QUESTION BANK

Algorithms and Data Structures Written Exam Proposed SOLUTION

The ADT Binary Search Tree

Data Structures Fibonacci Heaps, Amortized Analysis

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

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

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

Binary Search Trees CMPSC 122

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Optimal Binary Search Trees Meet Object Oriented Programming

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Binary Search Trees. Ric Glassey

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)

Lecture Notes on Binary Search Trees

Algorithms Chapter 12 Binary Search Trees

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

Algorithms and Data Structures

An Immediate Approach to Balancing Nodes of Binary Search Trees

Symbol Tables. Introduction

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Data Structure [Question Bank]

Persistent Binary Search Trees

Analysis of Algorithms I: Optimal Binary Search Trees

Binary search algorithm

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

Binary Search Trees 3/20/14

CS711008Z Algorithm Design and Analysis

Big Data and Scripting. Part 4: Memory Hierarchies

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

GRAPH THEORY LECTURE 4: TREES

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

Exam study sheet for CS2711. List of topics

Motivation Suppose we have a database of people We want to gure out who is related to whom Initially, we only have a list of people, and information a

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

- 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

A Comparison of Dictionary Implementations

International Journal of Software and Web Sciences (IJSWS)

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

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Mathematical Induction. Lecture 10-11

10CS35: Data Structures Using C

Lecture 1: Course overview, circuits, and formulas

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Persistent Data Structures and Planar Point Location

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Lecture 4: Balanced Binary Search Trees

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Persistent Data Structures

Enhancing the Search in MOLAP Sparse Data

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

Data Structures and Algorithms Written Examination

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Sample Questions Csci 1112 A. Bellaachia

6 March Array Implementation of Binary Trees

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

Efficient representation of integer sets

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Laboratory Module 6 Red-Black Trees

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Lecture 2 February 12, 2003

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

recursion, O(n), linked lists 6/14

Transcription:

Tree Properties (size vs height) Balanced Binary Trees

Due now: WA 7 (This is the end of the grace period). Note that a grace period takes the place of a late day. Now is the last time you can turn this in for credit. Due at the beginning of Day 17: OO Queens exercise from Day 15 class Due at the beginning of Day 18: WA8 Sliding Blocks Milestone 1 I eliminated the NameThatSort proramming problem, to leave you more time to work on SlidingBlocks and Scrabble.

Touch Base with your SlidingBlocks partner. Height vs size of binary trees The need for balanced trees Keeping trees completely balanced Height-Balanced (H-B) trees

WA8 Exhaustive search & Backtracking Sliding Blocks Problem Anything else

public static BinaryNode buildfrompreorderinorder(string pre, String in) { if (pre.length() == in.length()) return prein(pre, in); throw new IllegalArgumentException(); } private static BinaryNode prein(string pre, String in) { if (pre.equals("")) return null; char rootchar = pre.charat(0); int rootpos = in.indexof(rootchar); return new BinaryNode(rootChar, prein(pre.substring(1, rootpos+1), in.substring(0, rootpos)), prein(pre.substring(rootpos+1), in.substring(rootpos+1))); }

Size vs Height

A Binary Tree is either empty, or consists of: a distinguished node called the root, which contains an element and two disjoint subtrees A left subtree T L, which is a binary tree root A right subtree T R, which is a binary tree T L T R

Figure 18.20 Recursive view of the node height calculation: H T = Max (H L + 1, H R + 1) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley

If T is a tree, we'll often write h(t) for the height of the tree, and N(T) for the number of nodes in the tree For a particular h(t), what are the upper and lower bounds on N(T)? Lower: N(T) Upper N(T) (prove it by induction) (prove it by induction) Thus N(T). Write bounds for h(t) in terms of N(T) Thus h(t).

A tree with the maximum number of nodes for its height is a full tree. Its height is O(log N) A tree with the minimum number of nodes for its height is essentially a. Its height is O(N) Height matters! We saw that the algorithms for search, insertion, and deletion in a Binary Search Tree are O(h(T))

Review: Efficiency of insertion, deletion, find for Array list Linked list BST insertion algorithm O(height of tree) BST deletion algorithm O(height of tree) BST search algorithm - O(height of tree) Efficiency (worst case)? Can we get a better bound? What about balancing the tree each time? What do we mean by completely balanced? Insert E C G B D F A into a tree in that order. What is the problem? How might we do better? (less is more!)

We'd like the worst-case time for find, insert, and delete to be O(log N). The running time for find, insert, and delete are all proportional to the height of the tree. Height of the tree can vary from log N to N. Keeping the tree completely balanced is too expensive. Can require O(N) time to rebalance after insertion or deletion. Height-balanced trees may provide a solution. A BST T is height balanced if T is empty, or if height( T L ) - height( T R ) 1, and T L and T R are both height-balanced. What can we say about the maximum height of an height-balanced tree with N nodes? Details: next slide heightbalanced? Why do we use absolute value in the formula?

We want to calculate the maximum height of a height-balanced tree with N nodes. It s not the shortest possible tree, but how close is it? We first look at the dual concept: find the minimum number of nodes in a HB tree of height h. Make a table of heights and # of nodes. What can we say in general about height as a function of number of nodes?

Named for authors of original paper, Adelson-Velskii and Landis (1962). It is a height-balanced Binary Search Tree. Recall: A BST T is height balanced if T is empty, or if height( T L ) - height( T R ) 1, and T L and T R are both height-balanced. Maximum height of an AVL tree with N nodes is O(log N).

For a Binary Search Tree (BST), worst-case for insert, delete, and find operations are all O(height of tree). Height of tree can vary from O(log N) to O(N). We showed that the height of a height-balanced tree is always O(log N). Thus all three operations will be O(log N) if we can rebalance after insert or delete in time O(log N)

An AVL tree is 1. height-balanced 2. a Binary search tree We saw that the maximum height of an AVL tree with N nodes is O(log n). We want to show that after an insertion or deletion (also O(log n) since the height is O(log n)), we can rebalance the tree in O(log n) time. If that is true, then find, insert, and remove, will all be O(Log N). An extra field is needed in each node in order to achieve this speed. Values: / = \ We call this field the balance code. The balance code could be represented by only two bits.

Assume that the tree is height-balanced before the insertion. Start at the inserted node (always a leaf). Move back up the tree to the first (lowest) node (if any) where the heights of its subtrees now differ by more than one. We ll call that node A in our diagrams. Do the appropriate single or double rotation to balance the subtree whose root is at this node. If a rotation is needed, we will see that the combination of the insertion and rotation leaves this subtree with the same height that it had before insertion. So why is the algorithm O(log N)?

Depends on the first two links in the path from the node with the imbalance (A) down to the newly-inserted node. First link (down from A) Second link (down from A's child) Rotation type (rotate "around A's position") Left Left Single right Left Right Double right Right Right Single left Right Left Double left

Diagrams are from Data Structures by E.M. Reingold and W.J. Hansen.