Introduction to Computers and Programming. Concept Question

Similar documents
March 20/2003 Jayakanth Srinivasan,

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Binary Trees, Binary Search Trees

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

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

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

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

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

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

TREES. Trees - Introduction

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

CSI33 Data Structures

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Binary Trees

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

Data Structure - Binary Tree 1 -

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Trees. (Trees) Data Structures and Programming Spring / 28

CSI33 Data Structures

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

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

CE 221 Data Structures and Algorithms

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

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

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1


Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

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

BBM 201 Data structures

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Trees. Trees. CSE 2011 Winter 2007

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree Data Structures CSC 221

Trees Algorhyme by Radia Perlman

Data Structures and Algorithms for Engineers

Trees, Binary Trees, and Binary Search Trees

License. Discrete Mathematics. Tree. Topics. Definition tree: connected graph with no cycle. examples. c T. Uyar, A. Yayımlı, E.

Chapter 20: Binary Trees

CMSC th Lecture: Graph Theory: Trees.

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Graph Algorithms Using Depth First Search

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

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

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

Trees. Truong Tuan Anh CSE-HCMUT

Trees and Binary Trees

Associate Professor Dr. Raed Ibraheem Hamed

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Trees. Tree Structure Binary Tree Tree Traversals

Analysis of Algorithms

Binary Trees Fall 2018 Margaret Reid-Miller

Foundations of Discrete Mathematics

CSC148 Week 6. Larry Zhang

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Algorithms and Data Structures

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

CS 171: Introduction to Computer Science II. Binary Search Trees

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

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

Trees. Eric McCreath

Programming II (CS300)

Binary Trees. Height 1

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Non-context-Free Languages. CS215, Lecture 5 c

CS204 Discrete Mathematics. 11 Trees. 11 Trees

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

CSE 230 Intermediate Programming in C and C++ Binary Tree

Topic Binary Trees (Non-Linear Data Structures)

Advanced Tree Data Structures

Trees. CSE 373 Data Structures

Stacks, Queues and Hierarchical Collections

EE 368. Weeks 5 (Notes)

A set of nodes (or vertices) with a single starting point

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

Successor/Predecessor Rules in Binary Trees

Binary Trees and Binary Search Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Introduction to Binary Trees

DATA STRUCTURES USING C

Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Binary Trees. Examples:

CSCI2100B Data Structures Trees

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

Definition of Graphs and Trees. Representation of Trees.

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

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

LECTURE 13 BINARY TREES

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

COMP 250 Fall binary trees Oct. 27, 2017

IX. Binary Trees (Chapter 10)

CMPT 225. Binary Search Trees

Tree Structures. A hierarchical data structure whose point of entry is the root node

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Transcription:

Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 7 April 2 2004 Concept Question G1(V1,E1) A graph G(V, where E) is V1 a finite = {}, nonempty E1 = {} set of G2(V2,E2) vertices and where a set V2 of edges = {a,b}, E2 = {} G1(V1,E1) where V1 = {}, E1 = {} G2(V2,E2) where V2 = {a,b}, E2 = {} 1. Both G1 and G2 are Graphs 2. Only G1 is a Graph 3. Only G2 is a Graph 4. Neither G1 nor G2 are Graphs

[Theorem] Theorem: a mathematical statement that can be shown to be true Can be proved using other theorems, axioms (statements which are given to be true) and rules of inference Lemma: a pre-theorem or result needed to prove a theorem Corollary: post-theorem or result which follows directly from a theorem Proposition Claim Remark Why should we use trees? 4 2 15 9 5 7 5 4 9 2 7 15 Binary Search Tree

Trees A tree is a connected undirected graph with no simple circuits. it cannot contain multiple edges or loops Theorem : An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. 5 Which graphs are trees? a) b) c) d) 6

Rooted Tree A directed graph G is called a rooted tree if there exists a vertex u so that for each v V, there is exactly one path between u and v The in-degree of u is 0 and the in-degree of all other vertices is 1 For an undirected graph, different choices of the root produces different trees Choice of Root f e a b g d d b g a c a c e f c d g b e f Examples of of Rooted Trees?

