7.3 Tree Searching.

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

CS 171: Introduction to Computer Science II
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Rooted Trees. More definitions parent of d child of c sibling of d ancestor of d descendants of g leaf internal vertex subtree root.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Transforming Infix to Postfix
Graph Theory TreesAlgorithms. Graphs: Basic Definitions 4 n Let n be the number of nodes (stations) and e be the number of edges (links). n A graph is.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Created on 29/10/2008yahaya.wordpress.com1 Trees Another common nonlinear data structure is the tree. We have already seen an example of a tree when we.
Searching Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Searching Trees Reading: Kolman, Section 7.3.
1 Section 1.4 Graphs and Trees A graph is set of objects called vertices or nodes where some pairs of objects may be connected by edges. (A directed graph.
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.
Lecture 8 Tree.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
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.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
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.
Rules to Remember When solving an equation, the goal is to get the variable by itself. Addition and Subtraction are inverse operations. (opposites) Multiplication.
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.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Stem and leaf diagrams. Example - drawing a stem and leaf Answer (a):- (1) key: 4|7 means 47 (2) put values into preliminary diagram: Question:- 29 students.
CSCE 210 Data Structures and Algorithms
7.2 Labeled trees.
Unit 1: Radical Expressions
Recursive Objects (Part 4)
Redraw these graphs so that none of the line intersect except at the vertices B C D E F G H.
Binary search tree. Removing a node
UNIT III TREES.
Paul Tymann and Andrew Watkins
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Tree.
Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Section 8.1 Trees.
Factoring Review AMT.
Red-Black Trees Bottom-Up Deletion.
Tonga Institute of Higher Education
Stem and leaf diagrams.
Binary Tree Applications
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
Binary Trees.
Trees 7/14/2009.
Red-Black Trees Bottom-Up Deletion.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Red-Black Trees Bottom-Up Deletion.
Pythagorean Theorem.
Binary Trees.
Lecture 7: Introduction to Parsing (Syntax Analysis)
Naming and Measuring Angles
Paul Tymann, Andrew Watkins,
Red Black Trees Top-Down Deletion.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Section 9.3 by Andrew Watkins
The Basic Building Blocks of Geometry
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Binary Tree Chapter 8 (cont’) Part2.
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
2-3 Trees Extended tree. Tree in which all empty subtrees are replaced by new nodes that are called external nodes. Original nodes are called internal.
Section 3.1 Inequalities – Solve and Graph Inequalities
8.2 Tree Traversals Chapter 8 - Trees.
Presentation transcript:

7.3 Tree Searching

As each vertex of the tree is encountered, you may want to perform a task. You may want to perform a calculation or display something. Performing tasks at a vertex is called visiting. The process of visiting each vertex in some order is called searching the tree or performing a tree search. If a root has a child to the left then it has a left subtree VL. If a root ahs a child to the right then it has a right subtree VR We will review 3 ways to search a tree: Pre order, In order and Post order.

Name Rules Example Pre Order Root, Left, Right 9, 4, 2, 3, 8, 6, 7 The name of the search indicates when the root is visited. (The root goes at the beginning. To start, go to the left side and apply the rule to the first vertex you come to.) (The root goes in the middle. To start, first find the lowest left most vertex and apply the rule.) (The root goes at the end. To start, on the left, find the lowest left most vertex and apply the rule. When done with the left, go to the right. To start the right side, find the first left most vertex and apply the rule.) Name Rules Example Pre Order Root, Left, Right 9, 4, 2, 3, 8, 6, 7 Name Rules Example In order Left, Root, Right 2, 4, 3, 9 ,6, 8, 7 Name Rules Example Post order Left, Right, Root 2, 3, 4, 6, 7, 8, 9

Break everything down to a subset and apply the rule. 9 LEFT SIDE RIGHT SIDE 8 4 7 3 6 2

Expression: (a-b)*(c+(d ÷ e)) Digraph: Applying pre order (root, left, right(root at the beginning. To start, go to the left side and apply the rule to the first vertex you come to)): *- a b + c ÷ d e This is called the Polish form. We have the operational symbol before the arguments. * - + c ÷ a b d e

For pre order search, if we replace the letters with numbers: * - a b + c ÷ d e a = 6, b = 4, c = 5, d = 2 e = 2 * - 6 4 +5 ÷ 2 2 We move from left to right. Look at the sign closest to the first pair of numbers: - 6 4 put the – between the 6 and 4: 6 – 4 6 – 4 = 2. Replace – 6 4 with 2 * 2 + 5 ÷ 2 2 The next pair of numbers with a sign in front of them is ÷ 2 2 2 ÷ 2 = 1 Replace ÷ 2 2 with 1 * 2 + 5 1 The next pair of numbers with a sign in front of them is + 5 1 5 + 1 = 6. Replace + 5 1 with 6. *26 2 * 6 = 12 The result is 12.

In order search produces: a – b * c + d ÷ e Replacing the variables with numbers a = 6, b = 4, c = 5, d = 2 e = 2 6 - * 5 + 2 ÷ 2 We don’t know where the parentheses go, so this method doesn’t work.

Post order (left, right, root ), Reverse Polish Root goes at the end. To start, first find the lowest left most vertex and apply the rule. To start the right side, find the first left most vertex and apply the rule.) It’s a little tricky on the right side. a b – c d e ÷ + * The operator is listed after the arguments. a = 2, b = 1, c = 3, d = 4, e = 2 2 1 – 3 4 2 ÷ + * Reading left to right, we first see 2 1 - 2-1 = 1 replace 2 1 – with 1 1 3 4 2 ÷ + * Next, we see 4 2 ÷ 4 ÷ 2 = 2 replace 4 2 ÷ with 2 1 3 2 + * Next we see 3 2 + 3 + 2 = 5 replace 3 2 + with 5 1 5 * 1 * 5 = 5 Final answer is 5

Labeled Binary Positional Tree: Digraph: To draw this as a Binary Positional tree first, draw the same left most edge. 1 2 4 3 5 6 7 8 10 9 11 12 13 1 2 5 11

Next we look at every parent at level 1 and draw a line from vertex 2 in this diagram for 3 and 4. 3 is the second offspring of the root and 4 is the third offspring of the root. 3 has offspring 8 that is a leaf. 4 has offspring 9 and 10 that are leaves. 1 2 5 3 11 4 1 2 5 3 11 8 4 9 10

5 is the first offspring of 2 6 is the second offspring of 2 7 is the third offspring of 2 1 2 3 1 5 8 4 2 6 9 11 3 7 10 8 5 4 9 6 The first offspring of 5 is 11 The second offspring of 5 is 12 The third offspring of 5 is 13 11 10 12 7 13