Searching Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Searching Trees Reading: Kolman, Section 7.3.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Binary Trees. Node structure Data A data field and two pointers, left and right.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
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.
Lecture 18 Tree Traversal CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
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.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
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.
Labeled Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Labeled Trees Reading: Kolman, Section 7.2.
Copyright © Cengage Learning. All rights reserved. CHAPTER 10 GRAPHS AND TREES.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Tree Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
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 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Discrete Mathematics Chapter 10 Trees.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
Paul Tymann and Andrew Watkins
Week 6 - Wednesday CS221.
Trees Another Abstract Data Type (ADT)
Section 8.1 Trees.
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Section 9.3 by Andrew Watkins
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Searching Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Searching Trees Reading: Kolman, Section 7.3

Searching Trees – Page 2CSCI 1900 – Discrete Structures Tree Searching Trees can represent the organization of elements, but they can also represent a process with each vertex specifying a task. –For-loop example for i = 1 to 3 for j = 1 to 5 array[i,j] = 10*i + j next j next i –Mathematical expression from section 7.2 – each vertex represents a computation that combines its offspring.

Searching Trees – Page 3CSCI 1900 – Discrete Structures Terminology “Visiting” a vertex – the act of performing a task at a vertex, e.g., perform a computation or make a decision. “Searching” the tree – the process of visiting each vertex in a specific order or path. The term “searching” can be misleading. Just think of it as “traversing” the tree.

Searching Trees – Page 4CSCI 1900 – Discrete Structures Tree Search The application of some trees involves traversing the tree in a methodical pattern so as to address every vertex. Our book uses the term “search”, but sometimes search can imply we’re looking for a particular vertex. This is not the case. Example: Assume we want to compute the average age, maximum age, and minimum age of all of the children from five families. (Tree is on next slide.)

Searching Trees – Page 5CSCI 1900 – Discrete Structures Search Example 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 2

Searching Trees – Page 6CSCI 1900 – Discrete Structures Search Example (continued) To calculate the average, max, and min ages for all of the children, we need to have a method for going through the tree so that we don’t miss a child. By defining a rigorous process, not only can a human be sure not to miss a vertex, but also an algorithm can be defined for a computer

Searching Trees – Page 7CSCI 1900 – Discrete Structures A Suggested Process for Searching a Tree 1.Starting at the root, repeatedly take the leftmost “untraveled” edge until you arrive at a leaf which should be a child. 2.Include this child 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.If you get back to the root and cannot find an untraveled edge, you are done. Otherwise, return to step 1.

Searching Trees – Page 8CSCI 1900 – Discrete Structures 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

Searching Trees – Page 9CSCI 1900 – Discrete Structures Preorder Search This methodical pattern of traversing a tree is called a preorder search. Assume v is the root of a binary positional tree T. –Each vertex of this tree has at most a left vertex, v L, and a right vertex, v R. –If either v L or v R have offspring, then they are subtrees of T, and a search can be performed of them too. –By viewing a tree this way, then the search method we described in earlier slides can be performed using a recursive algorithm applied to each vertex. –The recursive algorithm is repeatedly applied until every leaf has been reached.

Searching Trees – Page 10CSCI 1900 – Discrete Structures Preorder Search Algorithm A preorder search of a tree has the following three steps: 1.Visit the root 2.Search the left subtree if it exists 3.Search the right subtree if it exists The term “search” in steps 2 and 3 implies that we apply all three steps to the subtree beginning with step 1.

Searching Trees – Page 11CSCI 1900 – Discrete Structures Vertices Visited in Alphabetical Order Using Preorder Search A B C DFJLG IK E H

Searching Trees – Page 12CSCI 1900 – Discrete Structures Prefix or Polish Form x – a de c ÷ b + Preorder search produces: × – a b + c ÷ d e Binary tree representing: ( a – b ) × ( c + ( d ÷ e))

Searching Trees – Page 13CSCI 1900 – Discrete Structures Polish Form (continued) Allows us to write complex arithmetic expressions without using parenthesis Expression is evaluated by performing following steps: –Move left to right until you find a string of the form Fxy, where F is the symbol for a binary operation and x and y are numbers. –Evaluate x F y and substitute answer for string Fxy. –Repeat starting at beginning of string again.

