Download presentation
Presentation is loading. Please wait.
Published byRoss Bloise Modified over 9 years ago
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
8
providence ChicagoSeattle BaltimoreNew York
9
Class BTNode
10
Interface Hierarchy for Positions Position element(); DNode element(){…}; getNext(){…}; getPrev(){…}; setNext(){…}; setPrev(){…}; setElement(){…}; BTNnode element(){…}; getLeft(){…}; getRight(){…}; setLeft(){…}; setRight(){…}; getParent(){…}; setElement(){…};
11
Class LinkedBinaryTree
20
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; }
21
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 }
23
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.
24
A Linked Structure for General Trees
26
Data Structure Exercises 12.1
27
Preorder Traversal
30
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)
39
Preorder Traversal of a Binary Tree
40
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
41
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;
42
a c g bd ef abefcgd public class Node1 {String x; Node2 y; } public class Node2 {Node1 x; Node2 y; }
43
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:
44
(*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; }
45
XML File “The Art of Programming” “D. Knuth” “1969” “The Art of Programming” “D. Knuth”“1969”
46
XML File node_value Pointer_to_node stack S: Read a file into a character array A : “ T h e A r t …
47
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();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.