1 / 59

Chapter 6 Trees

Chapter 6 Trees. Outline. Basics N-ary Trees Binary Trees Tree Traversals Expression Trees Implementing Trees Tree Traversals Tree Enumerations General Trees N-ary Trees Binary Trees Binary Tree Traversals Comparing Trees Applications. L ec 1. President.

argus
Télécharger la présentation

Chapter 6 Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6Trees ICS 202  2011 spring  Data Structures and Algorithms 

  2. Outline • Basics • N-ary Trees • Binary Trees • Tree Traversals • Expression Trees • Implementing Trees • Tree Traversals • Tree Enumerations • General Trees • N-ary Trees • Binary Trees • Binary Tree Traversals • Comparing Trees • Applications

  3. Lec1

  4. President Vice president division A Vice president division B Manager dept. A1 Manager dept. A2 Manager dept. B1 Clerk Clerk Clerk Clerk Clerk Clerk 1. Basics • A tree is anon-linear data structure • A tree is often used to represent a hierarchy

  5. 1. Basics Definition 1 A tree is a finite, non-empty set of nodes, with the following properties A chosen node of the set, r, is called the root of the tree; The remaining nodes are subsets, T1, T2, …, Tn, each of each is a tree.

  6. 1. Basics D B G E A F H J M C I K L

  7. Consider a tree 1. Basics Terminology: • 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 ri of subtree Ti of tree T is called a child of r. • The root node rof tree T is the parent of all the roots riof the subtrees Ti, 1 < i n. The term grandparent. • Two roots ri and rjof distinct subtrees Ti and Tj of tree T are called siblings(brothers). ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  8. 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 1. Basics • The length of path P is k-1. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  9. 1. Basics D G E F H J M I K L ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  10. 1. Basics D D G E F H J M E G F H J M I K L I K L ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  11. D G E F H J M I K L 1. Basics class D { class E { class F { } } class G { class H { class I { } } class J { class K { } class L { } class M { } } } 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, T0, T1, …, TN-1, each of which is an N-ary tree such that T = {R, T0, T1, …, TN-1}. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  13. 1. Basics A A Tertiary tree (N = 3) ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  14. 1. Basics B C B C 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. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  16. Theorem 2 Consider an N-ary tree of height h  0. The maximum number of internal nodes in T is given by: 2. N-ary Trees Theorem 1 An N-ary tree with n 0 internal nodes contains (N-1)n + 1 external The height of a node ri in a tree is the length of the longest path from node ri to a leaf. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  17. Theorem 3 Consider an N-ary tree of height h  0. The maximum number of leaf nodes T is 2. N-ary Trees 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 TL, and TR, T = {r, TL, TR}. • TL is called the left subtree of T, and the tree TR is called the right subtree of T. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  19. 3. Binary Trees A A B B • Binary trees are almost considered to be ordered trees (the above two binary trees are different). 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. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  21. 4. Tree Traversals A depth = 0 B D depth = 1 C E H depth = 2 F G I depth = 3 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 • Visit the root first, and then • 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 • Visit the root first, and then • Traverse the left subtree, and then • Traverse the right subtree ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  23. A depth = 0 B D depth = 1 C E H depth = 2 F G I depth = 3 4. Tree Traversals Preorder traversal A, B, C, D, E, F, G, H, I 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 • Do a postorder traversal each of the subtrees of the root one-by-one in the order given, and then • Visit the root. • In the case of a binary tree, the algorithm becomes • Traverse the left subtree, and then • Traverse the right, and then • Visit the root. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  25. A depth = 0 B D depth = 1 C E H depth = 2 F G I depth = 3 4. Tree Traversals Postorder traversal C, B, F, G, E, I, H, D, A 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 • Traverse the left subtree, and then • Visit the root, and then • Traverse the right subtree. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  27. A depth = 0 B D depth = 1 C E H depth = 2 F G I depth = 3 4. Tree Traversals Inorder Traversal B, C, A, F, E, G, D, I, H 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. ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  29. A depth = 0 B D depth = 1 C E H depth = 2 F G I depth = 3 4. Tree Traversals Inorder Traversal A, B, D, C, E, H, F, G, I ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  30. Lec 2

  31. +   a b - e c d 5. Expression Trees • Leaves = variables (a, b, …) • Non-terminal nodes = operators (+, -, ,) • The non-terminal nodes have either one or two non-empty subtrees (binary or unary operator). ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  32. +   a b - e c d 5. Expression Trees • 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. 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 ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  34. +   a b - e c d 5. Expression Trees ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  35. 6. Implementing Trees AbstractObject AbstractContainer Comparable GeneralTree Container Tree NaryTree AbstractTree BinaryTree 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 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 } } } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  38. 6. Implementing Trees: Tree traversal (depth-first) public interface PrePostVisitor { void preVisit (Object object); void inVisit (Object object); void postVisit (Object object); boolean isDone (); } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  39. 6. Implementing Trees: Tree traversal (depth-first) 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; } } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  40. 6. Implementing Trees: Tree traversal (depth-first) 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 (); } } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  41. 6. Implementing Trees: Tree traversal (depth-first) 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 (); } } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  42. 6. Implementing Trees: Tree traversal (depth-first) public class PostOrder extends AbstractPrePostVisitor { protected Visitor visitor; public PostOrder (Visitor visitor) { this.visitor = visitor; } public void postVisit (Object object) { visitor.visit (object); } publicboolean isDone () { return visitor.isDone (); } } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  43. 6. Implementing Trees: Tree traversal (depth-first) Visistor v = new PrintingVisitor (); Tree t = new SomeTree (); // … t.depthFirstTraversal (new PreOrder (v)); t.depthFirstTraversal (new InOrder (v)); t.depthFirstTraversal (new PostOrder (v)); ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  44. 6. Implementing Trees: Tree traversal (Breadth-first) 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 enqueuedfirstly 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 } } } } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  45. 6. Implementing Trees: Binary Trees A B D C E H F G I ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

  46. A  C B D H E    G I       F 6. Implementing Trees: Binary Trees 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; // ... } 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 ()); } } 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; publicvoid purge () { key = null; left = null; right = null; } // ... } 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; publicvoid depthFirstTraversal (PrePostVisitor visitor) { if (!isEmpty ()) { visitor.preVisit (key); left.depthFirstTraversal (visitor); visitor.inVisit (key); right.depthFirstTraversal (visitor); visitor.postVisit (key); } } } ICS 202  2007-2  Data Structures and Algorithms  Dr. Youssef Harrath

More Related