Internal Vertex A vertex that has children is called an internal vertex A graph H(W, F) is a subgraph of a graph G(V,E) iff W V and F E The subtree at vertex v is the subgraph of the tree consisting of vertex v and its descendants and all edges incident to those descendants 9 Tree Properties The parent of a non-root vertex v is the unique vertex u with a directed edge from u to v. A vertex is called a leaf if it has no children. The ancestors of a non-root vertex are all the vertices in the path from root to this vertex. The descendants of vertex v are all the vertices that have v as an ancestor. 10

Tree Properties The level of vertex v in a rooted tree is the length of the unique path from the root to v. The height of a rooted tree is the maximum of the levels of its vertices. a b c d e f g Level of of vertex f = 2 Height of of tree tree = 4 h i j k l m 11 Binary Tree An m-ary tree is a rooted tree in which each internal vertex has at most m children A rooted tree is called a binary tree if every internal vertex has no more than 2 children. The tree is called a full binary tree if every internal vertex has exactly 2 children. 12

Tree Properties Theorem: A tree with N vertices has N-1 edges. Theorem: There are at most 2 H leaves in a binary tree of height H. Corallary: If a binary tree with L leaves is full and balanced, then its height is H = log 2 L A balanced tree with height h is a m-ary tree with all leaves being at levels h or h-1 13 Examples T1 T2 T3

Ordered Binary Tree An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered. In an ordered binary tree, the two possible children of a vertex are called the left child and the right child, if they exist. 15 Example a b c d e f g FAREWELL TO AMY ADAMS The party atmosphere of Motown and the terrific backing rhythms of the Funk Brothers just couldn't bring out the best in Amy, who was sent home this week. h i j k l m d, e a c, a Children of b? Parent of b? Ancestors of g? Descendants of b? d, e, h, i Leafs? Internal vertices? Left child of g? k Right child of g? h, i, e, j, k, m a, b, c, d, f, g l

Traversal Algorithms A traversal algorithm is a procedure for systematically visiting every vertex of an ordered binary tree Tree traversals are defined recursively Three commonly used traversals are: preorder inorder postorder 17 PREORDER Traversal Algorithm Let T be an ordered binary tree with root R If T has only R then R is the preorder traversal Else Let T 1, T 2 be the left and right subtrees at R Visit R Traverse T 1 in preorder Traverse T 2 in preorder 18

Record Definition type Node; type Nodeptr is access Node; type Node is record Element : Elementtype; Left_Child : Nodeptr; Right_Child : Nodeptr; end record; 11 9 13 8 10 12 14 INORDER Traversal Algorithm Let T be an ordered binary tree with root R If T has only R then R is the inorder traversal Else Let T 1, T 2 be the left and right subtrees at R Traverse T 1 in inorder Visit R Traverse T 2 in inorder 20

POSTORDER Traversal Algorithm Let T be an ordered binary tree with root R If T has only R then R is the postorder traversal Else Let T 1, T 2 be the left and right subtrees at R Traverse T 1 in postorder Traverse T 2 in postorder Visit R 21 Binary Expression Tree A special kind of binary tree in which: Each leaf node contains a single operand Each inner vertex contains a single binary operator The left and right subtrees of an operator node represent sub-expressions that must be evaluated before applying the operator at the root of the subtree. 22

Binary Expression Tree ROOT - 8 5 INORDER TRAVERSAL: 8-5 has value 3 PREORDER TRAVERSAL: - 8 5 POSTORDER TRAVERSAL: 8 5-23 Binary Expression Tree * + 3 4 2 What value does it have? ( 4 + 2 ) * 3 = 18 24

Binary Expression Tree * + 3 4 2 Infix: ( ( 4 + 2 ) * 3 ) Prefix: * + 4 2 3 Postfix: 4 2 + 3 * 25 Levels Indicate Precedence When a binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation. Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always the last operation performed. 26

Binary Expression Tree * - / 8 5 + 3 4 2 Infix: ( ( 8-5 ) * ( ( 4 + 2 ) / 3 ) ) Prefix: * - 8 5 / + 4 2 3 Postfix: 8 5-4 2 + 3 / * 27 Trees - Glossary Perfectly balanced tree M-ary tree Height balanced tree Inner Vertex Root Leaf B A C Parent of B and C Child of A B and C are siblings