Searching Trees – Page 14CSCI 1900 – Discrete Structures Polish Form Example 1.× – ÷ 2 21 st pattern: – × ÷ 2 22 nd pattern: ÷ × rd pattern: × 2 64 th pattern: × (6 – 4) × (5 + (2 ÷ 2)) = 12

Searching Trees – Page 15CSCI 1900 – Discrete Structures Inorder and Postorder Searches Preorder search gets its name from the fact that the operator that joins two items is evaluated first, e.g., the binary operation 6 – 4 is visited in the order – 6 4. Inorder search evaluates the expression as it is written, e.g., the binary operation 6 – 4 is visited in the order 6 – 4. Postorder search evaluates the operator after the elements are read, e.g., the binary operation 6 – 4 is visited in the order 6 4 –.

Searching Trees – Page 16CSCI 1900 – Discrete Structures Inorder Search Algorithm An inorder search of a tree has the following three steps: 1.Search the left subtree if it exists 2.Visit the root 3.Search the right subtree if it exists

Searching Trees – Page 17CSCI 1900 – Discrete Structures Postorder Search Algorithm A postorder search of a tree has the following three steps: 1.Search the left subtree if it exists 2.Search the right subtree if it exists 3.Visit the root

Searching Trees – Page 18CSCI 1900 – Discrete Structures Evaluation of Tree Using Inorder Search A B C DFJLG IK E H Resulting string: DCBFEGAIJHKL

Searching Trees – Page 19CSCI 1900 – Discrete Structures Evaluation of Tree Using Postorder Search A B C DFJLG IK E H Resulting string: DCFGEBJILKHA

Searching Trees – Page 20CSCI 1900 – Discrete Structures Infix Form x – a de c ÷ b + Inorder search produces: a – b × c + d ÷ e Unfortunately, without parenthesis, we can’t do anything with this expression. Binary tree representing: ( a – b ) × ( c + ( d ÷ e))

Searching Trees – Page 21CSCI 1900 – Discrete Structures Postfix or Reverse Polish Form x – a de c ÷ b + Inorder search produces: a b – c d e ÷ + × Binary tree representing: ( a – b ) × ( c + ( d ÷ e))

Searching Trees – Page 22CSCI 1900 – Discrete Structures Reverse Polish Form (continued) Allows us to write complex arithmetic expressions without using parenthesis Expression is evaluated by performing following steps: –Move left to right until you find a string of the form xyF, where F is the symbol for a binary operation and x and y are numbers. –Evaluate x F y and substitute answer for string xyF. –Repeat starting at beginning of string again.

Searching Trees – Page 23CSCI 1900 – Discrete Structures Reverse Polish Form Example From left-to-right evaluate xyF first – ÷ + ×1 st pattern: 2 1 – ÷ + × 2 nd pattern: 4 2 ÷ × 3 rd pattern: ×4 th pattern: 1 5 × 5.5 (2 – 1) × (3 + (4 ÷ 2)) = 5

Searching Trees – Page 24CSCI 1900 – Discrete Structures Converting an Ordered n-tree to a Positional Binary Tree An ordered n-tree where some vertices have more than two offspring can be converted to a positional binary tree. This allows easier computer representation with methods such as linked lists. A process exists for this conversion that works on any finite tree.

Searching Trees – Page 25CSCI 1900 – Discrete Structures Process to Convert Ordered n-tree to Positional Binary Tree Starting at the root, the first or leftmost offspring of a vertex remains the leftmost vertex in the binary tree The first sibling to the right of the leftmost vertex becomes the right offspring of the leftmost vertex Subsequent siblings become the right offspring in succession until last sibling is converted. A BCDE A B C D E

Searching Trees – Page 26CSCI 1900 – Discrete Structures Conversion Example A B C D E G F HI JKL

Searching Trees – Page 27CSCI 1900 – Discrete Structures Conversion Example A B C D E G F HI JKL A B C D E GF H I J K L Preorder search: Left tree – ABEFCGJKLHID Right tree – ABEFCGJKLHID