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

Similar documents
Binary Trees

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Introduction and Overview

TREES. Trees - Introduction

Binary Trees, Binary Search Trees

Topic 14. The BinaryTree ADT

CE 221 Data Structures and Algorithms

Garbage Collection: recycling unused memory

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

tree nonlinear Examples

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

Trees. CSE 373 Data Structures

Programming II (CS300)

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

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

Why Do We Need Trees?

CSC148 Week 6. Larry Zhang

CS24 Week 8 Lecture 1


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

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

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

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

Trees. Truong Tuan Anh CSE-HCMUT

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

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

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

CS 151. Binary Trees. Friday, October 5, 12

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

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! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Binary Trees Fall 2018 Margaret Reid-Miller

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

CSCI2100B Data Structures Trees

Binary Trees and Binary Search Trees

DDS Dynamic Search Trees

March 20/2003 Jayakanth Srinivasan,

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

CS 206 Introduction to Computer Science II

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

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

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

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Trees. Trees. CSE 2011 Winter 2007

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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.

COSC 2011 Section N. Trees: Terminology and Basic Properties

Name CPTR246 Spring '17 (100 total points) Exam 3

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

TREES Lecture 10 CS2110 Spring2014

Stacks, Queues and Hierarchical Collections

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

Data Structure - Binary Tree 1 -

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

CSI33 Data Structures

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

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

Chapter 5. Binary Trees

Algorithms and Data Structures

3137 Data Structures and Algorithms in C++

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

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

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Associate Professor Dr. Raed Ibraheem Hamed

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

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

Trees. Tree Structure Binary Tree Tree Traversals

Trees, Binary Trees, and Binary Search Trees

CS301 - Data Structures Glossary By

TREES Lecture 12 CS2110 Spring 2019

If you took your exam home last time, I will still regrade it if you want.

Data Structures and Algorithms for Engineers

Data Structures Question Bank Multiple Choice

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

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

BBM 201 Data structures

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

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

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Spring 2018 Profs Bill & Jon

Recursive Data Structures and Grammars

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

Lecture 26. Introduction to Trees. Trees

Introduction to Binary Trees

Binary Search Trees Treesort

Trees. A tree is a directed graph with the property

CMPT 225. Binary Search Trees

Principles of Computer Science

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

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

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Transcription:

Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays, vectors, linked lists. We then put some restrictions on those structures, looking at stacks, queues, deques, and ordered structures. Just like we can write programs that can branch into a number of directions, we can design structures that have branches. Today, we ll start looking at our first more complicated structure: trees. In the structures we have studied so far, elements are very specifically ordered. In our list structures, we can refer to elements by an index. One way to think of this is that every element has unique successor. In a trees, an element may have multiple successors. We usually draw trees vertically, but upside-down, in computer science. You won t see trees in nature that grow with their roots at the top (but you can see some at Mass MoCA over in North Adams). Examples of Trees Expression trees One example of a tree is an expression tree: The expression can be represented as (*(4-))+((+7)/3) + * / - + 3 4 7

Once we have an expression tree, how can we evaluate it? We evaluate left subtree, then evaluate right subtree, then perform the operation at root. The evaluation of subtrees is recursive. Tournament Brackets Another example is a tree representing a tournament bracket: 4 3 8 4 5 7 3 6 6 8 9 4 3 5 5 7 0 3 4 6 or 4 3 8 4 5 7 3 6 8 9 4 5 7 0 3 6 Tree of Descendants One popular use of a tree is a pedigree chart looking at a person s ancestors. Instead, let s look at a person s descendants. (Example drawn in class). Tree Definitions and Terminology There are a lot of terms we will likely encounter when dealing with tree structures: A tree is either empty or consists of a node, called the root node, together with a collection of (disjoint) trees, called its subtrees. An edge connects a node to its subtrees The roots of the subtrees of a node are said to be the children of the node.

