Trees. Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck. April 29, 2016

Similar documents
Trees and Intro to Counting

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 10 Notes. Class URL:

Encoding/Decoding, Counting graphs

March 20/2003 Jayakanth Srinivasan,

Hamilton paths & circuits. Gray codes. Hamilton Circuits. Planar Graphs. Hamilton circuits. 10 Nov 2015

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

COL351: Analysis and Design of Algorithms (CSE, IITD, Semester-I ) Name: Entry number:

Binary Trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Lecture 3: Graphs and flows

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Figure 4.1: The evolution of a rooted tree.

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Introduction to Computers and Programming. Concept Question

Foundations of Discrete Mathematics

Eulerian tours. Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck. April 20, 2016

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

Introduction to Graphs

Analysis of Algorithms

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Lower Bound on Comparison-based Sorting

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Copyright 2000, Kevin Wayne 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

Programming II (CS300)

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Algorithms and Data Structures

Data Structures and Algorithms

EE 368. Weeks 5 (Notes)

Trees. Arash Rafiey. 20 October, 2015

Chapter 4: Trees. 4.2 For node B :

CS521 \ Notes for the Final Exam

Data Structure. IBPS SO (IT- Officer) Exam 2017

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

CIS265/ Trees Red-Black Trees. Some of the following material is from:

These notes present some properties of chordal graphs, a set of undirected graphs that are important for undirected graphical models.

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Data Structures Question Bank Multiple Choice

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Solutions to relevant spring 2000 exam problems

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Definition of Graphs and Trees. Representation of Trees.

Analysis of Algorithms

Balanced Search Trees. CS 3110 Fall 2010

Balanced Binary Search Trees. Victor Gao

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

INDIAN STATISTICAL INSTITUTE

CS 206 Introduction to Computer Science II

Properties of red-black trees

Info 2950, Lecture 16

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

CSE 331: Introduction to Algorithm Analysis and Design Graphs

Weighted Graphs and Greedy Algorithms

Question 7.11 Show how heapsort processes the input:

CPS 102: Discrete Mathematics. Quiz 3 Date: Wednesday November 30, Instructor: Bruce Maggs NAME: Prob # Score. Total 60

Multiple-choice (35 pt.)

COMP Analysis of Algorithms & Data Structures

Algorithms. Deleting from Red-Black Trees B-Trees

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

BACKGROUND: A BRIEF INTRODUCTION TO GRAPH THEORY

Cut vertices, Cut Edges and Biconnected components. MTL776 Graph algorithms

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Binary Heaps in Dynamic Arrays

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

Lecture 22 Tuesday, April 10

Recitation 9. Prelim Review

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

CMSC th Lecture: Graph Theory: Trees.

COMP Analysis of Algorithms & Data Structures

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

Trees. Eric McCreath

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Tree traversals and binary trees

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

Content. 1 Algorithms. 2 Assignment and Knapsack problem 3 Bin Packing. 4 Scheduling

Randomized incremental construction. Trapezoidal decomposition: Special sampling idea: Sample all except one item

CS204 Discrete Mathematics. 11 Trees. 11 Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Binary Trees, Binary Search Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Algorithm Design and Analysis

Motivation for B-Trees

CSE 101. Algorithm Design and Analysis Miles Jones and Russell Impagliazzo Miles Office 4208 CSE Building

Chapter 9. Priority Queue

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Copyright 2000, Kevin Wayne 1

Transcription:

Trees Russell Impagliazzo and Miles Jones Thanks to Janine Tiefenbruck http://cseweb.ucsd.edu/classes/sp16/cse21-bd/ April 29, 2016

Another Special Type of Graph: Trees

Trees 1. Definitions of trees 2. Properties of trees 3. Revisiting uses of trees

(Rooted) Trees: definitions Rosen p. 747-749 A rooted tree is a connected directed acyclic graph in which one vertex has been designated the root, which has no incoming edges, and every other vertex has exactly one incoming edge.

(Rooted) Trees: definitions Rosen p. 747-749 A rooted tree is a connected directed acyclic graph in which one vertex has been designated the root, which has no incoming edges, and every other vertex has exactly one incoming edge. Top layer next layer Special case of DAGs from last class. Note that each vertex in middle has exactly one incoming edge from layer above. Edges are directed away from the root.

