CE 221 Data Structures and Algorithms

Similar documents
Binary Trees, Binary Search Trees

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

CSCI2100B Data Structures Trees

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

Trees. Trees. CSE 2011 Winter 2007

March 20/2003 Jayakanth Srinivasan,

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

Binary Trees

TREES. Trees - Introduction

Trees. CSE 373 Data Structures

Trees, Binary Trees, and Binary Search Trees

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

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

CSI33 Data Structures

Why Do We Need Trees?

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

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


Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

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

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

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

Programming II (CS300)

Garbage Collection: recycling unused memory

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

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

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

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

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

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

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.

Algorithms and Data Structures

Introduction to Computers and Programming. Concept Question

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

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

CSC148 Week 6. Larry Zhang

Chapter 20: Binary Trees

Trees. Truong Tuan Anh CSE-HCMUT

CSI33 Data Structures

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

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

Formal Languages and Automata Theory, SS Project (due Week 14)

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

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

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

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

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CS 206 Introduction to Computer Science II

Binary Trees. Height 1

Binary Trees. Reading: Lewis & Chase 12.1, 12.3 Eck Programming Course CL I

Trees Algorhyme by Radia Perlman

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

Binary Trees Fall 2018 Margaret Reid-Miller

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

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

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

Topic 14. The BinaryTree ADT

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

INF2220: algorithms and data structures Series 1

BBM 201 Data structures

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Trees. R. J. Renka 10/14/2011. Department of Computer Science & Engineering University of North Texas. R. J. Renka Trees

Data Structure - Binary Tree 1 -

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Recursive Data Structures and Grammars

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

LECTURE 13 BINARY TREES

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

tree nonlinear Examples

Objectives. In this session, you will learn to:

Trees, Part 1: Unbalanced Trees

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

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

Data Structures and Algorithms for Engineers

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

INF2220: algorithms and data structures Series 1

Data Structures and Algorithms

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

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

a graph is a data structure made up of nodes in graph theory the links are normally called edges

COMP : Trees. COMP20012 Trees 219

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

[ DATA STRUCTURES ] Fig. (1) : A Tree

Associate Professor Dr. Raed Ibraheem Hamed

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

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

Trees. Tree Structure Binary Tree Tree Traversals

Trees: examples (Family trees)

Data Structures Question Bank Multiple Choice

Chapter 5. Binary Trees

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

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Transcription:

CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1

Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The collection can be empty; otherwise, a tree consists of a distinguished node r, called the root, and zero or more nonempty (sub)trees T 1, T 2,..., T k, each of whose roots are connected by a directed edge from r. The root of each subtree is said to be a child of r, and r is the parent of each subtree root. Izmir University of Economics 2

Preliminaries - II Observation: For a tree with N nodes, there are N-1 edges. Proof: Each edge connects a node to its parent and every node except the root has one parent. Izmir University of Economics 3

Preliminaries - III The root is A. Node F has A as a parent and K, L, and M as children. Each node may have an arbitrary number of children, possibly zero. Nodes with no children are known as leaves (leaf node). B, C, H, I, P, Q, K, L, M, and N are leaves. Nodes with the same parent are siblings; thus K, L, and M are all siblings. Grandparent and grandchild relations can be defined in a similar manner. Izmir University of Economics 4

Preliminaries - IV A path from node n 1 to n k is defined as a sequence of nodes n 1, n 2,..., n k such that n i is the parent of n i+1 for 1 i < k. The length of this path is the number of edges on the path, namely k - 1. There is a path of length zero from every node to itself. Notice that there is exactly one path from the root to each node. For any node n i, the depth of n i is the length of the unique path from the root to n i. Thus, the root is at depth 0. The height of n i is the length of the longest path from n i to a leaf. Thus all leaves are at height 0. The height of a tree is equal to the height of the root. The depth of a tree is equal to the depth of the deepest leaf which is always equal to the height of the tree. If there is path from n 1 to n 2 (n 1 n 2 ), then n 1 is an (proper) ancestor of n 2 and n 2 is a (proper) descendant of n 1. Izmir University of Economics 5

