# Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10.

## Presentation on theme: "Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10."— Presentation transcript:

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 ChicagoSeattle BaltimoreNew York

Class BTNode

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 PositionContainer swapElement replaceElement InspectableTree root, parent, children, isRoot isInternal, isExternal Tree InspectableBinaryTree leftChild, rightChild, sibling BinaryTree LinkedBinaryTree … …, replaceElement, swapElement, expandExternal, removeAboveExternal imple.

A Linked Structure for General Trees

Data Structure Exercises 12.1

Preorder Traversal

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

Preorder Traversal of a Binary Tree

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

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

a c g bd ef abefcgd public class Node1 {String x; Node2 y; } public class Node2 {Node1 x; Node2 y; }

Access 0 th 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 x 1, …, x k be the children of x; for j = k to 1 do S.push(x j, n); } node_info Pointer_to_parent a; b, c, d. b; e, f. e; f; c; g. g; d; stack S:

(*Assume that the nodes are stored in preorder in the file.*) i := 0; Access 0 th 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 i th line in the file to find the children of x; let x 1, …, x k be the children of x; for j = k to 1 do S.push(x j, n); i := i + 1; }

XML File “The Art of Programming” “D. Knuth” “1969” “The Art of Programming” “D. Knuth”“1969”

XML File node_value Pointer_to_node stack S: Read a file into a character array A : “ T h e A r t …

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();

Download ppt "Tree representation and tree search - Ed. 2. and 3.: Chapter 6 - Ed. 4.: Chapter 10."

Similar presentations