Binary Tree Chapter 8 (cont’) Part2.

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

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
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.
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.
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
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.
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.
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.
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.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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 Saurav Karmakar
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Lecture No.13 Data Structures Dr. Sohail Aslam
Data Structures Binary Trees 1.
Paul Tymann and Andrew Watkins
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 20: Binary Trees.
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
CS212: Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Paul Tymann, Andrew Watkins,
Binary Tree Traversal Methods
Binary Tree Traversals
Section 9.3 by Andrew Watkins
Binary Trees, Binary Search Trees
Paul Tymann, Andrew Watkins,
Chapter 20: Binary Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Binary Tree Chapter 8 (cont’) Part2

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.

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.

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

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: V- visiting a node. L- traversing the left subtree. R- traversing the right subtree.

Depth- First Traversal 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. VLR – preorder tree traversal LVR – inorder tree traversal 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.

Depth- First Traversal Given a binary tree having a root, left subtree(LST), right sub tree(RST): 1 2 3 2 3 1 3 1 2 L R L R L R Preorder traversal NLR 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 1 4 15 16 20 25

Inorder (another example) CBDAEF A E B C D F C B D A E F

Preorder and Postorder traversal 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 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 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) ABCDEF A E B C D F A B C D E F

Postorder (another example) CDBFEA A E B C D F C D B F E A

End Of Chapter References: Text book, chapter 8: Binary Trees