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

Similar documents
Binary Trees

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

Objectives. In this session, you will learn to:

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

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

Binary Trees, Binary Search Trees

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

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

Binary Search Trees. What is a Binary Search Tree?

Data Structure - Binary Tree 1 -

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

Data Structures And Algorithms

Tree Data Structures CSC 221

Programming II (CS300)

March 20/2003 Jayakanth Srinivasan,

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

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

Chapter 20: Binary Trees

CSCI2100B Data Structures Trees

Trees. CSE 373 Data Structures

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

COSC 2011 Section N. Trees: Terminology and Basic Properties

IX. Binary Trees (Chapter 10)

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

Garbage Collection: recycling unused memory

CS24 Week 8 Lecture 1

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

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.

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

TREES. Trees - Introduction

Friday Four Square! 4:15PM, Outside Gates

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Data Structures - Binary Trees 1

CSC148 Week 6. Larry Zhang

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

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

Trees. Truong Tuan Anh CSE-HCMUT

Why Do We Need Trees?

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

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

CE 221 Data Structures and Algorithms

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

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

Data Structures - Binary Trees and Operations on Binary Trees

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

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

CSC148H Week 8. Sadia Sharmin. July 12, /29

Binary Trees Fall 2018 Margaret Reid-Miller

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

Figure 4.1: The evolution of a rooted tree.

Trees. Tree Structure Binary Tree Tree Traversals

Topic 14. The BinaryTree ADT

Introduction to Computers and Programming. Concept Question

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

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Binary Search Trees Treesort

Algorithms and Data Structures

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

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

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

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

CSI33 Data Structures

CMPT 225. Binary Search Trees

Trees. Eric McCreath

Successor/Predecessor Rules in Binary Trees

TREES Lecture 12 CS2110 Fall 2016

CMSC th Lecture: Graph Theory: Trees.

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

Data Structures and Organization (p.5 Recursion. Binary Trees)

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

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

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

tree nonlinear Examples

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

ITEC2620 Introduction to Data Structures

Data Structures and Algorithms for Engineers

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

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

Principles of Computer Science

Trees: examples (Family trees)

CSE 326: Data Structures Binary Search Trees

A Hierarchical Structure. Lecture11: Tree I. Tree Data Structures. Unix/Linux file systems. Bohyung Han CSE, POSTECH

INF2220: algorithms and data structures Series 1

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

CSI33 Data Structures

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

Trees Algorhyme by Radia Perlman

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

INF2220: algorithms and data structures Series 1

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

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

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

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

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

Binary Trees and Huffman Encoding Binary Search Trees

Trees, Binary Trees, and Binary Search Trees

Transcription:

OMP 210: Object-Oriented Programming Lecture Notes 8 Trees and Tree Traversals ased on notes by Logan Mayfield In these notes we look at inary Trees and how to traverse them. inary Trees Imagine a list. Now instead of every non-empty list having a single next list, what would happen if you allowed two nexts? What you d have is a binary tree like the one seen in figure 1. igure 1: inary Tree H reat. Let s define this kind of structure using a P. If we keep the natural base case of an empty tree then we get a simple extension of a list. <<interface>> intree MTinTree intreenode root : oo left : intree right : intree oo If we wanted, we could then redraw our example tree to show the empty trees as shown in figure 3. This new diagarm gives us a pretty good feel for the actual structure of a intree object where the previous diagram 1 highlighted the logical structure of the tree. 1 figure 1 It makes sense that the explosion of nexts leads to an explosion of empty trees. It s often convenient to work with another base case, the singleton tree. There s no reason we can t have two base cases, so let s add this variant of a binary tree to our previous design. The new P design given in figure 4 allows both empty an singleton base cases. It s not yet clear if this is helpful or just extra work, but just igure 2: inary Tree of oo Objects

comp 210 notes 8: trees and tree traversals 2 igure 3: inary Tree with mpty Trees shown H drawing simple diagrams shows this might be a nice capability to have as the tree given in figure 5 both covers the complete structure and minimizes the need for a large number of empty trees. <<interface>> intree MTinTree SingletonTree root : oo intreenode root : oo left : intree right : intree oo igure 4: inary Tree of oo Objects with Two ase ases igure 5: inary Tree with mpty Trees shown H t this point, it s worth imagining how the tree shown in figure 5 might be constructed in Java. or the sake of simplicity, we ll just assume the letters in each circle correspond to the name of some oo variable.