Implementation of Trees One way is to have in each node besides its data, a link to to each child of that node. However, since the number of children per node can vary greatly and is not known in advance, it might be infeasible. The solution is simple: keep the children in a linked list of tree nodes. Izmir University of Economics 6

Tree Traversals with an Application - I One popular use is the directory structure in many common operating systems. The root of this directory is /usr. (The asterisk next to the name indicates that /usr is itself a directory.) /usr has three children, mark, alex, and bill, which are themselves directories. Thus, /usr contains three directories and no regular files. The filename /usr/mark/book/ch1.r is obtained by following the leftmost child three times. Each / after the first indicates an edge; the result is the full pathname. Izmir University of Economics 7

Tree Traversals with an Application - II Suppose we would like to list the names of all of the files in the directory. Our output format will be that files that are at depth d will have their names indented by d tabs. The strategy below is known as preorder traversal. In a preorder traversal, work at a node is performed before (pre) its children. If there are N file names, then the running time is O(N). Izmir University of Economics 8

Tree Traversals with an Application - III Another common method of traversing a tree is the postorder traversal. With it, the work at a node is performed after (post) its children are evaluated. As an example, the same directory structure as before, is represented with the numbers in parentheses (the number of disk blocks taken up by each file). Since the directories are themselves files, they have size too. The running time of calculating the size each node is O(N) again. Izmir University of Economics 9

Binary Trees A binary tree is a tree in which no node can have more than two children. Subtrees T L and T R which could both be possibly empty. The depth of an average binary tree is considerably smaller than N ( actually O( N ) ). For a special type of binary tree, namely the binary search tree, the average value of depth is O (log N). Unfortunately it can be as large as N - 1. Izmir University of Economics 10

Implementation Because a binary tree has at most two children, we can keep direct links to them. The declaration of tree nodes is similar in structure to that for doubly linked lists, in that a node is a structure consisting of the element information plus two pointers (left and right) to other nodes. Trees are generally drawn as circles connected by lines. NULL links are not explicitly drawn. Why? A binary tree with N nodes, has N+1 NULL links. Proof: N i : #nodes with i children N 0 +N 1 +N 2 =N (1) // #nodes N 1 +2*N 2 =N-1 (2) // #edges multiply (1) by 2 and subtract (2) 2*N 0 +N 1 =2*N-(N-1)=N+1 Izmir University of Economics 11

An Example: Expression Trees The leaves of an expression tree are operands, such as constants or variable names, and the other nodes contain operators. Operator nodes might have 1 (unary minus), 2, or more than two children. We can evaluate an expression tree, T, by applying the operator at the root to the values obtained by recursively evaluating the left and right subtrees. Inorder traversal: produce an infix expression by first recursively processing left, then, output the operator at the root, and finally recursively process the right. the left subtree evaluates to a + (b * c) and the right subtree evaluates to ((d *e) + f )*g. The entire tree therefore represents (a + (b*c)) + (((d * e) + f)* g). Izmir University of Economics 12

Constructing an Expression Tree - I We now give an algorithm to convert a postfix expression into an expression tree. Since we already have an algorithm to convert infix to postfix, we can generate expression trees from the two common types of input. The method we describe strongly resembles the postfix evaluation algorithm of Section 3.2.3. MakeNode(element,left,right) creates a class BinaryNode, Returning a reference to it stack= ; while (!eof(expression)){ read(ch); if (isoperand(ch)) stack.push(makenode(ch, NULL, NULL)); else if (isoperator(ch)){ } } T1=stack.topAndPop(); T2=stack.topAndPop(); stack.push(makenode(ch, T2, T1)); Izmir University of Economics 13

Constructing an Expression Tree - II As an example, suppose the input is: a b + c d e + * * a b + c d e + * * a b + c d e + * * a b + c d e + * * a b + c d e + * * Izmir University of Economics 14

Constructing an Expression Tree - III a b + c d e + * * a b + c d e + * * Izmir University of Economics 15

Homework Assignments 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.8, 4.31, 4.33, 4.40, 4.43, 4.45, You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics 16