470 likes | 496 Views
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
E N D
Tree representation and tree search • Ed. 2. and 3.: Chapter 6 • Ed. 4.: Chapter 10
providence Chicago Seattle Baltimore New York
Interface Hierarchy for Positions Position element(); DNode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; BTNnode element(){…}; getLeft(){…}; getRight(){…}; setLeft(){…}; setRight(){…}; getParent(){…}; setElement(){…};
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; } }
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 }
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
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
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
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;
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; }
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
(*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; }
<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>
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
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();