Nearly Complete Binary Trees and Heaps

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

Full and Complete Binary Trees

Converting a Number from Decimal to Binary

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

Binary Search Trees CMPSC 122

Algorithms and Data Structures

Analysis of Algorithms I: Binary Search Trees

Binary Heap Algorithms

Binary Heaps. CSE 373 Data Structures

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

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

Algorithms and Data Structures

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

Ordered Lists and Binary Trees

Symbol Tables. Introduction

Analysis of Algorithms I: Optimal Binary Search Trees

From Last Time: Remove (Delete) Operation

Data Structures Fibonacci Heaps, Amortized Analysis

Algorithms Chapter 12 Binary Search Trees

TREE BASIC TERMINOLOGIES

Cpt S 223. School of EECS, WSU

GRAPH THEORY LECTURE 4: TREES

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

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

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

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

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

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

Data Structures and Algorithms

6 March Array Implementation of Binary Trees

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

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

Data Structure with C

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

Binary Search Trees (BST)

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

Chapter 14 The Binary Search Tree

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

CS711008Z Algorithm Design and Analysis

Lecture Notes on Binary Search Trees

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:

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

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

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

Data Structure [Question Bank]

Big Data and Scripting. Part 4: Memory Hierarchies

Rotation Operation for Binary Search Trees Idea:

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

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

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

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

Fundamental Algorithms

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 221] edge. parent

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

Binary Trees and Huffman Encoding Binary Search Trees

Binary Search Trees. Ric Glassey

DATABASE DESIGN - 1DL400

Lecture 1: Course overview, circuits, and formulas

International Journal of Advanced Research in Computer Science and Software Engineering

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

6.3 Conditional Probability and Independence

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

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.

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

Data Structures and Algorithms Written Examination

Binary Search Trees 3/20/14

Sample Questions Csci 1112 A. Bellaachia

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

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

Lecture Notes on Binary Search Trees

Exam study sheet for CS2711. List of topics

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

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)

Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4)

DATA STRUCTURES USING C

Introduction to Data Structures and Algorithms

Section IV.1: Recursive Algorithms and Recursion Trees

Why Use Binary Trees?

Data Structures and Algorithms(5)

Classification/Decision Trees (II)

CS473 - Algorithms I

Persistent Data Structures and Planar Point Location

Lecture 4: Balanced Binary Search Trees

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)

Physical Data Organization

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

The ADT Binary Search Tree

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

Lecture Notes on Linear Search

Data Structures and Data Manipulation

Strategic Deployment in Graphs. 1 Introduction. v 1 = v s. v 2. v 4. e 1. e e 3. e 2. e 4

Union-Find Problem. Using Arrays And Chains

An Evaluation of Self-adjusting Binary Search Tree Techniques

Persistent Binary Search Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

A Randomized Self-Adjusting Binary Search Tree

CS473 - Algorithms I

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

An Immediate Approach to Balancing Nodes of Binary Search Trees

Sequential Data Structures

Transcription:

Nearly Complete Binary Trees and Heaps DEFINITIONS: i) The depth of a node p in a binary tree is the length (number of edges) of the path from the root to p. ii) The height (or depth) of a binary tree is the maximum depth of any node, or 1 if the tree is empty. Any binary tree can have at most 2 d nodes at depth d. (Easy proof by induction) DEFINITION: A complete binary tree of height h is a binary tree which contains exactly 2 d nodes at depth d, 0 d h. In this tree, every node at depth less than h has two children. The nodes at depth h are the leaves. The relationship between n (the number of nodes) and h (the height) is given by and n = 1 + 2 + 2 2 +... + 2 h 1 + 2 h = 2 h+1 1 h = lg(n+1) 1.

