U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 

Slides:



Advertisements
Similar presentations
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 Introduction to Binary Trees. 2 Background All data structures examined so far are linear data structures. Each element in a linear data structure has.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
1 Trees Definition of a Tree Tree Terminology Importance of Trees Implementation of Trees Binary Trees –Definition –Full and Complete Binary Trees –Implementation.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Graph Traversals Depth-First Traversal. The Algorithm.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
1 Binary Tree Traversals Binary Tree Traversal classification Tree traversal animations DepthFirst traversal abstraction Accept method of AbstractTree.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Marc Smith and Jim Ten Eyck
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
Course: Programming II - Abstract Data Types Slide Number 1 The ADT Binary Tree Definition The ADT Binary Tree is a finite set of nodes which is either.
Trees CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Saturday, 04 Apr 2010 University of Palestine Computer Science II Trees.
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.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
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.
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.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
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.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
The Tree ADT.
Trees Chapter 15.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Csc 2720 Instructor: Zhuojun Duan
Trees What is a Tree? Tree terminology Why trees?
Binary Trees, Binary Search Trees
CS212: Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
Trees.
CS210- Lecture 9 June 20, 2005 Announcements
B.Ramamurthy Chapter 9 CSE116A,B
Binary Trees, Binary Search Trees
Presentation transcript:

U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 

U n i v e r s i t y o f H a i l 2 Outline 1.Basics 2.N-ary Trees 3.Binary Trees 4.Tree Traversals 5.Expression Trees 6.Implementing Trees Tree Traversals Tree Enumerations General Trees N-ary Trees Binary Trees Binary Tree Traversals Comparing Trees Applications

U n i v e r s i t y o f H a i l Lec 1

U n i v e r s i t y o f H a i l 4 1. Basics  A tree is anon-linear data structure  A tree is often used to represent a hierarchy Clerk President Vice president division A Manager dept. A1 Clerk Manager dept. A2Manager dept. B1 Vice president division B

U n i v e r s i t y o f H a i l 5 1. Basics Definition 1 A tree is a finite, non-empty set of nodes, with the following properties 1. A chosen node of the set, r, is called the root of the tree; 2. The remaining nodes are subsets, T 1, T 2, …, T n, each of each is a tree.

