Chapter 6 (cont’) Binary Tree. 6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does.

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

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
COP 3540 Data Structures with OOP
Tree.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Slow Insertion in an Ordered Array 1. Slow Searching in a Linked List 2.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
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
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
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.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Traversing Trees. Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree. Consider the three operations: V: Visit a node.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
1 COP 3540 Data Structures with OOP Chapter 8 - Part 1 Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Trees Chapter 15.
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
Paul Tymann and Andrew Watkins
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Paul Tymann, Andrew Watkins,
Binary Tree Traversal Methods
Section 9.3 by Andrew Watkins
Binary Tree Chapter 8 (cont’) Part2.
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
Binary Trees.
Presentation transcript:

Chapter 6 (cont’) Binary Tree

6.4 Tree Traversal Tree traversal is the process of visiting each node in the tree exactly one time. This definition does not specify the order in which the nodes are visited. There are three simple ways to traverse a tree. They’re called preorder, inorder, and postorder. The order most commonly used for binary search trees is inorder.

6.4.1 Breadth-first Traversal BFT is visiting each node starting from the highest(or lowest) level and moving down (or up) level by level, visiting nodes on each level from left to right(or from right to left). So, we will get 4 possibilities. This traverse an be done using queue: after a node is visited, its children, if any, are placed at the end of the queue, and the node at the beginning of the queue is visited. All nodes on level n must be visited before visiting any nodes on level n+1 is accomplished.

A top-down, left-to-right breadth first traversal of this tree is: 13, 10, 25, 2, 12, 20, 31, 29

6.4.2 Depth- First Traversal DFT proceeds as far as possible to the left(or right), then backs up until the first crossroad, goes one step to the right(or left), and again as far as possible to the left(or right). Repeat this process until all nodes are visited. There are some variations of the depth-first traversal. There are three tasks here: 1. V- visiting a node. 2. L- traversing the left subtree. 3. R- traversing the right subtree.

The number of different orders can be reduce to three traversals where the move is always from left to right and focus on the first column. 1. VLR – preorder tree traversal 2. LVR – inorder tree traversal 3. LRV – postorder tree traversal The real job is done by the system on the run-time stack (using double recursion) Remember that visiting a node means doing something to it.

M. ALSabhan & M. ALMusallam 7 Given a binary tree having a root, left subtree(LST), right sub tree(RST): Preorder traversal NLR LR LR LR Inorder traversal LNR Postorder traversal LRN Standard Traversals

Inorder traversal An inorder traversal of a binary search tree will cause all the nodes to be visited in ascending order, based on their key values. The simplest way to carry out a traversal is the use of recursion. A recursive function to traverse the tree is called with a node as an argument. Initially, this node is the root. The function must perform only three tasks. 1. Call itself to traverse the node’s left subtree. 2. Visit the node. 3. Call itself to traverse the node’s right subtree.

Inorder traversal So, the output of visiting this tree is

Inorder (another example) A B E CDF DC BA F E CBDAEF

C++ code Like any recursive function, there must be a base case: the condition that causes the routine to return immediately, without calling itself. void inOrder(Node* pLocalRoot) { if(pLocalRoot != 0) { inOrder(pLocalRoot->pLeftChild); //left child cout iData << “ “; //display node inOrder(pLocalRoot->pRightChild); //right child }

These traversals are useful with programs that analyze algebraic expressions. A binary tree (not a binary search tree) can be used to represent an algebraic expression that involves the binary arithmetic operators +, -, /, and *. The root node holds an operator, and the other nodes represent either a variable name (like A, B, or C), or another operator. Each subtree is an algebraic expression. For example, the algebraic expression A*(B+C) Besides writing different kinds of algebraic expressions, you might find other uses for the different kinds of traversals like using postorder traversal to delete all the nodes when the tree is destroyed. Preorder and Postorder traversal

Preorder Traversal 1- Visit the node. 2- Call itself to traverse the node's left subtree. 3- Call itself to traverse the node's right subtree. Postorder traversal 1. Call itself to traverse the node’s left subtree. 2. Call itself to traverse the node’s right subtree. 3. Visit the node.

Traversing the tree using preorder would generate the expression *A+BC. This is called prefix notation. Note : parentheses are not required. Traversing the tree using postorder would generate the expression ABC+*. This is called postfix notation..

Preorder (another example) A B E CDF DC BA F E ABCDEF

A B E CDF DC BA F E Postorder (another example) CDBFEA