Download presentation
Presentation is loading. Please wait.
Published byMolly White Modified over 9 years ago
1
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
2
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
3
U n i v e r s i t y o f H a i l Lec 1
4
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
5
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.
6
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
7
U n i v e r s i t y o f H a i l ICS 202 2007-2 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).
8
U n i v e r s i t y o f H a i l ICS 202 2007-2 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.
9
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 9 1. Basics D G E FMJH IKL
10
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 10 1. Basics D G E FMJH IKL F E D JM I H G KL
11
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 11 1. 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
12
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 12 2. 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 }.
13
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 13 1. Basics A A Tertiary tree ( N = 3 )
14
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 14 1. Basics C B C B
15
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 15 2. 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.
16
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 16 2. 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.
17
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 17 2. N-ary Trees Theorem 3 Consider an N -ary tree of height h 0. The maximum number of leaf nodes T is
18
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 18 3. 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.
19
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 19 3. Binary Trees Binary trees are almost considered to be ordered trees (the above two binary trees are different). B A B A
20
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 20 4. 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.
21
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 21 4. Tree Traversals D G B CE FI H A depth = 0 depth = 1 depth = 2 depth = 3
22
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 22 4. 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
23
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 23 4. 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
24
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 24 4. 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.
25
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 25 4. 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
26
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 26 4. 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.
27
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 27 4. 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
28
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 28 4. 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.
29
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 29 4. 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
30
U n i v e r s i t y o f H a i l Lec 2
31
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 31 5. 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).
32
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 32 5. 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.
33
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 33 5. 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
34
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 34 5. Expression Trees a c b- d e +
35
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 35 Comparable Container Tree AbstractObject AbstractContainer GeneralTree NaryTree BinaryTree AbstractTree 6. Implementing Trees
36
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 36 6. 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
37
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 37 6. 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 }
38
U n i v e r s i t y o f H a i l ICS 202 2007-2 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)
39
U n i v e r s i t y o f H a i l ICS 202 2007-2 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)
40
U n i v e r s i t y o f H a i l ICS 202 2007-2 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)
41
U n i v e r s i t y o f H a i l ICS 202 2007-2 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)
42
U n i v e r s i t y o f H a i l ICS 202 2007-2 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)
43
U n i v e r s i t y o f H a i l ICS 202 2007-2 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)
44
U n i v e r s i t y o f H a i l ICS 202 2007-2 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)
45
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 45 D G B CE FI H A 6. Implementing Trees: Binary Trees
46
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 46 6. Implementing Trees: Binary Trees ACBDHE FGI
47
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 47 6. Implementing Trees: Binary Trees public class BinaryTree extends AbstractTree { protected Object key; protected BinaryTree left; protected BinaryTree right; //... }
48
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 48 6. 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 ()); }
49
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 49 6. 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; } //... }
50
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 50 6. 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); }
51
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
52
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); }
53
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 )
54
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 54 6. 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.
55
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 55 6. 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.
56
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 56 6. Implementing Trees: Applications
57
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 57 6. 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 (); }
58
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 58 6. 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 (); }
59
U n i v e r s i t y o f H a i l ICS 202 2007-2 Data Structures and Algorithms Dr. Youssef Harrath 59 6. 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))).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.