Complete binary trees are perfectly balanced and have the maximum possible number of nodes, given their height However, they exist only when n is one less than a power of 2. DEFINITION: A nearly complete binary tree of height h is a binary tree of height h in which a) There are 2 d nodes at depth d for d = 1,2,...,h 1, b) The nodes at depth h are as far left as possible. Condition (b) can be stated more rigorously, like this: If a node p at depth h 1 has a left child, then every node at depth h 1 to the left of p has 2 children. If a node at depth h 1 has a right child, then it also has a left child. The relationship between the height and number of nodes in a nearly complete binary tree is given by 2 h n 2 h+1 1, or h = lg(n). (This depends only on condition (a) in the definition.)

Examples: Not nearly complete: (a) fails. Not nearly complete: (b) fails. Nearly complete.

Say we label the nodes of a nearly complete binary tree by 1, 2, 3,..., n in order of increasing depth, and left-to-right at a given depth. Then, equating each node with its label, i) left(k) = 2k, if 2k n, ii) right(k) = 2k+1, if 2k+1 n, iii) parent(k) = k/2 if k > 1. iv) k has one or more children if 2k n. It has two children if any only if 2k+1 n. v) k is the left child of its parent if and only if k is even. Suppose each node in the tree contains an element from some set. Denote the element in node p as element(p).

1 J 2 3 Q D 4 5 6 7 B T Z G 8 9 10 11 12 13 14 15 L C U A N F R H 16 17 18 19 20 21 22 V I E W K M Y We don t really need the tree structure (nodes with pointers to the two children, and possibly the parent). We can represent the tree implicitly by an array. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 a J Q D B T Z G L C U A N F R H V I E W K M Y The array contains all the information in the tree. In the tree, if p is the node containing T (node 5), then parent(p) contains Q, left(p) contains U, and right(p) contains A. (We examine the link fields in the node.) In the array representation, we compute 5/2 = 2, 2 5 = 10, and 2 5+1 == 11, and we find parent(a[5]] = a[2] = Q, left(a[5]] = a[10] = U, and right(a[5]) = a[11] = A.

It is useful to think in terms of the tree, but all computation is actually performed with the array. DEFINITION: A max-heap (or simply a heap) is a nearly complete binary tree in which each node contains an element from a set S with a strict weak ordering, such that: For each node p except the root, element( parent(p)) > ~ element(p). Heap condition at node p A min-heap is defined similarly except the heap condition is element( parent(p)) < ~ element(p). Example of max-heap: 81 75 52 59 71 39 48 50 22 42 69 35 2 12 29 8 45 19 16 5 27 56 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 81 75 52 59 71 39 48 50 22 42 69 35 2 12 29 8 45 19 16 5 27 56

Note in a max-heap: i) The largest element is in the root. ii) The second largest element is in one of the children of the root, but the third largest element need not be in the other child. With a heap, we can perform at least these operations efficiently (time at worst Θ(lg(n)). 1) Insert a new element. 2) Find the largest element. 3) Remove the largest element. 1) Insert a new element (say insert 73, in the heap above) There is only one place where we can insert a new node, and still have a nearly complete binary tree. 81 75 52 59 71 39 48 50 22 42 69 35 q 2 12 29 8 45 19 16 5 27 56 73 p

In general, if the old size of the heap is n, the new node becomes of child of node (n 1)/2 a right child is n is even, and a left child if it is odd. The only place the heap property can possibly fail is at the new node (node p). We compare the element in node p (73) with the element in node parent(p) = q (69), and find that the heap property does fail at node p. We correct the problem at p by exchanging the elements in nodes p (73) and q (69). 81 75 52 59 71 r 39 48 50 22 42 73 35 q 2 12 29 8 45 19 16 5 27 56 69 p Now the only place the heap property can possibly fail is at node q.

