Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

Huffman code and ID3 Prof. Sin-Min Lee Department of Computer Science.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Trees Chapter 8.
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.
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Is ASCII the only way? For computers to do anything (besides sit on a desk and collect dust) they need two things: 1. PROGRAMS 2. DATA A program is a.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Lecture 06: Tree Structures Topics: Trees in general Binary Search Trees Application: Huffman Coding Other types of Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Searching Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Searching Trees Reading: Kolman, Section 7.3.
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Labeled Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Labeled Trees Reading: Kolman, Section 7.2.
Lecture 17 Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Huffman Coding. Huffman codes can be used to compress information –Like WinZip – although WinZip doesn’t use the Huffman algorithm –JPEGs do use Huffman.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Discrete Structures Trees (Ch. 11)
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
CSCE350 Algorithms and Data Structure Lecture 19 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Huffman Codes Juan A. Rodriguez CS 326 5/13/2003.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Discrete Mathematics Chapter 5 Trees.
CS 103 Discrete Structures Lecture 23 Trees (1). Second Midterm Exam 1 st Lecture in December (same time as the lecture) 75 minute duration Will cover.
Foundation of Computing Systems
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Lecture 19 Minimal Spanning Trees CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Huffman Codes ASCII is a fixed length 7 bit code that uses the same number of bits to define each character regardless of how frequently it occurs. Huffman.
HUFFMAN CODES.
Recursive Objects (Part 4)
Chapter 5 : Trees.
Section 8.1 Trees.
Chapter 8 – Binary Search Tree
Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths.
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Presentation transcript:

Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

CSCI 1900 Lecture Lecture Introduction Reading –Rosen Section 11.3 Tree traversal –Preorder –Inorder –Postorder Encoding –Huffman encoding

CSCI 1900 Lecture Tree Traversal Trees can represent the organization of items Trees can represent a decision hierarchy Trees can also represent a process –With each vertex specifying a task –for-loop example for i = 1 thru 3 by 1 for j = 1 thru 5 by 1 array[i,j] = 10*i + j next j next i

CSCI 1900 Lecture For Loop Positional Tree i = 1 i = 2 i = 3 j=1 j=2 j=3 j=4 j=5

CSCI 1900 Lecture Terminology Traverse a tree –Visit each vertex in a specific order Visit a vertex –Performing a task at a vertex Do a computation Take a decision Kolman uses the term “search” to mean traverse –Search implies looking for a specific vertex –This is not necessarily the case

CSCI 1900 Lecture Tree Traversal Applications using trees, traverse the tree in a methodic way –To ensure visiting every vertex, exactly once We will explore three methodic ways Example: Assume we want to compute –The average age, maximum age, and minimum age of all of the children from five families Tree on next slide

CSCI 1900 Lecture Traversal Example Neighborhood families A. Jones HallsSmithTaylorB. Jones Katy age 3 Tommy age 5 Phil age 8 Taylor age 1 Lori age 4 Lexi age 2 Karen age 14 Bart age 12 Mike age 6 Ben age 2

CSCI 1900 Lecture Traversal Example (cont) To ensure that we don’t miss a child –Need a systematic way to visit each vertex –To calculate the average, max, and min ages for all of the children By defining a systematic process –Not only can a human be sure not to miss a vertex –But also can serve as the basis for developing a computer algorithm

CSCI 1900 Lecture One Such Systematic Process 1.Starting at the root, repeatedly take the leftmost “untraveled” edge until you arrive at a leaf 2.Include each vertex in the average, max, and min calculations 3.One at a time, go back up the edges until you reach a vertex that hasn’t had all of its outgoing edges traveled 4.Traverse the leftmost “untraveled” edge 5.If you get back to the root and there are no untraveled edges, you are done

CSCI 1900 Lecture Vertices Numbered in Order of Visits Neighborhood families A. JonesHallsSmithTaylorB. Jones Katy age 3 Tommy age 5 Phil age 8 Taylor age 1 Lori age 4 Lexi age 2 Karen age 14 Bart age 12 Mike age 6 Ben age

CSCI 1900 Lecture Preorder Traversal The name for this systematic way of traversing a tree is preorder traversal

CSCI 1900 Lecture Preorder Traversal Algorithm A preorder traversal of a binary tree consists of the following three steps: 1.Visit the root 2.Traverse the left subtree if it exists 3.Traverse the right subtree if it exists The term “traverse” in steps 2 and 3 implies that we apply all three steps to the subtree beginning with step 1

CSCI 1900 Lecture Inorder Traversal Algorithm An inorder traversal of a binary tree has the following three steps: 1.Traverse the left subtree if it exists 2.Visit the root 3.Traverse the right subtree if it exists

CSCI 1900 Lecture Postorder Traversal Algorithm A postorder traversal of a binary tree has the following three steps: 1.Traverse the left subtree if it exists 2.Traverse the right subtree if it exists 3.Visit the root

CSCI 1900 Lecture Example: Preorder Traversal A B C DFJLG IK E H Resulting string: A B C D E F G H I J K L

CSCI 1900 Lecture Example: Inorder Traversal A B C DFJLG IK E H Resulting string: D C B F E G A I J H K L

CSCI 1900 Lecture Example: Postorder Traversal A B C DFJLG IK E H Resulting string: D C F G E B J I L K H A

CSCI 1900 Lecture Encoding Encode – translate a string into a series of 1’s and 0’s ASCII – fixed length code –Code length = 7 bits Each character is represented by 7 bits Why 7 and not 8 bits? –Encode 128 distinct characters 95 printable characters 33 nonprintable characters

CSCI 1900 Lecture Encoding (cont) Suppose instead of a fixed length code, we use a variable length –Why? To reduce the size of the encoding Message compression –Assign the short codes to the more frequently used characters in message The amount of compression achieved is a function of –The method used to generate the codes –Relative frequency count of the characters in the message

CSCI 1900 Lecture Huffman Encoding One way of generating the codes –Huffman encoding Huffman encoding can be represented as a tree Details of generating the code are beyond the scope of this course We will examine the use of a generated code

CSCI 1900 Lecture Huffman Tree Example ET A I NS

CSCI 1900 Lecture Using Huffman Trees To convert a code to a string –Begin at the root of the tree –Take the branch indicated by the bit –Look at next bit and take the indicated branch –Until you reach a leaf; this gives the first character –Return to the root and repeat until done What string is represented by – – –

CSCI 1900 Lecture Using Huffman Trees (cont) Generate the code for a string –Begin by traversing the tree once, creating a set of pairs –Use the set of pairs to look up the code for each character What is the encoding for –NEST –STAT

CSCI 1900 Lecture Key Concepts Summary Tree traversal –Preorder –Inorder –Postorder Encoding –Huffman encoding Reading for next time –Kolman Section 7.5