Class 12
Class 12
search
- Ed. 2. and 3.: Chapter 6
- Ed. 4.: Chapter 10
b
2
4
8
5
9
10
6
11
12
7
13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
14
15
2p(u)+1=5
4
8
p(u)=4
5
9
10
4
2p(u)+1=9
2p(u)=8 8
11
5
10
7
11
14
15
5
10
2 3
7
11
4 5
14
10 11
15
14 15
left
right
element
providence
root
Chicago
5
size
Baltimore
Baltimore
Chicago
New York
Providence
Seattle
Seattle
New York
Class BTNode
public class BTNode implements Position {
private Object element;
private BTNode left, right, parent;
public BTNode() {}
public BTNode( Object o, BTNode u, BTNode v, BTNode w ) {
setElement( o );
setParent( u );
setLeft( v ):
setRight( w );
}
public Object element() { return element; }
public void setElement( Object o ) { element = o; }
public BTNode getLeft() { return left; }
public void setLeft( BTNode v ) { left = v; }
public BTNode getRight() { return right; }
public void setRight( BTNode v ) { right = v; }
public BTNode getParent() { return parent; }
public void setParent( BTNode v ) { parent = v; }
}
BTNnode
element(){};
getLeft(){};
getRight(){};
setLeft(){};
setRight(){};
getParent(){};
setElement(){};
Class LinkedBinaryTree
Additional methods are supported.
expandExternal(v): Transform the external node v into an internal node
by creating two new external nodes and making them
the left and right children of v, respectively; an error
condition occurs if v is an internal node.
Input: Position; Output: None
v
v
v
z
z
w
p=paren(v)
lc=leftChild(p)
v
rightChild(p)
v
v
p
s
s
v
IspectableContainer
size
isElement
Elements
IspectablePositionContainer
positions
PositionContainer
swapElement
replaceElement
Tree
InspectableTree
root, parent, children, isRoot
isInternal, isExternal
InspectableBinaryTree
leftChild, rightChild, sibling
BinaryTree
imple.
LinkedBinaryTree
, replaceElement, swapElement, expandExternal,
removeAboveExternal
parent
element
childrenContainer
Example:
New York
Baltimore
Chicago
Providence
Seattle
Preorder Traversal
r
u
v
Title Abstract
1.1
Ch. 1
1.2
Ch. 2
2.1
2.2
Ch. 3
2.3
3.1
3.2
References
Algorithm preorder(T,v):
perform the visit action for node v
for each child w of v
call preorder(T,w)
v
postorder(T,w)
postorder(T,v)
Example:
What is the string that preorderPrint(T,v) returns?
Sales
Domestic
Canada
International
S. America
Overseas
traversal
breadth-first traversal
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);}
c
f
a; b, c, d.
b; e, f.
e;
f;
c; g.
g;
d;
a
b
a
d
b
e
f
c
g
d
stack S:
node_info Pointer_to_parent
f;
c; g.
g;
d;
XML File
<book>
<title>
The Art of Programming
</title>
<author>
D. Knuth
</author>
<year>
1969
</year>
</book>
<book>
<title>
<author>
<year>
1969
XML File
Read a file into a character array A:
< b o o k > < t i t l e > T h e
stack S:
node_value Pointer_to_node
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();