U n i v e r s i t y o f H a i l 6 1. Basics A C B D G E FMJH IKL

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 7 1. Basics Terminology: Consider a tree The degree of a node is the number of subtrees associated with that node A node of degree zero has no subtrees. Such a node is called a leaf. Each root r i of subtree T i of tree T is called a child of r. The root node r of tree T is the parent of all the roots r i of the subtrees T i, 1 < i  n. The term grandparent. Two roots r i and r j of distinct subtrees T i and T j of tree T are called siblings(brothers).

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 8 1. Basics Definition 2 (Path and Path Length) -Given a tree T containing the set of nodes R, a path in T is defined as a non-empty sequence of nodes - The length of path P is k-1.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 9 1. Basics D G E FMJH IKL

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Basics D G E FMJH IKL F E D JM I H G KL

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Basics class D { class E { class F { } class G { class H { class I { } class J { class K { } class L { } class M { } D G E FMJH IKL

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath N-ary Trees Definition 3 (N-ary Tree) An N-ary tree T is a finite set of nodes with the following properties: -Either the set is empty, T =  ; or -The set consists of a root, R, and exactly N distinct N-ary trees. That is, the remaining nodes are partitioned into N  0 subsets, T 0, T 1, …, T N-1, each of which is an N-ary tree such that T = {R, T 0, T 1, …, T N-1 }.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Basics A A Tertiary tree ( N = 3 )

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Basics C B C B

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath N-ary Trees The empty trees are called external nodes (they have no subtrees and they appear at the extremities of the tree (squares)). The non-empty trees are called internal nodes (circles). A tree can be ordered (if its subtrees are ordered) or oriented if the order of the subtrees doesn’t matter.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath N-ary Trees Theorem 1 An N -ary tree with n  0 internal nodes contains (N-1)n + 1 external Theorem 2 Consider an N -ary tree of height h  0. The maximum number of internal nodes in T is given by: The height of a node r i in a tree is the length of the longest path from node r i to a leaf.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath N-ary Trees Theorem 3 Consider an N -ary tree of height h  0. The maximum number of leaf nodes T is

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Binary Trees Definition 4 (Binary Tree) A binary tree T is a finite set of nodes with the following properties: -Either the set is empty, T =  ; or -The set consists of a root, r, and exactly two distinct binary trees T L, and T R, T = {r, T L, T R }.  T L is called the left subtree of T, and the tree T R is called the right subtree of T.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Binary Trees  Binary trees are almost considered to be ordered trees (the above two binary trees are different). B A B A

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals  There are many different algorithms for manipulating trees.  A most common characteristic of these algorithms their visit of all nodes in the tree (called a tree traversal).  There are essentially two different traversal ways: depth-first traversal and breadth-first traversal.  Some depth-first traversals can be preorder, inorder, or, postorder traversals.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals D G B CE FI H A depth = 0 depth = 1 depth = 2 depth = 3

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Preorder Traversal  The first depth-first traversal method is called preorder traversal.  Preorder traversal is defined as follows 1.Visit the root first, and then 2.Do a preorder traversal each of the subtrees of the root one-by-one in the order given.  In the case of a binary tree, the algorithm becomes 1.Visit the root first, and then 2.Traverse the left subtree, and then 3.Traverse the right subtree

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Preorder traversal A, B, C, D, E, F, G, H, I D G B CE FI H A depth = 0 depth = 1 depth = 2 depth = 3

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Postorder Traversal  The second depth-first traversal method is called postorder traversal.  Postorder traversal is defined as follows 1.Do a postorder traversal each of the subtrees of the root one-by-one in the order given, and then 2.Visit the root.  In the case of a binary tree, the algorithm becomes 1.Traverse the left subtree, and then 2.Traverse the right, and then 3.Visit the root.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Postorder traversal C, B, F, G, E, I, H, D, A D G B CE FI H A depth = 0 depth = 1 depth = 2 depth = 3

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Inorder Traversal  The third depth-first traversal method is called inorder traversal (available only for binary trees).  Inorder traversal is defined as follows 1.Traverse the left subtree, and then 2.Visit the root, and then 3.Traverse the right subtree.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Inorder Traversal B, C, A, F, E, G, D, I, H D G B CE FI H A depth = 0 depth = 1 depth = 2 depth = 3

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Breadth-First Traversal  The Breadth-first traversals are non-recursive traversals and better understood.  A Breadth-first traversal visits the nodes of a tree in the order of their depth.  It visits first nodes at depth zero, then all nodes at depth one, and so on.  At each depth, the nodes are visited from left to right.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Tree Traversals Inorder Traversal A, B, D, C, E, H, F, G, I D G B CE FI H A depth = 0 depth = 1 depth = 2 depth = 3

U n i v e r s i t y o f H a i l Lec 2

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Expression Trees a  c  b- d e + - Leaves = variables (a, b, …) - Non-terminal nodes = operators (+, -, ,  ) - The non-terminal nodes have either one or two non-empty subtrees (binary or unary operator).

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Expression Trees a  c  b- d e + - Inorder traversal of the expression tree: a, , b, +, c, -, d, , e - Except the missing parentheses, the order in which the symbols appear is the same as in the expression.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Expression Trees  To apply an inorder traversal and take into account the level of operators, we process as following:  When the inorder traversal encounters(found) a terminal node (variable or constant), prints it out  When it encounters a non-terminal node, it does the following: Print a left parenthesis, and then Traverse the left subtree, and then Print the root, and then Traverse the right subtree, and then Print a right parenthesis

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Expression Trees a  c  b- d e +

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 35 Comparable Container Tree AbstractObject AbstractContainer GeneralTree NaryTree BinaryTree AbstractTree 6. Implementing Trees

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees public interface Tree extends Container { Object getKey (); // returns the object contained in the root of the tree Tree getSubtree (int i); // returns the ith subtree of the tree boolean isEmpty (); // returns true if the root is empty boolean isLeaf (); // returns true if the root of the tree is a leaf int getDegree (); // returns the degree of the root int getHeight (); // returns the height of the tree void depthFirstTraversal (PrePostVisitor visitor); void breadthFirstTraversal (Visitor visitor); } // all the nodes are systematically visited either in depth-first or breadth-first traversal

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Tree traversal (depth-first) public abstract class AbstractTree extends AbstractContainer implements Tree { public void depthFirstTraversal (PrePostVisitor visitor) { if (visitor.isDone ()) return; if (!isEmpty ()) { visitor.preVisit (getKey ()); // visit the root for (int i = 0; i < getDegree (); ++i) // visit the nodes under the root getSubtree (i).depthFirstTraversal (visitor); // recursive visitor.postVisit (getKey ()); // visit subtree }

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 38 public interface PrePostVisitor { void preVisit (Object object); void inVisit (Object object); void postVisit (Object object); boolean isDone (); } 6. Implementing Trees: Tree traversal (depth-first)

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 39 public abstract class AbstractPrePostVisitor implements PrePostVisitor { public void preVisit (Object object) {} public void inVisit (Object object) {} public void postVisit (Object object) {} public boolean isDone () { return false; } 6. Implementing Trees: Tree traversal (depth-first)

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 40 public class PreOrder extends AbstractPrePostVisitor { // using the visitor interface protected Visitor visitor; public PreOrder (Visitor visitor) { this.visitor = visitor; } public void preVisit (Object object) { visitor.visit (object); } public boolean isDone () { return visitor.isDone (); } 6. Implementing Trees: Tree traversal (depth-first)

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 41 public class InOrder extends AbstractPrePostVisitor { protected Visitor visitor; public InOrder (Visitor visitor) { this.visitor = visitor; } public void inVisit (Object object) { visitor.visit (object); } public boolean isDone () { return visitor.isDone (); } 6. Implementing Trees: Tree traversal (depth-first)

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 42 public class PostOrder extends AbstractPrePostVisitor { protected Visitor visitor; public PostOrder (Visitor visitor) { this.visitor = visitor; } public void postVisit (Object object) { visitor.visit (object); } public boolean isDone () { return visitor.isDone (); } 6. Implementing Trees: Tree traversal (depth-first)

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 43 Visistor v = new PrintingVisitor (); Tree t = new SomeTree (); // … t.depthFirstTraversal (new PreOrder (v)); t.depthFirstTraversal (new InOrder (v)); t.depthFirstTraversal (new PostOrder (v)); 6. Implementing Trees: Tree traversal (depth-first)

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 44 public abstract class AbstractTree extends AbstractContainer implements Tree { public void breadthFirstTraversal (Visitor visitor) { Queue queue = new QueueAsLinkedList (); if (!isEmpty ()) queue.enqueue (this); // the root node is enqueued firstly while (!queue.isEmpty () && !visitor.isDone ()) { Tree head = (Tree) queue.dequeue (); // remove the head visitor.visit (head.getKey ()); // visit the object in head for (int i = 0; i < head.getDegree (); ++i) { Tree child = head.getSubtree (i); if (!child.isEmpty ()) queue.enqueue (child); // enqueue in order each // non-empty subtree } 6. Implementing Trees: Tree traversal (Breadth-first)

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath 45 D G B CE FI H A 6. Implementing Trees: Binary Trees

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Binary Trees ACBDHE  FGI 

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Binary Trees public class BinaryTree extends AbstractTree { protected Object key; protected BinaryTree left; protected BinaryTree right; //... }

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Binary Trees public class BinaryTree extends AbstractTree { protected Object key; protected BinaryTree left; protected BinaryTree right; public BinaryTree (Object key, BinaryTree left, BinaryTree right) { this.key = key; this.left = left; this.right = right; } public BinaryTree () { this (null, null, null); } public BinaryTree (Object key) { this (key, new BinaryTree (), new BinaryTree ()); }

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Binary Trees public class BinaryTree extends AbstractTree { protected Object key; protected BinaryTree left; protected BinaryTree right; public void purge () { key = null; left = null; right = null; } //... }

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Binary Trees public class BinaryTree extends AbstractTree { protected Object key; protected BinaryTree left; protected BinaryTree right; public void depthFirstTraversal (PrePostVisitor visitor) { if (!isEmpty ()) { visitor.preVisit (key); left.depthFirstTraversal (visitor); visitor.inVisit (key); right.depthFirstTraversal (visitor); visitor.postVisit (key); }

U n i v e r s i t y o f H a i l // A // B C // D E F G Implementing tree Example D CB EFG A

U n i v e r s i t y o f H a i l import ADTs.*; public class testTree { public static void main(String[] args) { BinaryTree D = new BinaryTree(new Chr('D')); BinaryTree E = new BinaryTree(new Chr('E')); BinaryTree B = new BinaryTree(new Chr('B'),D,E); BinaryTree F = new BinaryTree(new Chr('F')); BinaryTree G = new BinaryTree(new Chr('G')); BinaryTree C = new BinaryTree(new Chr('C'),F,G); BinaryTree A = (BinaryTree) new BinaryTree(new Chr('A'),B,C); Visitor v = new ChrPrintingVisitor(); System.out.println("*************PreOrder Traversal***************"); A.depthFirstTraversal(new PreOrder(v)); System.out.println("***************InOrder Traversal**************"); A.depthFirstTraversal(new InOrder(v)); System.out.println("****************PostOrder Traversal*************"); A.depthFirstTraversal(new PostOrder(v)); System.out.println("****************Breadth Traversal***************"); A.breadthFirstTraversal(v); }

U n i v e r s i t y o f H a i l *************PreOrder Traversal*************** A B D E C F G ***************InOrder Traversal************** D B E A F C G ****************PostOrder Traversal************* D E B F G C A ****************Breadth Traversal*************** (A B C D E F G )

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Applications  In chapter 6, we sow how a stack can be used to compute the value of a postfix expression such as:  We are interested in this section in constructing the corresponding expression tree.  Once the tree is constructed, we can print it in infix or prefix notation.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Applications  The symbols in the postfix expression are processed from left to right as follow: If the next symbol in the expression is an operand, a tree composed of a single node labeled with that operand is pushed onto the stack. If the next symbol in the expression is a binary operator, the top two trees in the stack correspond to its operands. Two trees are popped from the stack and a new tree is created that has the operator as its root and the two trees corresponding to the operands as its subtrees. Then the new tree is pushed onto the stack.

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Applications

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Applications public class ExpressionTree extends BinaryTree { public ExpressionTree (char c) { super (new Chr (c)); } public static ExpressionTree parsePostfix (Reader in) throws IOException { Stack stack = new StackAsLinkedList (); int i; while ((i = in.read ()) >= 0) // reads one character at a time from the keyboard { char c = (char) i; if (Character.isLetterOrDigit (c)) stack.push (new ExpressionTree (c)); else if (c == '+' || c == '-' || c == '*' || c =='/') { ExpressionTree result = new ExpressionTree (c); result.attachRight((ExpressionTree) stack.pop()); result.attachLeft ((ExpressionTree) stack.pop()); stack.push (result); } return (ExpressionTree) stack.pop (); }

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Applications public class ExpressionTree extends BinaryTree { public String toString () { final StringBuffer buffer = new StringBuffer (); PrePostVisitor visitor = new AbstractPrePostVisitor () { public void preVisit (Object object) { buffer.append ("("); } public void inVisit (Object object) { buffer.append (object); } public void postVisit (Object object) { buffer.append (")"); } }; depthFirstTraversal (visitor); return buffer.toString (); }

U n i v e r s i t y o f H a i l ICS 202   Data Structures and Algorithms  Dr. Youssef Harrath Implementing Trees: Applications Given the input ab/cd-e*+. The program constructs the expression tree and then forms the infix expression: (((a)/(b))+((c)-(d))*(e))).