Download Trees

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

Quadtree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Trees
- Ed. 2. and 3.: Chapter 6
- Ed. 4.: Chapter 7
Trees
• What is a tree?
- Examples of trees
- Tree interface and interface hierarchy
• Algorithms on binary trees
- Binary tree interface
- Traversal on a binary tree
- Binary tree implementation
- Traversal on a tree
• Sample case study application
Trees
Is it a sequence?
Family Felidae
(cats)
Order Carnivora
(carnivores)
Family Phocidae
(seals)
Subfamily
Acinonychinae
(cheetahs)
Subfamily Felinae
(small cats)
Order Chiroptera
(bats)
Family Ursidae
(bears)
Class Mammalia
...
...
Order Proboscidea
(elephants)
Subfamily
Pantherinae
(leopards, lions,
and tigers)
tree: A data structure that organizes data hierarchically.
hierarchical: With the exception of the top element, each
element in a tree has a parent element and zero or more
children elements.
root of the tree: The top element
Example:
Electronics R’ Us
R&D
Sales
Domestic
Purchasing
International
US
S. America
Manufacturing
TV
CD
Overseas
Tuner
A formal definition of trees:
A tree T is a set of nodes storing elements in a parent-child
relationship with the following properties:


T has a special node r, called the root of T.
Each node v of T different from r has a parent node u.
r
T
u
v
Note: By definition, a tree has at least one element: the root.
child: If node u is the parent of node v, then we say that v is a
child of u.
u
v
siblings: Two nodes that are children of the same parent
u
w
v
Nodes w and v are siblings.
external node (leaf): The node has no children.
r
T
u
v
Node v is an external node.
internal node: The node has one or more children.
Node u is an internal node.
ancestor: Either the node itself or an ancestor of the parent of
the node.
r
T
u
v
Node u and r are ancestors of node v.
descendent: A note v is a descendent of a node u if u is an
ancestor of v.
r
T
u
v
Node u and v are descendent of node r.
subtree rooted at a node v: The tree consisting of all the
descendents of v (including v itself).
r
T
u
u
v
A subtree rooted at node u
v
Example 6.1
The inheritance relation between classes in a Java program.
o class java.lang.Object
o
o
o
o
class java.lang.Boolean
class java.lang.Character
class java.lang.Math
class java.lang.Number
o
o
o
o
o
o
o
o
o
o
o
o
class java.lang.Byte
class java.lang.Double
class java.lang.Float
class java.lang.Integer
class java.lang.Long
class java.lang.Short
class java.lang.Process
class java.lang.Runtime
class java.lang.SecurityManager
class java.lang.String
class java.lang.System
class java.lang.Thread
Example 6.2
In operating systems, files are
organized hierarchically into
directories (folders).
ordered tree: There is a linear ordering defined for the children
of each node; that is, we can identify children of a node as the
first, second, third, and so on.
Example 6.3: The structure of a book is an ordered tree
Book
Ch. 1
1.1
...
Ch. 2
1.4
…
…
Ch. 9
9.1
... 9.6
References
Comparing Example 6.3 with Example 6.2, we can see an
important difference between the ordered tree and the unordered
tree.
In an ordered tree, the order of the children of a node is significant
while in an unordered tree, the order of the children of a node is
not important.
binary tree: A tree in which every node has at most two
children.
Example:
B
T
c
b
1
2
3
proper binary tree: Each node has either zero or two children.
Every internal node has exactly two children. These children are
labelled as a left child and a right child, respectively.
B
T
1
b
c
b
2
3
4
Left child 1
2
Right child
A proper binary tree. Also node b’s left child and right child.
A proper binary tree and the left subtree and right subtree of
node B.
B
T
c
b
1
2
3
4
b
Left subtree
1
c
2
3
4
Right subtree
From now on, all the binary trees in this course (and in the
text book) are proper binary trees by default.
Example 6.4: a decision tree
Are you nervous?
No
Yes
Saving account
Will you need to access most of the
money within the next 5 years?
No
Yes
Money market fund
Yes
Stock portfolio
Are you willing to accept risks in
exchange for higher expected returns
No
Diversified portfolio with stocks,
bonds, and short-term investments
Example 6.5: Math with binary operators
-
/
+
*
+
3
+
-
3
1
9
*
2
-
3
5
6
7
4
(((( 3 + 1 ) * 3 ) / (( 9 - 5 ) + 2 )) - (( 3 * ( 7 - 4 )) + 6 ))
Each node has a value associated with it.


