COMP 103 Introduction to Trees.

Slides:



Advertisements
Similar presentations
Introduction to Trees Chapter 6 Objectives
Advertisements

Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Fundamentals of Python: From First Programs Through Data Structures
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Marc Smith and Jim Ten Eyck
Binary Trees Chapter 6.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
COSC2007 Data Structures II
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees. Introduction to Trees Trees are very common in computer science They come in different forms They are used as data representation in many applications.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
CS261 Data Structures Trees Introduction and Applications.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
2014-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures TREES.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
The Tree ADT.
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
COMP 103 Introduction to Trees Thomas Kuehne 2013-T2 Lecture 19
Lecture 18. Basics and types of Trees
COMP 103 Binary Search Trees.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees.
CSC 143 Java Trees.
Binary Trees.
Presentation transcript:

COMP 103 Introduction to Trees

RECAP-TODAY RECAP TODAY Linked List, Linked Stack, Linked Queue Other linked collections TODAY Introduction to Trees Reading: Chapter 16 in textbook

Other linked collections LinkedStack and LinkedQueue All the key operations are O(1) Set Can store sorted or unsorted What is the cost of add, remove, contains, size, equals in each case? Bag Same as set, but add a count to each node. Map Same as set, but store value as well as key in each node Or store pointed to a key-value pair What is the cost of get, put, remove?

Linear vs. Hierarchical Structures So far we’ve looked at unordered structures and linear structures: Unordered structures have no structural order between items: Set, Bag, Map Linear structures have items arranged in order one after another List, Stack, Queue… Some data have a hierarchical or ‘tree-like’ structure Hierarchical structure have items arranged in levels, with items ‘above’ or ‘below’ the other, such as Family tree Organisational Charts Taxonomies Filing systems (paper-based, computer-based) Models of shapes for computer graphics Programs/decision processes…. And so on….

Trees We're going to look into the data structure and the crucial algorithms for trees Applications of tree structures: Representing data with a natural hierarchical structure Using trees as data structures to implement other collections: sets, maps and priority queues (and another sorting algorithm!) Note: some data has an even more complex network-like structure: Communications networks Geographical maps (real world, or in computer games)

Family Trees: ancestral tree Justine Jeremy Julie John Julia Jesse Jordan Jenna Jack Jacob Jules James Jada Jenny Jackie Jasmine Jean seniority

Organisational hierarchy Jane Jeremy Julie Justin John Jules Jenny Jesse Jordan Jacky Jenna Juan Jacob Julia James Jada Jared Jake Joseph Jasmine seniority

Taxonomy Animals Reptiles Mammals Primates Felines Canines Turtles Snakes Lizards Tiger Leopard Lion Cat

Program Structure private void reportMachineQueues(int tick) { System.out.printf("%4d: ", tick); for (Queue<Job> queue : machineQueues) { if (queue.isEmpty()) System.out.print(" - "); else System.out.printf("%3s ", queue.peek().getID()); } System.out.println(); if (verbose) for (int m=0; m<NumberOfMachines; m++) { Queue<Job> queue = machineQueues.get(m); System.out.printf(" M%d: %d jobs: ", m, queue.size()); boolean first = true; for (Job job : queue) { if (first) { System.out.print(job); first=false; else System.out.printf("\n %s", job);

Tracing program execution archWall(10, 300, 80, 40) aw(50,220,40,20) drawArch aw(10, 220, 40, 20) drawArch aw(10,180,20,10) aw(30,180,20,10) drawArch aw(50,180,20,10) aw(70,180,20,10) drawArch drawArch drawArch drawArch

Noughts and Crosses (Tic Tac Toe) X X X X X O X O X X X X O O O

Decision Tree

Tree Terminology A tree is a collection of items in a strict hierarchical structure. Each item is in a node of the tree. The root is at the top (!) The leaves are at the bottom. Each node may have child nodes – except a leaf, which has none. Each node has one parent - except the root, which has none. An edge joins a node to its parent – may be labelled. A subtree is a node plus all its descendents. The depth of a node is its distance from the root. The height or depth of a tree is the depth of the lowest leaf. Level = set/list of nodes at the same depth. Branch = sequence of nodes on a path from root to a leaf. This is more correctly called a rooted, directed, labelled tree. Children may be ordered/unordered Unique!!

Components of Trees A C B E F G J K M Node, root, leaf, child, parent, edge, subtree, depth, height, level, branch, siblings, ancestors, descendants, cousins, …

Common Constraints on Trees Number of children per node (“arity”) 2: Binary tree 3: Ternary tree N: N-ary tree (5-ary, 100-ary, …) Strict N-ary: every non-leaf has exactly N children; otherwise at most N General Tree: No limit on number of children Ordering constraints: Children of a node ordered (List of children). Children of a node unordered (Set/Bag of children). Item in each node is less than items in child nodes. Item at each node is smaller that all items in its left subtree and greater than all items in its right subtree. Balancing: Subtrees are the same height (all leaves are on same level) Subtrees heights differ by at most one (leaves within one level of each other) What is a unary tree?

What can you do with a tree? Construct/modify/print it: Add nodes, remove nodes (where?) Parsing: read a string and turn it into a tree, eg. program, expression Search it: Find a node with a given value/property Find a path to (or from) a node Compute some property of the tree or its data: Find the height, width, arity, … Sum, max, average, … of values stored Find a given person’s mother, or all of their grandchildren Find the manager with the most (direct/indirect) underlings

Traversing trees Many tree algorithms involve doing something at each node in a tree Different algorithms require nodes to be visited in different orders Breadth-first/level-order: Visit nodes in order of increasing depth Root, then all nodes with depth 1, all with depth 2, … May be in specified order within each level Depth-first: Visit one child and all of its descendants before its siblings May visit root before, after or between its children May visit children in specified order

Trees and Recursion Recursively defined data structure: A tree is a node, along with a set/list of subtrees (empty for leaf) Can a tree be empty? Recursion is very natural for trees – important! If you don’t use recursion you often need to simulate it (later)

Binary Trees Each node may have a left child and/or a right child Notice that tree size is limited by the depth of the tree. For binary tree, nodes at level k = n, for N-ary tree, k = n ^ (k-1). Size for binary is: 1 + 2 + … + n = 2^n – 1 and size for N-ary is (n^k-1)/(n-1) where k is number of full levels. What’s the maximum number of nodes in a binary tree of height k ? What’s the minimim height of a binary tree with n nodes?

Representing tree data in Java What kind of data structure can we use to represent a tree? Linked Structures, just like Linked List Nodes: Binary Tree Nodes: General Tree Nodes Arrays ?! we'll come to this later on... G T K M Z A G M K G K M