Trees? Which of the following directed graphs are trees (with root indicated in green)? A. C. B. D.

(Rooted) Trees: definitions Rosen p. 747-749 Internal vertices Root Leaf Leaf

(Rooted) Trees: definitions Rosen p. 747-749, 753 Root Height 0 Children of root Height 1 If vertex v is not the root, it has exactly one incoming edge, which is from its parent, p(v). Height of vertex v is given by the recurrence: h(v) = h( p(v) ) + 1 if v is not the root,and h(r) = 0

(Rooted) Trees: definitions Height of vertex v: h(v) = h( p(v) ) + 1 if v is not the root,and h(r) = 0 What is the height of the red vertex? A. 0 B. 1 C. 2 D. 3 E. None of the above.

(Rooted) Trees: definitions Height of vertex v: h(v) = h( p(v) ) + 1 if v is not the root,and h(r) = 0 Height of tree is maximum height of a vertex in the tree. Rosen p. 753 What is the height of the tree? A. 0 B. 1 C. 2 D. 3 E. None of the above.

Binary tree Rosen p. 749, 754 A binary tree is a rooted tree where every (internal) vertex has no more than 2 children. How many leaves does a binary tree of height 3 have? A. 2 B. 3 C. 6 D. 8 E. any of the above.

Binary tree Rosen p. 749, 754 A binary tree is a rooted tree where every (internal) vertex has no more than 2 children. How many leaves does a binary tree of height 3 have? A. 2 B. 3 C. 6 D. 8 E. any of the above. *See Theorem 5 for proof of upper bound*

Binary tree Which of the following are full binary trees? A. C. Rosen p. 749 A full binary tree is a rooted tree where every internal vertex has exactly 2 children. B. D.

Binary tree Rosen p. 749 A full binary tree is a rooted tree where every internal vertex has exactly 2 children. At most how many vertices are there in a full binary tree of height h? A. C. B. D. Max number of vertices when tree is balanced

Binary tree Rosen p. 749 A full binary tree is a rooted tree where every internal vertex has exactly 2 children. Key insight: number of vertices doubles on each level. If n is number of vertices: so 1 + 2 + 4 + 8 + + 2 h = 2 h+1-1 i.e. n = 2 h+1-1 h = log(n+1) -1 i.e. Max number of vertices when tree is balanced

Binary tree Rosen p. 749 Relating height and number of vertices: This is what we just proved. log(n+1) 1 <= h <= How do we prove? What tree with n vertices has the greatest possible height?

Binary tree Rosen p. 749 Relating height and number of vertices: This is what we just proved. log(n+1) 1 <= h <= n-1 How do we prove? What tree with n vertices has the greatest possible height?

Trees 1. Definitions of trees 2. Properties of trees 3. Revisiting uses of trees In data structures: Max heap

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic Implementation Each vertex is an object with the fields p = parent lc = left child rc = right child value When is p null? A. If we have an error in our implementation. B. When the value is 0. C. When the vertex is a leaf node. D. When the vertex is the root node. E. None of the above.

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic Implementation Each vertex is an object with the fields p = parent lc = left child rc = right child value Under which of these conditions is lc always null? A. If we have an error in our implementation. B. When the value is 0. C. When the vertex is a leaf node. D. When the vertex is the root node. E. None of the above.

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic Banana Apple Peach For each vertex v If x is in subtree rooted at lc(v), value(x) <= value (v). If x is in the subtree rooted at rc(v), value(x)>= value(v). Coconut Mango Papaya Pear

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic Banana Apple Peach How would you search for "orange? Coconut Mango Pear Papaya

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic To search for target T in a binary search tree. 1. Compare T to value(r) where r is the root. 2. If T = value(r), done J. 3. If T < value(r), search recursively starting at lc(r). 4. If T > value(r), search recursively starting at rc(r).

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic To search for target T in a binary search tree. 1. Compare T to value(r) where r is the root. 2. If T = value(r), done J. 3. If T < value(r), search recursively starting at lc(r). 4. If T > value(r), search recursively starting at rc(r). How long does this take?

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic To search for target T in a binary search tree. 1. Compare T to value(r) where r is the root. 2. If T = value(r), done J. 3. If T < value(r), search recursively starting at lc(r). 4. If T > value(r), search recursively starting at rc(r). How long does this take? Constant time at each level, number of levels is height+1.