There may be many nodes without any successors: These are called leaves or leaf nodes. The others are called interior nodes. All nodes except root have unique predecessor, or parent. A collection of trees is called a forest. Other terms are borrowed from the family tree analogy: sibling nodes sharing the same parent ancestor a node s parent, parent s parent, etc. descendant a node s child, child s child, etc. Some other terms we ll use: A simple path is series of distinct nodes such that there is an edge between each pair of successive nodes. The path length is the number of edges traversed in a path (equal to the number of nodes on the path - ) The height of a node is length of the longest path from that node to a leaf. The height of the tree is the height of its root node. The depth of a node is the length of the path from the root to that node. The degree of a node is number of its direct descendents. The idea of the level of a node is defined recursively: The root is at level 0. The level of any other node is one greater than the level of its parent. Equivalently, the level of a node is the length of a path from the root to that node. We often encounter binary trees trees whose nodes are all have degree. We will also orient the trees: each subtree of a node is defined as being either the left or right. For binary trees, there are additional properties to consider, though not all terms are universally accepted to have the meanings below. a full binary tree is one in which every node has either 0 or children (i.e., no only children allowed) 3

a complete binary tree is one where each level contains all possible nodes exept the last, and any missing nodes must be all the way to the right a perfect binary tree is one where all levels in existence contain all possible nodes (some texts use this as the definition of a full tree) A Binary Tree for Arithmetic Expressions Much like we did for our first linked list, the RatioList that was capable of storing only Ratio objects, we will consider a specialized binary tree as our first actual tree stucture. This one will store and evaluation binary expression trees. A More Generic Binary Tree There are many possible specifications and implementations of binary trees that allow reasonable insertion and deletion of elements. We will consider the one provided in the structure package. Unlike what we have seen for most of the structures to this point, the structure package does not define an interface for binary trees and then use implement that in one or more concrete classes. Structure simply implements the concrete class BinaryTree. Unlike the linked list implementations, where we do not give the users of the structures access to the actual list nodes, the binary tree exposes more of its structure to users. The actual recursive data structure is given directly to users. Given this design decision, the implementation needs to ensure that changes that might be made by users cannot render a tree invalid. For example, if node A has a child reference to a node B, then it must always be the case the node B s parent reference is to A. The tree is constructed of instances of class BinaryTree. Each BinaryTree object has the fields it needs to store its value and the parent and child references. structure5/binarytree.java A node has 4 fields, which we might draw as follows: parent val left right There are plenty of things to notice and think about here: We have three constructors. 4

. The first is used to create an empty BinaryTree. We will see this constructor used by the other constructors to create these empty trees in place of having null references to represent empty subtrees. This allows most methods to be called on these, eliminating lots of special cases. We could also use null to represent empty trees, but this would mean some extra code in several methods. Note that only empty tree nodes may contain a null value. Regular tree nodes must contain non-null values.. The second constructor creates a tree node with no children (a leaf node) containing a particular value. 3. The third constructor creates a tree node that may have children. Note that if a user of this constructor specifies a null child, it is replaces with an empty tree instance. We have accessors to retrieve the children or parent of a node. Note that the value and subtree links can be set by the user, but the parent reference is set only in a protected method. This is done to make sure we don t put the tree into a (bad) state mentioned above where a parent points to a child but the child doesn t point back to the parent. We have a variety of other (self-explanatory) methods to retrieve information about a tree: size, root, height, depth, isfull, isempty, iscomplete. Note the recursive nature of many of these methods. Binary Tree Example We can convert our earlier example of a binary expression tree to use structure5 s BinaryTree implementation. The example again represents the arithmetic expression: ((4+3)*(0-5))/ See Example: BinaryExpressionTree There are two versions of this program:. BinaryExpressionTree.java stores the operators and values to be used as Strings. This lets us use a BinaryTree<String>. Since some are operators and some are numbers, we need to check and treat as appropriate, based on the contents of the String.. BinaryExpressionTreeObject.java stores the operators as Characters and the numbers as Integers. 5

