Drawing Tree wijanarto.

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.
CS 171: Introduction to Computer Science II
Binary Tree Terminology Linear versus hierarchical data Tree – connected graph with no cycles Child Parent Descendant Sibling Ancestor Leaf vs. internal.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Binary Tree B G E D I H F c A Binary tree
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS 240: Data Structures Monday, July 30 th Binary Search Trees.
Binary Search Trees Chapter 7 Objectives
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
Lecture 17 Non-Linear data structures Richard Gesick.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Compiled by: Dr. Mohammad Omar Alhawarat
Tree Traversal.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
Binary Search Trees Data Structures Ananda Gunawardena
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
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.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
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.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Binary Search Trees Chapter 7 Objectives
CSCE 210 Data Structures and Algorithms
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
CS223 Advanced Data Structures and Algorithms
Abstract Data Structures
Binary Trees.
Trees Definitions Implementation Traversals K-ary Trees
Principles of Computing – UFCFA3-30-1
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees.
Non-Linear data structures
Binary Tree Implementation And Applications
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

Drawing Tree wijanarto

Any Tree Insert Leveling Traversal Preorder 1 3 2 3 Insert Leveling Traversal Preorder 1 18 4 6 11 21 5 6 11 9 19 24 4 16 Pre : 3,1,18,11,4,5,16,14,21,19,24,26 In : 1,3,4,5,11,14,16,18,19,21,24,26 Post :1,5,4,14,16,11,19,26,24,21,18,3 10 7 8 26 5 14 3,1,18,11,4,21,16,5,14,24,26,19 Value Sequences 1,2, 3, 4, 5, 6, 7, 8, 9,10, 11,12

BST Traversal Methods LDR =Left Data Right LRD =Left Right Data DLR =Data Left Right RDL =Righ Data Left RLD =Right Left Data DRL =Data Right Left

Traversal Tree

Drawing Any BTree There are any Btree.. 1 Pre Order : print left right 3 In Order : left print rigth 1 3 5 5 Post Order : left right print Print on every visited node 4 4 4 2 1 2 5 3 2 1 5 3

Reading Tree Pictures Pre Order = 30, 40, 15, 27, 45 In Order = 40, 15, 30, 27, 45 Post Order = 15, 40, 45, 27, 30 Level = 30, 40, 27, 15, 45 (Reading level, start from root, left, right, down to next level, left, right, and so forth.. )

Example 1 Given Tree In Order : 7, 4, 3, 12, 6, 1, 13 Question: Draw any binary tree in InOrder Write list in PreOrder?

Answer 1 Any BTree InOrder PreOrder from picture InOrder InOrder : LDR / Left Data right PreOrder : DLR / Data left right 4 12 1 1 7 4 7 3 13 12, 7, 3, 4, 13, 6, 1 1 2 3 4 5 6 7 2 3 5 7 3 4 3 6 5 6 2 6 3 6 4 1 1 1 4 4 4 1 7 7 4 7 Result notUNIque 7, 4, 3, 12, 6, 1, 13 1 2 3 4 5 6 7

Example 2 Ordered Given Tree In Order : 1, 2, 3, 4, 5, 6,7 Question: Draw any Binary Tree in InOrder Write list in PostOrder?

Answer 2 Any BTree InOrder PostOrder from InOrder InOrder : PreOrder : LDR / Left Data right PreOrder : LRD / left right Data 3 3 7 2 7 5 1, 2, 4, 7, 6, 5, 3 1 2 3 4 5 6 7 1 2 5 4 6 4 1 5 6 6 4 5 6 1 1 6 4 5 2 4 2 2 1 7 7 3 Result not unique 4 5 1, 2, 3, 4, 5, 6, 7 1 2 3 4 5 6 7 4 5 6 6

Solution Drawing InOrder Tree : InOrder PreOrder  Draw PostOrder PostOrder  Draw PreOrder

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them Preorder Inorder a b c d f g e c b f d g a e

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them Preorder inorder a b c d f g e c b f d g a e

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder a b c d f g e c b f d g a e

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder Inorder a b c d f g e c b f d g a e

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder a b c d f g e c b f d g a e

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder Inorder e a b c d f g e c b f d g a e b c d f g c b f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e b c d f g c b f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e b c d f g c b f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e b c d f g c b f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e c b c d f g c b f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e c b c d f g c b f d g d f g f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder Postorder b e a b c d f g e c b f d g a e c b c d f g c b f d g d f g f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e c b c d f g c b f d g d f g f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e c d b c d f g c b f d g d f g f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e c d b c d f g c b f d g d f g f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e c d b c d f g c b f d g d f g f d g

Drawing tree from pre and inorder Given list pre and inorder in binary tree, we can draw unique tree from them a Preorder inorder b e a b c d f g e c b f d g a e c d b c d f g c b f d g c d d f g f d g

Drawing tree from pre and inorder You can do with post and inrder In post , root always at the end of list

How about pre andpost Tree is not unique Pre : a b c Post : c b a a a

Special case If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal a preorder postorder a b c d f g e c f g d b e a

Special Case If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal a e preorder postorder a b c d f g e c f g d b e a

Special Case If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal a e preorder postorder a b c d f g e c f g d b e a Cause e in preorder no child Then e must be leaf and places at the right of a

Special Case If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal a e preorder postorder a b c d f g e c f g d b e a b c d f g c f g d b

Special Case If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal a b e preorder postorder a b c d f g e c f g d b e a b c d f g c f g d b

Special Case If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal a b e preorder postorder a b c d f g e c f g d b e a b c d f g c f g d b

Special Case a If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal b e d Cause d is right child of b, then in Preorder, node behind its right of b, and c must be left child of b. The rest we follow post method preorder postorder a b c d f g e c f g d b e a b c d f g c f g d b

Special Case a If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal b e c d Cause c on the post is leaf, and at the left of b Then we can draw it. preorder postorder a b c d f g e c f g d b e a b c d f g c f g d b

Special Case a If every internal node from binary tree there exist 2 child, then tree could determine from pre and post order traversal b e c d f g preorder postorder a b c d f g e c f g d b e a b c d f g c f g d b d f g f g d

Another example Given tree : In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 (index) 11 10 9 8 7 6 5 4 3 2 1 Root is 40, becaouse in Post Order the right most value is the root. How do we draw the picture In Order ?

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 1

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 2

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 3

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 4

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 5

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 6

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 7

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 8

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 9

In Order : 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 Post Order : 5, 15, 25, 20, 30, 10, 35, 50, 55, 45, 40 Step 10

What we’ve got so far ? To read and draw tree we need : In and Pre OR In and post TRAVERSAL LIST Thats all