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

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

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

Binary Trees

BBM 201 Data structures

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

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

Programming II (CS300)

Binary Trees, Binary Search Trees

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

Trees. Tree Structure Binary Tree Tree Traversals

Trees. CSE 373 Data Structures

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

CS24 Week 8 Lecture 1

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.

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

Trees. Truong Tuan Anh CSE-HCMUT

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

CMPT 225. Binary Search Trees

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

CE 221 Data Structures and Algorithms

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

Binary Search Trees 1

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

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

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

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

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

March 20/2003 Jayakanth Srinivasan,

CSC148 Week 6. Larry Zhang

Topic 14. The BinaryTree ADT

Binary Trees Fall 2018 Margaret Reid-Miller

Chapter 20: Binary Trees

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


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

COSC 2011 Section N. Trees: Terminology and Basic Properties

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

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

tree nonlinear Examples

Recursion, Binary Trees, and Heaps February 18

Sample Questions for Midterm Exam 2

CS 206 Introduction to Computer Science II

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

Binary trees. Binary trees. Binary trees

Principles of Computer Science

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

Carlos Delgado Kloos Mª Carmen Fernández Panadero Raquel M. Crespo García Dep. Ingeniería Telemática Univ. Carlos III de Madrid

Data Structures and Algorithms

Programming II (CS300)

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

Why Do We Need Trees?

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

EE 368. Weeks 5 (Notes)

CSI33 Data Structures

TREES. Trees - Introduction

Successor/Predecessor Rules in Binary Trees

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

Trees, Binary Trees, and Binary Search Trees

LECTURE 11 TREE TRAVERSALS

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

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

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

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Data Structures and Algorithms

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

CSI33 Data Structures

INF2220: algorithms and data structures Series 1

CSE 214 Computer Science II Introduction to Tree

Algorithms and Data Structures

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

Tree traversals and binary trees

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

Data Structures Question Bank Multiple Choice

INF2220: algorithms and data structures Series 1

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Trees Algorhyme by Radia Perlman

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

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

Computer Science for Engineers

TREES Lecture 10 CS2110 Spring2014

Introduction to Computers and Programming. Concept Question

Trees: examples (Family trees)

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

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

Trees. Eric McCreath

CmpSci 187: Programming with Data Structures Spring 2015

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

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

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

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

Binary Trees. Height 1

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

Trees. Trees. CSE 2011 Winter 2007

Chapter 5. Binary Trees

Tree Travsersals and BST Iterators

CSCI2100B Data Structures Trees

Binary Search Trees Treesort

Transcription:

Topic 17 Introduction ti to Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb Definitions A A tree is an abstract t data t d internal type nodes one entry point, the root Each node is either a leaf or an internal node An internal node has 1 or more children, nodes that can be reached directly from that internal node. The internal node is said to be leaf nodes the parent of its child nodes root node 1 2 Properties of Trees Only access point is the root All nodes, except the root, have one parent like the inheritance hierarchy in Java Traditionally trees drawn upside down root Properties of Trees and Nodes siblings: two nodes that have the same parent edge: the link from one node to another path length: the number of edges that must be traversed to get from one node to another edge root siblings path length from root to this node is 3 leaves 3 4

More Properties of Trees depth: d the path length from the root of the tree to this node height h i ht of a node: The maximum distance (path length) of any leaf from this node a leaf has a height of 0 the height of a tree is the height of the root of that tree descendants: any nodes that can be reached via 1 or more edges from this node ancestors: any nodes for which this node is a descendant B Tree Visualization C A E F G H I J D K L M N O 5 6 Attendance Question 1 What is the depth of the node that contains M on the previous slide? A. -1 B. 0 C. 1 D. 2 E. 3 Binary Trees There are many variations on trees but we will work with binary trees binary tree: a tree with at most two children for each node the possible children are normally referred to as the left and right child parent left child right child 7 8

Full Binary Tree full binary tree: a binary tree is which each node was exactly 2 or 0 children Complete Binary Tree complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible 9 Where would the next node go to maintain a complete tree? 10 Perfect Binary Tree perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. a perfect binary tree has the maximum number of nodes for a given height a a perfect ec binary tree has 2 (n+1) -1 nodes where n is the height of a tree height = 0 -> 1 node height = 1 -> 3 nodes height = 2 -> 7 nodes height = 3 -> 15 nodes 11 A Binary Node class public class BNode { private Object mydata; private BNode myleft; private BNode myright; } public BNode(); public BNode(Object data, BNode left, BNode right) public Object getdata() public BNode getleft() public BNode getright() public void setdata(object data) public void setleft(bnode left) public void setright(bnode right) 12

Binary Tree Traversals Many algorithms require all nodes of a binary tree be visited and the contents of each node processed. There are 4 traditional types of traversals preorder traversal: process the root, then process all sub trees (left to right) in order traversal: process the left sub tree, process the root, process the right sub tree post order traversal: process the left sub tree, process the right sub tree, then process the root level order traversal: starting from the root of a tree, process all nodes at the same depth from left to right, then proceed to the nodes at the next depth. 13 Results of Traversals To determine the results of a traversal on a given tree draw a path around the tree. start on the left side of the root and trace around the tree. The path should stay close to the tree. 13 12 pre order: process when pass down left side of node 12 49 13 5 42 49 42 in order: process when pass underneath node 13 49 5 12 42 5 post order: process when pass up right side of node 13 5 49 42 12 14 Tree Traversals A C D F G H J K L Attendance Question 2 What is a the result of a post order traversal of the tree on the previous slide? A. F C G A K H L D J B. F GC K L H JD A C. A C F G D H K L J D. ACDFGHJKL E. L K J H G F D C A 15 16

Implement Traversals Implement preorder, inorder, and post order traversal Big O time and space? Implement p a level order traversal using a queue Big O time and space? Implement a level order traversal without a queue target depth Different kinds of Iterators for traversals? 17