1 / 47

Tree representation and tree search

Tree representation and tree search. Ed. 2. and 3.: Chapter 6 Ed. 4.: Chapter 10. Data Structures for Representing Trees. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15. providence. Chicago. Seattle. Baltimore. New York. Class BTNode. Interface Hierarchy for Positions. Position

Download Presentation

Tree representation and tree search

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. Tree representation and tree search • Ed. 2. and 3.: Chapter 6 • Ed. 4.: Chapter 10

  2. Data Structures for Representing Trees

  3. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

  4. providence Chicago Seattle Baltimore New York

  5. Class BTNode

  6. Interface Hierarchy for Positions Position element(); DNode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; BTNnode element(){…}; getLeft(){…}; getRight(){…}; setLeft(){…}; setRight(){…}; getParent(){…}; setElement(){…};

  7. Class LinkedBinaryTree

  8. public class ArrayPositionIterator implements Iterator { protected Position a[]; // the underlying array protected Position cur; int i = 0; // the current (next) position public ArrayPositionIterator() { } // default constructor public ArrayPositionIterator(Position[] L) { // preferred constructor a = L; if (a[0] == null) cur = null; // array is empty else cur = a[i]; // start with the first position } public boolean hasNext() { return (cur != null); } public Position next() throws NoSuchElementException { if (!hasNext()) throw new NoSuchElementException("No next position"); Position toReturn = cur; if (cur == a[i+1]) cur = null; // no positions left else cur = a[i+1]; i++; // move cursor to the next position return toReturn; } }

  9. protected void inorderPosition (Position v, Positions[] pos, int i) throws InvalidPositionException { if (hasLeft(v)) inorderPosition(left(v), pos, i); // recurse on left child pos[i] := v; i := i + 1; //if (((BTPosition)v).element() instanceof Integer) // System.out.print(((Integer)((BTPosition)v).element()).intValue() + " "); //else System.out.print(((Character)((BTPosition)v).element()).charValue() + " "); if (hasRight(v)) inorderPosition(right(v), pos, i); // recurse on right child }

  10. IspectableContainer size isElement Elements IspectablePositionContainer positions InspectableTree root, parent, children, isRoot isInternal, isExternal PositionContainer swapElement replaceElement InspectableBinaryTree leftChild, rightChild, sibling Tree BinaryTree imple. LinkedBinaryTree … …, replaceElement, swapElement, expandExternal, removeAboveExternal

  11. A Linked Structure for General Trees

  12. Data Structure Exercises 12.1

  13. Preorder Traversal

  14. Algorithm preorder(T,v): perform the “visit” action for node v for each child w of v call preorder(T,w) postorder(T,v) v postorder(T,w) w

  15. Preorder Traversal of a Binary Tree

  16. Preorder Traversal Using Stack preorder traversal S.push(root); While (S is not empty) do{ x := S.pop( ); access x; let x1, …, xk be the children of x; for i = k to 1 do {S.push(xi);} } Q.enqueue(root); While (Q is not empty) do{ x := Q.dequeue( ); access x; let x1, …, xk be the children of x; for i = 1 to k do {Q.enqueue(xi);} } breadth-first traversal

  17. a c g b d e f Load a tree from disk into main memory File: a; b, c, d. b; e, f. e; f; c; g. g; d;

  18. a b e f c g d a c g b d e f public class Node1 { String x; Node2 y; } public class Node2 { Node1 x; Node2 y; }

  19. Access 0th in the file to find the root of the tree; S.push(root, null); while (S is not empty) do{ x := S.pop( ); generate a node n for x.node_info; if x.point_to_parent is not null then generate links between n and x.pointer_to_parent; Access the corresponding line in the file to find the children of x; let x1, …, xk be the children of x; forj = k to 1 do S.push(xj, n); } a; b, c, d. b; e, f. e; f; c; g. g; d; stack S: Pointer_to_parent node_info

  20. (*Assume that the nodes are stored in preorder in the file.*) i := 0; Access 0th line in the file to find the root of the tree; S.push(root, null); while (S is not empty) do{ x := S.pop( ); generate a node n for x.node_id; if x.point_to_parent is not null then generate links between n and x.pointer_to_parent; Access the ithline in the file to find the children of x; let x1, …, xk be the children of x; for j = k to 1 do S.push(xj, n); i := i + 1; }

  21. <book> <title> <author> <year> “The Art of Programming” “D. Knuth” “1969” XML File <book> <title> “The Art of Programming” </title> <author> “D. Knuth” </author> <year> “1969” </year> </book>

  22. stack S: Pointer_to_node node_value Read a file into a character array A: < b o o k > < t i t l e > “ T h e A r t … XML File

  23. XML File Algorithm: Scan array A; If A[i] is ‘<’ and A[i+1] is a character then { generate a node x for A[i.e.], where A[j] is ‘>’ directly after A[i]; let y = S.top().pointer_to_node; make x be a child of y; S.push(A[i..j], x); If A[i] is ‘ ‘‘ ’, then { genearte a node x for A[i.e.], where A[j] is ‘ ’’ ’ directly after A[i]; let y = S.top().pointer_to_node; make x be a child of y; If A[i] is ‘<’ and A[i+1] is ‘/’, then S.pop();

More Related