Upload
avis-clarke
View
213
Download
1
Embed Size (px)
Citation preview
(as used in graph theory)
A tree (undirected) is ... an empty tree (i.e. zero nodes) or a node together with zero or more subtrees
G M DR W
H K Z
FB
A
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
A tree (undirected) is ... an empty tree (i.e. zero nodes) or a node together with zero or more subtrees
A rooted tree (sometimes called a directed tree) is ...a tree in which each node is connected by an arc from itself to any node in its subtree.
K
G M DR W
ZH
FB
A
X
The tree’s root is the only node with no incoming arcs.
Each arc defines a parent (arc origin) and a child (arc destination). The terms ancestor and descendant follow the parent/child directions.
A leaf is any node with no outgoing arcs.
The level of a node is its distance (number of arcs) from the root.
The height of a tree is the maximum level of all its nodes.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
A tree (undirected) is ... an empty tree (i.e. zero nodes) or a node together with zero or more subtrees
A rooted tree (sometimes called a directed tree) is ...a tree in which each node is connected by an arc from itself to any node in its subtree.
K
G
M
DR
W
ZH
FB
A
X
A binary tree is a rooted tree with two additional properties.
1) Each child is either a right child or left child. 2) A parent can have one left and/or one right child.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
«interface»BinaryTree
«query» + boolean isEmpty( ) + BinTreeIterator iteratorAtRoot( )«update» + void makeRoot(Object z) + void clear( )
«interface»BinTreeIterator
«query» + BinTreeIterator clonedIt( ) + Object item( ) + boolean isOff( ) + boolean isRoot( ) + boolean hasLeft( ) + boolean hasRight( ) + BinTreeIterator left( ) + BinTreeIterator right( )«update» + void setNodeContent(Object z) + void sproutLeft(Object z) + void sproutRight(Object z) + void pruneLeft( ) + void pruneRight( )
Sample Code(assuming theTree conforms to BinaryTree)
theTree.clear();theTree.makeRoot(“A”);BinTreeIterator theIt = theTree.iteratorAtRoot();theIt.sproutLeft(“T”);theIt.sproutRight(“R”);theIt.left().sproutLeft(“E”);theIt = theIt.right();theIt.sproutLeft(“E”);theTree.iteratorAtRoot().setNodeContent(“Z”);
EE
RT
Z
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
K
G
M
DR
W
ZH
FB
A
X
A tree traversal algorithm is a procedure for visiting every tree node.The most common tree traversal algorithms are considereddepth-first algorithms.
public void preOrder(BinTreeIterator it) { if (!it.isOff()) { visitNodeAt(it); preOrder( it.left() ); preOrder( it.right() ); }}
Visiting orderA, B, H, G, R, F, K, W, D, M, X, Z
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
K
G
M
DR
W
ZH
FB
A
X
public void inOrder(BinTreeIterator it) { if (!it.isOff()) { inOrder( it.left() ); visitNodeAt(it); inOrder( it.right() ); }}
Visiting orderG, R, H, B, A, K, M, D, X, W, F, Z
public void postOrder(BinTreeIterator it) { if (!it.isOff()) { postOrder( it.left() ); postOrder( it.right() ); visitNodeAt(it); }}
Visiting orderR, G, H, B, M, X, D, W, K, Z, F, A
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.