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



Similar documents
Full and Complete Binary Trees

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

Binary Heap Algorithms

6 March Array Implementation of Binary Trees

Binary Trees and Huffman Encoding Binary Search Trees

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. 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

Algorithms and 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

From Last Time: Remove (Delete) Operation

Chapter 14 The Binary Search Tree

Binary Heaps. CSE 373 Data Structures

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

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

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

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

Analysis of a Search Algorithm

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Data Structures Fibonacci Heaps, Amortized Analysis

Cpt S 223. School of EECS, WSU

Binary Search Trees (BST)

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

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

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

Algorithms and Data Structures

Why Use Binary Trees?

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

Binary Search Trees 3/20/14

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)

Mathematical Induction. Lecture 10-11

Analysis of Algorithms I: Binary Search Trees

7.1 Our Current Model

GRAPH THEORY LECTURE 4: TREES

The ADT Binary Search Tree

Recursive Implementation of Recursive Data Structures

TREE BASIC TERMINOLOGIES

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

Converting a Number from Decimal to Binary

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

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

Ordered Lists and Binary Trees

Artificial Intelligence

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

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

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Data Structures and Algorithms

Union-Find Problem. Using Arrays And Chains

Analysis of Algorithms, I

Optimal Binary Search Trees Meet Object Oriented Programming

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

ADTs,, Arrays, Linked Lists

Data Structure with C

Load Balancing. Load Balancing 1 / 24

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

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

PES Institute of Technology-BSC QUESTION BANK

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Symbol Tables. Introduction

Binary Multiplication

DATABASE DESIGN - 1DL400

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

CS711008Z Algorithm Design and Analysis

Analysis of Algorithms I: Optimal Binary Search Trees

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

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

THIS CHAPTER studies several important methods for sorting lists, both contiguous

Lecture 7: NP-Complete Problems

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

Why? A central concept in Computer Science. Algorithms are ubiquitous.

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

Sample Questions with Explanations for LSAT India

Find-The-Number. 1 Find-The-Number With Comps

Fundamental Algorithms

Chapter 13: Query Processing. Basic Steps in Query Processing

Approximation Algorithms

Big Data and Scripting. Part 4: Memory Hierarchies

Lecture Notes on Binary Search Trees

CS473 - Algorithms I

Section #6: Addressing

Binary search tree with SIMD bandwidth optimization using SSE

Classification On The Clouds Using MapReduce

Parallelization: Binary Tree Traversal

Arithmetic Coding: Introduction

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:

Algorithms Chapter 12 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)

Binary Search Trees CMPSC 122

Section IV.1: Recursive Algorithms and Recursion Trees

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

Lecture Notes on Binary Search Trees

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

OPTIMAL BINARY SEARCH TREES

Chapter 7 Data Modeling Using the Entity- Relationship (ER) Model

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

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Homework until Test #2

Chapter 13. Disk Storage, Basic File Structures, and Hashing

Transcription:

1 Binary Trees (1) Outline and Required Reading: Binary Trees ( 6.3) Data Structures for Representing Trees ( 6.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Binary Tree 2 Binary Tree tree with the following properties Proper and Ordered! each internal node has two children children of internal nodes form ordered pairs: left node 1 st, right node 2 nd A 1 st child of A B C 2 nd child of A D E F G I J Application representation of arithmetic expression, decision process,

Binary Tree (cont.) 3 Example 1 [ binary tree for decision process ] Are you a mammal? Yes No Are you bigger than cat? Do you live under water? Yes No Yes No Kangaroo Mouse Trout Can you fly? Yes No Robin Snake

Properties of Binary Trees 4 Binary Tree Notation A n number of nodes e number of external nodes B C i number of internal nodes h height of the tree D E F G level set of nodes with the same depth I J Property 1.1 Level d has at most 2 d nodes. Proof Let us annotate max number of nodes at level d with mn(d). Clearly, mn(0) = 1, and mn(d) = 2*mn(d-1) for d 0. Hence, mn(d) = 2*mn(d-1) = 2*2*mn(d-2) = 2*[2*[..2*mn(0)]] = 2 d

Properties of Binary Trees (cont.) 5 Property 1.2 A full binary tree of height h has (2 h+1 1) nodes. Full binary tree. Proof n = mn(0) + mn(1) +.. + mn(d) = = 2 0 + 2 1 + 2 2 + + 2 h = = + 1 h + 1 h 1 2 1 2 = 2 1

Properties of Binary Trees (cont.) 6 Induction as a Proof Technique Assume we want to verify the correctness of a statement (P(n)). (1) First, prove that P(n) holds for n=1 (2, 3); (2) Assume it holds for an arbitrary n and try to prove it holds for (n+1) Property 2 In a binary tree, the number of external nodes is 1 more than the number of internal nodes, i.e. e = i + 1. Proof Clearly true for one node. Clearly true for tree nodes. Assume true for trees with up to n nodes.

Properties of Binary Trees (cont.) 7 Let T be a tree with n+1 nodes (top diagram). 1. Choose a leaf and its parent (which, of course, is internal). For example, the leaf a and parent p. 2. Remove the leaf and its parent (middle diagram). 3. Splice the tree back without the two nodes (bottom diagram). 4. Since S has n-1 nodes, S satisfies initial assumption. 5. T is just S + one leaf + one internal so it also satisfies the assumption. a p

Properties of Binary Trees (cont.) 8 Approach (2): Assume true for a tree with n nodes (e = i + 1). Now, we want to add new external nodes: 1. Cannot add only one external that would violate the property of proper binary tree. Hence, it cannot be: e = i + 2 2. Add two externals. In this case, one old external becomes internal, so we have: e new = (e-1)+2 = e+1 = i+1+1 = i+2 i new = i+1 Hence, e new = i new + 1

Properties of Binary Trees (cont.) 9 Property 3 The number of external nodes (e) satisfies: (h+1) e 2 h. Proof At every level (except last) there is only one internal node. Full binary tree. Property 4 The number of internal nodes (i) satisfies: h i 2 h -1. Proof Based on Property 3: Based on Property 2: h+1 e 2 h h+1 i+1 2 h h i 2 h -1

Properties of Binary Trees (cont.) 10 Property 5 The total number of nodes (n) satisfies: 2h+1 n 2 h+1-1. Proof Based on Property 3: (h+1) e 2 h Based on Property 2 and n=i+e: (h+1) (n+1)/2 2 h 2h+1 n 2 h+1-1 Property 6 The height (h) satisfies: log 2 (n+1)-1 h (n-1)/2. Proof Based on Property 5, the following two inequalities hold: 2h+1 n h (n-1)2 n 2 h+1-1 n+1 2 h+1 log 2 (n+1) h + 1 log 2 (n+1) - 1 h

Properties of Binary Trees (cont.) 11 Property 7 The height (h) satisfies: log 2 (e) h e-1. Proof Based on Property 6: Based on Property 2: log 2 (n+1)-1 h (n-1)/2 log 2 (2e-1+1)-1 h (2e-1-1)/2 log 2 (2e)-1 h e-1 log 2 (2)+log 2 (e)-1 h e-1 log 2 (e) h e-1

Properties of Binary Trees (cont.) 12 Summary of Properties Number of external, internal, and overall nodes as a function of tree s height n = e + i e = i + 1 (h+1) e 2 h h i 2 h -1 2h+1 n 2 h+1-1 All other expressions can be obtained from these three. Tree s height as a function of number of external, internal, of overall nodes log 2 (n+1)-1 h (n-1)/2 log 2 (e) h e-1 log 2 (i+1) h i

Binary Tree ADT: Interface 13 Binary Tree ADT Additional Methods extends Tree ADT, i.e. inherits all its methods public Position leftchild(position v); /* return the left child of a node */ /* error occurs if v is an external node */ public Position rightchild(position v); /* return the right child of a node */ /* error occurs if v is an external node */ public Position sibling(position v); /* return the sibling of a node */ /* error occurs if v is the root */

Binary Tree: Array-Based Implementation 14 Indexing Scheme for every node v of T, let its index/rank p(v) be defined as follows if v is the root: p(v) = 1 if v is the left child of node u: p(v) = 2p(u) if v is the right child of node u: p(v) = 2p(u) + 1 1 A B 2 3 C D E F G 4 5 6 7 No element at A B C D E F G rank 0! 0 1 2 3 4 5 6 7 Advantages simple implementation, easy access

Binary Tree: Array-Based Implementation (cont.) 15 Space Complexity let use the following notation n number of nodes in T p M maximum value of p(v) N array size (N=p M +1), i.e. space usage 1) Best Case: full, balanced tree all array slots occupied N = p M +1 = n+1 = O(n) 2) Worst Case: highly unbalanced tree many slots empty 1 2 3 6 7 14 15 height: h = (n-1)/2 max p(v): p M = 2 h+1 1 = 2 (n+1)/2 1 required: N = p M +1 = 2 (n+1)/2 = O(2 n ) array size max p M - as if this was full binary tree