comp 210 notes 8: trees and tree traversals 3 intree atree = new intreenode(, new intreenode(, new SingletonTree(), new SingletonTree()), new intreenode(, new intreenode(, new SingletonTree(), new MTinTree()), new SingletonTree(H)) ); igure 6: The tree in figure 5 as a Java intree So we have some foothold into the world of trees now. We can define them as recursive class hierarchy and could easily start implementing functionality for that hierarchy so that we could play around with inary Trees. efore we do that we really should first look at trees in a much broader context and find something interesting to do with trees. inary Tree Traversal Traversing a list is fairly straight forward. rom the recursive perspective you either recurse on the rest then deal with the first or your deal with the first then recurse on the rest. If we were computing the size of a list that might look like the snippets we see in figure 7. // first, then rest return 1 + this.rst.size(); igure 7: Recursive List Traversal for size // rest, then first return this.rst.size() + 1; To generalize these patterns we talk about what we do for the first as visiting the current node of the list. little analysis shows that visiting then recursing will visit nodes in first to last order while recurse then visit will visit in last to first. With trees we don t just have one recursively defined field but two: the left and right. What are we to do? The answer is simple, pick a permutation of visit (v), go left (l), and go right (r). There are six such permutations: vlr, lvr, lrv, vrl, rvl, and rlv. To simplify matters we only consider permutations where l comes before r. This leaves us with the three depth first traversal patterns. 1. Preorder Traversal: v l r 2. Inorder Traversal: l v r 3. Postorder Traversal: l r v The names are derived from the placement of the visit within the

comp 210 notes 8: trees and tree traversals 4 permutation. In figure 8 we see how each of these patterns could be used to find the size 2 in a tree. 2 number of non-empty nodes igure 8: Recursive inary Tree Traversal for size // preorder return 1 + this.left.size() + this.right.size(); // inorder return this.left.size() + 1 + this.right.size(); //postorder return this.left.size() + this.right.size() + 1; If you wanted to carry these processes out using an iterative loop then you d need the assistance of a data structure that you studied in OMP220 3. We ll save that task for another time. Right now it s more important to understand the order in which we ll visit a tree s nodes when doing each traversal. or our example tree from figure 5 we d do the visit orders shown in table 1. 3 a stack Pattern Preorder (vlr) Visit Order H Table 1: Traversal orders for the tree in figure 5 Inorder (lvr) H Postorder (lrv) H oing these traversals by hand is wonderful practice for thinking through recursive a process. Just like some problems with lists necessitate a last to first traversal and some first to last, there will be tree problems that require different orders for visiting. Talking Trees Trees have been studied in mathematics for a long time and have found use in computing for as long as there has been computing. This means there is a lot of terminology to go along with trees. Much of it pulls from one of two metaphors, family trees or trees in nature. Tree Structure Node non-empty tree structure containing a single element and a left and right subtree. dge The connection between a Node and one of its subtrees. Path linearly connected set of nodes In our example tree, there is a path from the node containing to the node containing through the nodes and.

comp 210 notes 8: trees and tree traversals 5 Root Node The Root node of a tree is the first node in that tree. is the root of the tree shown in figure 5. It s left subtree is rooted by the node containing and its right subtree is rooted by the node containing. hild Node The root node of a node s subtree. The node containing is a child of the node containing. hildren of children and further down are called descendents. The node containing is a descendent of the node containing but the node containing is not a descendent of the node containing. Parent Node The node of which a given node is a child. The node containing is the parent of the node containing. Notice the root of a tree has no parent. Parents of parents and so on up the tree are called ancestors. The node containing is an ancestor of the node containing but not of the node containing. Leaf Node node with no children These nodes are the bottom most layer of a tree. They re what we call the singleton tree and an alternate base case to the empty tree. The nodes containing,,, and H are all leaf nodes. ll other nodes are internal nodes because they have at least one child. Properties of Trees and Nodes Node epth The number of edges from a given node to the tree s root node. The root of a tree always has a depth of 0 and from there depth increases by one with each level. Node Height The number of edges on the longest path from a give node to a leaf node. Tree Height The height of the tree s root node. This property is the tree analog of list length. Tree Size The number of nodes in the tree. Our example tree has a size of 8. lasses of inary Trees (Height) alanced inary Tree tree in which the height of the left subtree and the height of the right subtree differ by at most one. Our example tree given in figure 5 is height balanced because the left subtree has a height of 1 and the right subtree has a height of 2. omplete inary Tree tree of height h where the tree is full up to depth h 1 and then at depth h the leaves fill in from left to right. omplete trees are height balanced.

comp 210 notes 8: trees and tree traversals 6 ull inary Tree tree where every node except leaf nodes has exactly two children. // ull trees a special sub-class of omplete trees where every node at depth h 1 has two children. We ve seen a height balanced tree already. igure 9 shows you a complete tree of height 2 and figure 10 shows you a full tree of height 2. igure 9: omplete inary Tree of height 2 igure 10: ull inary Tree of height 2 References