We compare the element in node q (73) with the element in node parent(q) = r (71), and find that the heap property does fail at node q. We correct the problem by exchanging the elements in nodes q and r. 81 t 75 s 52 59 73 r 39 48 50 22 42 71 35 q 2 12 29 8 45 19 16 5 27 56 69 p Now the only place the heap property can possibly fail is at the parent of r (node s). We compare the element in node r (73) with the element in node parent(r) = s (75), and find that the heap property actually holds at node r. We are done. In the worst case, we would have compared the new element with the elements in nodes q, r, s, and t.

In general, the worst-case number of comparisons to insert a new element is the depth of the new node. This is the height of a heap with n+1 elements, or lg(n+1). Thus: C max (n) = lg(n+1) lg(n), T max (n) = Θ(lg(n)). With the array representation, the algorithm to insert a new element is: // Insert a new element x into a heap of size n // represented in an array A of size at least n+1. max-heap-insert( A, n, x) n = n + 1; A[n] = x; while ( n > 1 and A[n] > A[ n/2 ] ) swap( A[n], A[ n/2 ] ); n = n/2 ; 2) Find the largest element The largest element is in the root. Simply return the element in the root (constant time)

3) Remove the largest element Let us remove the largest element from the heap 77 q 72 55 67 31 45 51 28 41 29 25 35 2 7 47 24 8 19 22 5 27 32 p The element to be removed (77) is in the root. Removing it leaves the root empty. The only node we can delete from the tree, and still have a nearly complete tree, is the last node (node p). So we move the element in node p (32) to the root (node q), and remove node p from the tree. We still have a nearly complete binary tree, and the heap property can fail only at the children of the root (nodes r and s).

r 72 32 q heap property could fail here s 55 67 31 45 51 28 41 29 25 35 2 7 47 24 8 19 22 5 27 Given a nearly complete binary tree, in which the heap property can fail only at the children of the root, we can make the tree into a heap using a procedure called max-heapify(). Among the root and its two children (nodes q, r, s), we find the largest element. (Two comparisons will suffice.) In this case, the largest (72) occurs in node r. If the largest of these three elements were to occur in the root (not the case here), we would be done. If the largest occurs in a child of the root (as happens here, node r), we exchange the element in the root with the element in this child.

In our case, we exchange 32 and 72. r 32 72 q heap property must hold here s 55 67 but could 31 fail here 28 41 29 25 35 45 51 2 7 47 24 8 19 22 5 27 This guarantees that the heap property holds at both children of the root, but may cause it to fail at the children of the node exchanged with the root (the children of node r, in our case). We apply the same process recursively to the subtree rooted at r, i.e., invoke max-heapify() recursively. The recursion terminates when we reach a leaf node, if not sooner. The maximum number of calls to max-heapify() is (height of heap) = lg(n) and the maximum number of comparisons is 2 lg(n).

With the array representation, we can write max-heapify() like this. // A is an array of size at least n, which we think of // as a nearly complete binary tree. In the subtree // of A[1..n] rooted at A[i], the heap property // holds everywhere except possibly at the children // of A[i]. This function makes the subtree of // A[1..n] rooted at A[i] into a heap. max-heapify( A, i, n) largest = i; if ( 2i n and A[2i] > A[i] ) largest = 2i; if ( 2i+1 n and A[2i+1] > A[largest] ) largest = 2i+1; if ( largest i ) swap( A[i], A[largest]) max-heapify( A, largest, n); We can also write max-heapify() non-recursively like this:

max-heapify( A, i, n) while ( 2i n ) largest = i; if (A[2i] > A[i] ) largest = 2i; if ( 2i+1 n and A[2i+1] > A[largest] ) largest = 2i+1; if ( largest i ) swap( A[i], A[largest]) i = largest; else i = n+1; Note: In addition to operations (1), (2), (3), we can perform several other operations efficiently (Θ(lg(n)) time). Increase or decrease the element in a known position. Remove the element in a known position.

However, we can not efficiently Given x, decide if the heap contains an element equal to x. Given k, find the k th largest element in the heap (unless k is 1, or at least is very close to 1). Given x, remove x from the heap, if it is present.