If a node is external, then its value is that of its variable or constant.
If a node is internal, then its value is defined by applying its operation to the
values of its children.
Recall that in a list, a position is located by its two neighbours:
the previous and next positions.
Similarly, a position or node in a tree is located by its
neighbours: Its parent and its children, if any.
The position interface for a tree supports the method:
element(): Return the object at this position.
Input: None; Output: Object
The Tree Abstract Data Type
The tree abstract data type supports the following fundamental
accessor methods:
root(): Return the root of the tree.
Input: None; Output: Position
parent(v): Return the parent of node v; an error if v is root.
Input: Position; Output: Position
children(v): Return an iterator of the children of node v.
Input: Position; Output: Iterator of positions.
The tree ADT also supports the following query methods:
isInternal(v): Test whether node v is internal.
Input: Position; Output: Boolean
isExternal(v): Test whether node v is external.
Input: Position; Output: Boolean
isRoot(v): Test whether node v is the root.
Input: Position; Output: Boolean
Other methods are:
size(): Return the number of nodes in the tree.
Input: None; Output: Integer
elements(): Return an iterator of all the elements stored at nodes of
the tree.
Input: None; Output: Iterator of elementss
positions(): Return an iterator of all the nodes of the tree.
Input: Position; Output: Iterator of positions
swapElements(v, w): Swap the elements stored at the nodes v and w.
Input: Two positions; Output: None
replaceElement(v,e): Replace with e and return the element stored at node v.
Input: A position and an object; Output: Object
A Tree Interface in Java
public interface Tree {
public int size();
public Boolean isEmpty();
public ElementIterator elements();
public PositionIterator positions();
public void swapElements( Position v, Position w );
public Object replaceElement( Position v, Object e );
public Position root();
public Position parent( Position v );
public PositionIterator children( Position v );
public boolean isInternal( Position v );
public boolean isExternal( Position v );
public boolean isRoot( Position v );
}
Construction of Interface
Hierarchy
public interface InspectableContainer {
public int size();
public Boolean isEmpty();
public ElementIterator elements();
}
public interface InspectablePositionalContainer
extends InspectableContainer {
public PositionIterator positions();
}
public interface PositionalContainer
extends InspectablePositionalContainer {
public void swapElements( Position v, Position w );
public Object replaceElement( Position v, Object e );
}
Since this interface inherites InspectablePositionalContainer,
the other available methods are:
size
isEmpty
elements
positions
public interface InspectableTree
extends InspectablePositionalContainer {
public Position root();
public Position parent( Position v );
public PositionIterator children( Position v );
public boolean isInternal( Position v );
public boolean isExternal( Position v );
public boolean isRoot( Position v );
}
Since this interface extends InspectablePositionalContainer, the
other available methods are
size
isEmpty
elements
positions
public interface Tree
extends InspectableTree, PositionalContainer {
}
Since this interface inherites both InspectableTree and
PositionalContainer, the available methods are:
size
isEmpty
elements
positions
swapElements
replaceElement
root
parent
children
isInternal
isExternal
isRoot
IspectableContainer
size
isEmpty
Elements
IspectablePositionContainer
positions
InspectableTree
root
parent
children
isRoot
isInternal
isExternal
PositionContainer
swapElement
replaceElement
Tree
The Binary Tree Abstract Data
Type
Recall that the two children of an internal node in a binary tree are
labelled as a left child and a right child, respectively. Therefore,
the binary tree ADT supports three more methods.
leftChild(v):
Return the left child of v; an error condition occurs if v
is an external node.
Input: Position; Output: Position
rightChild(v): Return the right child of v; an error condition occurs if
v is an external node.
Input: Position; Output: Position
sibling(v):
Return the sibling of v; an error condition occurs if v is
the root.
Input: Position; Output: Position
A Binary Tree Interface in Java
Similar to the interfaces InspectableTree and Tree, we have
interfaces InspectableBinaryTree and BinaryTree.
public interface InspectableBinaryTree
extends InspectableTree {
public Position leftChild( Position v );
public Position rightChild( Position v );
public Position sibling( Position v );
}
public interface BinaryTree
extends InspectableBinaryTree, PositionalContainer {
}
InspectableContainer
size
isEmpty
elements
InspectablePositionalContainer
positions
InspectableList
first
last
before
after
positions
InspectableVector
elemAtRank
Vector
replaceAtRank
insertAtRank
removeAtRank
InspectableSequence
atRank
rankOf
Sequence
List
insertFirst
insertLast
insertBefore
insertAfter
remove
PositionalContainer
swapElements
replaceElement
InspectableTree
root
parent
children
isRoot
isInternal
isExternal
InspectableBinaryTree
leftChild
rightChild
sibling
Tree
BinaryTree