Here we instead use a BinaryTree<Object>, since that s the type that can represent both a Character and an Integer. We check the actual type of the value retrieved with the instanceof operator and use the value as appropriate In both cases, note the treestring method that prints our binary tree in a nice format. Tree Traversals Iterating over all values in our linear structures is usually fairly easy. Moreover, one or two orderings of the elements are the obvious choices for our iterations. Some structures, like an array or a Vector, allow us to traverse from the start to the end or from the end back to the start very easily. A SinglyLinkedList, however, is most efficiently traversed only from the start to the end. For trees, there is no single obvious ordering. Do we visit the root first, then go down through the subtrees to the leaves? Do we visit one or both subtrees before visiting the root? Think about the orderings we used for the expression tree: one was used to evaluate (where we needed to do something with both children before we could apply an operator) and a different one was used to print it (where we needed to print the operator between the left and right child printouts). We will consider 4 standard tree traversals for our binary trees:. preorder: visit the root, then visit the left subtree, then visit the right subtree.. in-order visit the left subtree, then visit the root, then visit the right subtree. 3. postorder: visit the left subtree, then visit the right subtree, then visit the root. 4. level-order: visit the node at level 0 (the root), then visit all nodes at level, then all nodes at level, etc. For example, consider the preorder, in-order, and postorder traversals of the expression tree we looked at in the example code: preorder leads to prefix notation: / * + 4 3-0 5 in-order leads to infix notation: 4 + 3 * 0-5 / postorder leads to postfix notation: 4 3 + 0 5 - * / 6

The iterator concept fits nicely with tree traversals, but since the code for the iterators in the text is somewhat complex, so we will first consider traversals without iterators. In our first traversal examples, we will build a small binary tree of Integer values and call methods that perform the traversal. Here, visiting a tree node involves passing its value to the method process. See Example: BTTraversals First, note the construction of the tree. We build the tree from bottom up, but do not store the subtrees in local variables during construction we simply construct them in the parameters of the constructor for the next level up. Now, consider each of the traversal implementations. The in-order, preorder, and postorder traversls work exactly as we would expect. Each is recursive, and we visit the subtrees and the root node as defined for each ordering. The level-order traversal is a bit trickier. We need to visit the root of each subtree before doing anything in the next level. This calls for a queue! For all of the others, we used a stack, just without thinking about it. We took advantage of the call stack to support the recursion! Tree Iterators The structure package has implementations of iterators for each of these four traversals. Whereas in the do{pre,post,in}order methods above, we were able to take advantage of the computer s run-time stack, we need to have a stack explicitly declared and used in the iterator implementations. The complexity of the iterators varies with the type of traversal. We need to make sure we get things onto the stack (or queue, in the case of the level order) in the right order. At any time, we want the stack/queue to contain the tree nodes that still need to be visited. Visiting a non-leaf node will result in additional nodes being added to the stack/queue. In all cases, iteration can continue as long as something remains on the stack/queue. We will consider this example tree: 6 4 9 5 7 0 3 8 7

In each case, recall that we need to satisfy the iterator interface (actually, the AbstractIterator in structure) by providing:. a constructor. a reset method 3. a hasnext method 4. a next method 5. a get method We look at each in turn. structure5/btpreorderiterator.java Our preorder traversal visits the root first, followed by the left subtree, then the right subtree. Recall that we implicitly used the run-time stack for the non-iterator traversal code. Here, we manage the stack. At any point, we want the tree node on the top of the stack to be the next tree node that needs to be visited. We want to visit the root, then the left subtree, then the right subtree. So to start or reset our iteration, we initialize the stack with the root node. A next operation just involves popping, then processing the node on top of the stack, then pushing its right and left subtrees (in that order, since we want to process the left first). Finally, we can implement hasnext by just checking if the stack is empty, which will tell is that the traversal has been completed. structure5/btinorderiterator.java For this traversal, we need to visit the left subtree, then the root, then the right subtree. Here, the first thing we want to visit is the deepest, leftmost child. So we need to initialize the state of our iterator so that that node (the deepest, leftmost child) is on top of the stack. To do this, we push the root, and all of the left subtrees until we come to a node which doesn t have a left subtree. A next operation here involves popping the top value off the stack to be returned, then dealing with its right subtree. The first thing there that needs to happen is again its leftmost branch, so we need to push the right subtree then all of its left children. structure5/btpostorderiterator.java Here, we visit the left subtree, then the right subtree, and finally the root. 8

This is the tricky one. First, if there is a left subtree, we need to push down through those left subtrees as far as we can. If any node has no left subtree but has a right subtree, push that instead. Continue to a leaf. A next operation involves popping the top value to be returned. If the thing we just popped is a left child, push the sibling and its left children (or right when there is no left) until we get to a leaf again. structure5/btlevelorderiterator.java Here, we visit the tree level by level. This one is actually quite easy. We have a queue instead of a stack. We start by enqueueing the root, as this is the first thing we want to visit. When we visit a node, we enqueue its children. The BTTraversals example also demonstrates the use of these iterators. 9