Binary Search Trees Facilitate binary search (must maintain sorted order of data) Dynamic To search for target T in a binary search tree. 1. Compare T to value(r) where r is the root. 2. If T = value(r), done J. 3. If T < value(r), search recursively starting at lc(r). 4. If T > value(r), search recursively starting at rc(r). How long does this take? Time proportional to height!

Unrooted trees Rosen p. 746 An unrooted tree is a connected undirected graph with no cycles.

Equivalence between rooted and unrooted trees Theorem: An undirected graph is an unrooted tree if and only if it contains all the edges of some rooted tree. What does this mean? (1) If we replace all directed edges in a rooted tree with undirected edges, the result will be an unrooted tree. (2) There is always some way to put directions on the edges of an unrooted tree to make it a rooted tree.

Equivalence between rooted and unrooted trees Goal (1): If we replace all directed edges in a rooted tree with undirected edges, the result will be an unrooted tree. What do we need to prove? A. The resulting undirected graph will be connected. B. The resulting undirected graph will be undirected. C. The resulting undirected graph will not have cycles. D. All of the above.

Equivalence between rooted and unrooted trees Goal (1): If we replace all directed edges in a rooted tree with undirected edges, the result will be an unrooted tree. SubGoal (1a): this resulting graph is connected, i.e. between any two vertices u and v there is a path in the graph. Idea: To find path between purple and orange, follow parents of purple all the way to root, then follow its children down to orange.

Equivalence between rooted and unrooted trees Goal (1): If we replace all directed edges in a rooted tree with undirected edges, the result will be an unrooted tree. SubGoal (1b): this resulting graph has no cycles. Assume by contradiction that there exists a cycle and let v be the vertex with the deepest height. Since v is in a cycle, there are two edges in the cycle incident with v. There is one edge from the parent. All other edges go to deeper vertices contradicting that v is the deepest.

Equivalence between rooted and unrooted trees Goal (2): There is always some way to put directions on the edges of an unrooted tree to make it a rooted tree. Idea: finding right directions for edges will be similar to finding topological sort last class.

Equivalence between rooted and unrooted trees Goal (2): There is always some way to put directions on the edges of an unrooted tree to make it a rooted tree. SubGoal (2a): Any unrooted tree with at least two vertices has a vertex of degree exactly 1. Proof: Towards a contradiction, assume that all vertices have degree 0 or >=2. Since a tree is connected, eliminate the case of degree-0 vertices. Goal: construct a cycle to arrive at a contradiction. Start at any vertex u 0. Pick u i+1 so that it is adjacent to u i but is not u i-1. Why? Get u 0, u 1,, u n. By Pigeonhole Principle, must repeat. Cycle!

Equivalence between rooted and unrooted trees Goal (2): There is always some way to put directions on the edges of an unrooted tree to make it a rooted tree. SubGoal (2b): If T is unrooted tree and v has degree 1 in T, then T-{v} is unrooted tree. Proof: To check that T-{v} is unrooted tree, * confirm T-{v} is connected and * T-{v} does not have a cycle.

Equivalence between rooted and unrooted trees Goal (2): There is always some way to put directions on the edges of an unrooted tree to make it a rooted tree. SubGoal (2b): If T is unrooted tree and v has degree 1 in T, then T-{v} is unrooted tree. Proof: To check that T-{v} is unrooted tree, * confirm T-{v} is connected and * T-{v} does not have a cycle.

Equivalence between rooted and unrooted trees Goal (2): There is always some way to put directions on the edges of an unrooted tree to make it a rooted tree. Using the subgoals to achieve the goal: Root(T: unrooted tree with n nodes) 1. If n=1, let the only vertex v be the root, set h(v):=0, and return. 2. Find a vertex v of degree 1 in T, and let u be its only neighbor. 3. Root(T-{v}). 4. Set p(v):= u and h(v):=h(u)+1. Recursion!

Example vertex A B C D E F degree 1 3 1 3 1 1