Array-Based Binary Tree: Performance 16 Run Times Good Good! all methods, except positions and elements run in constant O(1) time Method position, elements swapelements, replaceelement root, parent, children leftchild, rightchild, sibling isinternal, isexternal, isroot expandexternal, removeaboveexternal Time O(n) O(1) O(1) O(1) O(1) O(1) Space Usage Poor! (in general) best case (full balanced tree): O(n) worst case (highly unbalanced tree): O(2 n )

Link Structure - Based Implementation of Binary Tree 17 Node in Linked Structure object containing for Binary Trees 1) element 2) reference to parent 3) reference to the right child 4) reference to the left child if node is the root: reference to parent = null if node is external: references to children = null parent left child element right child

18 Link Structure - Based Implement. of Binary Tree (cont.) Example 4 [ binary tree and its linked list implementation ] A B C A B C D E D E

BTNode ADT: Implementation 19 BTNode Class generalization of Position ADT, i.e. implements Position interface public class BTNode implements Position { private Object element; private BTNode left, right, parent; public BTNode(Object o, BTNode u, BTNode v, BTNode w) { setelement(o); setparent(u); setleft(v); setright(w); } public Object element() { return element; } public void setelement(object o) { element = o; }

BTNode ADT: Implementation (cont.) 20 public BTNode getleft() { return left; } public void setleft(btnode v) { left = v; } public BTNode getright() { return right; } public void setright(btnode v) { right = v; } } public BTNode getparent() { return parent; } public void setparent(btnode v) { parent = v; } Root Node: BTNode root = BTNode(o,null,v, w) External Node: BTNode root = BTNode(o, u, null, null)

BTNode ADT: Implementation (cont.) 21 Null_Node contains no elements, no children, has only reference to the parent implements Position interface! Extended BT every external node reference becomes reference to Null_Node with this approach, there is never a need to check whether a reference to a child is null implementation presented here, and in the textbook, does not employ Null_Node A null null null null null null (A.getLeft()).element(); A (A.getLeft()).element();

LinkedBinaryTree ADT: Implementation 22 LinkedBinaryTree Class implements BinaryTree interface, and also provides 2 additional methods 1) expandexternal 2) removeaboveexternal public class LinkedBinaryTree implements BinaryTree { } private Position root; /* reference to the root */ private int size; /* number of nodes */ public LinkedBinaryTree() { root = new BTNode(null, null, null, null); size = 1; } public void expandexternal (Position v) { } public void removeaboveexternal (Position v) { } For other methods, and their implementation details, see pp. 268-269 of the textbook.

LinkedBinaryTree ADT: Implementation (cont.) 23 expandexternal() Method transforms v from external into internal node, by creating 2 new external nodes and making them the children of v error occurs if v is internal A expandexternal(a) A public void expandexternal (Position v) { if (isexternal(v)) { ((BTNode) v ).setleft(new BTNode(null, (BTNode) v, null null); ((BTNode) v ).setrigth(new BTNode(null, (BTNode) v, null null); size += 2; } } Application of expandexternal() used for building a tree see pp. 27

LinkedBinaryTree ADT: Implementation (cont.) 24 removeaboveexternal() Method removes external node w together with its parent v, replacing v with the sibling of w error occurs if w is internal w v z removeaboveexternal(w) z Application of removeaboveexternal() used to dismantle a tree

LinkedBinaryTree ADT: Implementation (cont.) 25 public void removeaboveexternal (Position v) { if (isexternal(v)) { BTNode p = (BTNode) parent(v); BTNode s = (BTNode) sibling(v); v p s if (isroot(p)) { s.setparent(null); root = s; } else { }; BTNode g = (BTNode) parent(p); if (p == lefchild(g)) g.setleft(s); else g.setright(s); s.setparent(g); g v p s } } size-=2;

LinkedBinaryTree ADT: Implementation (cont.) 26 Run Times Good Good! all methods, except positions and elements run in constant O(1) time Space Complexity Good! only O(n), since there is one BTNode object per every node of the tree no empty slots as in array-based implementation Method position, elements swapelements, replaceelement root, parent, children leftchild, rightchild, sibling isinternal, isexternal, isroot expandexternal, removeaboveexternal Time O(n) O(1) O(1) O(1) O(1) O(1) Only immediate children!

LinkedBinaryTree ADT: Implementation (cont.) 27 Example 5 [ creating a tree ] LinkedBinaryTree t = new LinkedBinaryTree(); t.root().setelement( Albert ); t.expandexternal(tree.root()); t.root().leftchild().setelement( Betty ); t.root().rightchild().setelement( Chris ); t.expandexternal(tree.root().leftchild()); t.root().leftchild().leftchild().setelement( David ); t.root().leftchild().rightchild().setelement( Elvis ); Albert Betty Chris David Elvis