Title: Trees
1Trees
2Chapter Contents
- Tree Concepts
- Hierarchical Organization, Terminology
- Traversals of a Tree
- Binary Tree, General Tree
- Java Interfaces for Trees
- All Trees, Binary Tree
- Examples of Binary Trees
- Expression Trees, Decision Trees, Binary Search
Trees - Examples of General Trees
- Parse Trees, Game Trees
3Tree Concepts
- Previous data organizations place data in linear
order - Some data organizations require categorizing data
into groups, subgroups - This is hierarchical classification
- Data items appear at various levels within the
organization
4Hierarchical Organization
Fig. 24-1 Computer files organized into folders.
5Hierarchical Organization
- Example A university's organization
Fig. 24-2 A university's administrative structure.
6Hierarchical Organization
Fig. 24-3 Carole's children and grandchildren.
7Hierarchical Organization
Fig. 24-4 Jared's parents and grandparents.
8Tree Terminology
- A tree is
- A set of nodes
- Connected by edges
- Edges indicate relationships among nodes
- Nodes arranged in levels
- Indicate hierarchy
- Top level is a single node called the root
9Tree Terminology
Fig. 24-5 A tree equivalent to the tree in Fig.
24-1 MyStuff (level 1) has Home, Work, Play,
School (level 2)
10Tree Terminology
- Nodes at a given level are children of nodes of
previous level - Node with children is the parent node of those
children - Nodes with same parent are siblings
- Node with no children is a leaf node
- The only node with no parent is the root
- All others have one parent each
11Tree Terminology
- Empty trees?
- Some authors specify a general tree must have at
least the root node - This text will allow all trees to be empty
- A node is reached from the root by a path
- The length of the path is the number of edges
that compose it - The height of a tree is number of levels in tree
- The subtree of a node is a tree rooted at a child
of that node
12Binary Trees
- Each node has at most two children
Fig. 24-6 Three binary trees.
13Binary Trees
- A binary tree is either empty or has the
following form - Where Tleft and Tright are binary trees
14Binary Trees
- Every nonleaf in a full binary tree has exactly
two children - A complete binary tree is full to its
next-to-last level - Leaves on last level filled from left to right
- Height of a binary tree with n nodes that is
either complete or full is log2(n 1)
15Binary Trees
Fig. 24-7 The number of nodes in a full binary
tree as a function of the tree's height.
16Traversals of a Tree
- Visiting a node
- Processing the data within a node
- This is the action performed on each node during
traversal of a tree - A traversal can pass through a node without
visiting it at that moment - For a binary tree
- Visit the root
- Visit all nodes in the root's left subtree
- Visit all nodes in the root's right subtree
17Traversals of a Tree
- Preorder traversal visit root before the subtrees
Fig. 24-8 The visitation order of a preorder
traversal.
18Traversals of a Tree
- Inorder traversal visit root between visiting
the subtrees
Fig. 24-9 The visitation order of an inorder
traversal.
19Traversals of a Tree
- Postorder traversal visit root after visiting
the subtrees
These are examples of a depth-first traversal.
Fig. 24-10 The visitation order of a postorder
traversal.
20Traversals of a Tree
- Level-order traversal begin at the root, visit
nodes one level at a time
This is an example of a breadth-first traversal.
Fig. 24-11 The visitation order of a level-order
traversal.
21Traversals of a General Tree
- A general tree can be traversed
- Level order
- Preorder
- Postorder
- Inorder traversal not well defined for a general
tree
22Traversals of a General Tree
Fig.24-12 The visitation order of two traversals
of a general tree (a) preorder (b) postorder.
23Java Interfaces for Trees
- An interface that specifies operations common to
all trees
public interface TreeInterface public Object
getRootData() public int getHeight() public
int getNumberOfNodes() public boolean
isEmpty() public void clear() // end
TreeInterface
24Java Interfaces for Trees
- Interface for iterators for various traversals
import java.util.Iteratorpublic interface
TreeIteratorInterface public Iterator
getPreorderIterator() public Iterator
getPostorderIterator() public Iterator
getInorderIterator() public Iterator
getLevelOrderIterator() // end
TreeIteratorInterface
25Java Interfaces for Trees
- Interface for a class of binary trees
public interface BinaryTreeInterface extends
TreeInterface, TreeIteratorInterface public
void setTree(Object rootData) public void
setTree(Object rootData, BinaryTreeInterface
leftTree, BinaryTreeInterface rightTree)
// end BinaryTreeInterface
26Java Interfaces for Trees
Leaf nodes use simple constructor then build to
higher levels with multiarg constructor (see p.
551)
Fig. 24-13 A binary tree whose nodes contain
one-letter strings.
27Examples of Binary Trees
Fig. 24-14 Expression trees for four algebraic
expressions.
28Examples of Binary Trees
- Algorithm for evaluating an expression tree in
postorder traversal
Algorithm evaluate(expressionTree)if
(expressionTree is empty) return
0else firstOperand evaluate(left
subtree) secondOperand evaluate(right
subtree) operator the root of
expressionTree return firstOperand
secondOperand
29Decision Trees
- A decision tree can be the basis of an expert
system - Helps users solve problems, make decisions
Fig. 24-15 A binary decision tree.
30Decision Trees
- A possible Java interface for a binary decision
tree.
public interface DecisionTreeInterface
extends BinaryTreeInterface public Object
getCurrentData() public boolean
isAnswer() public void advanceToNo() public
void advanceToYes() public void reset() //
end DecisionTreeInterface
31Decision Trees
Fig. 24-16 An initial decision tree for a
guessing game.
32Decision Trees
Fig. 24-17 The decision tree for a guessing game
after acquiring another fact.
33Binary Search Trees
- A search tree organizes its data so that a search
is more efficient - Binary search tree
- Nodes contain Comparable objects
- A node's data is greater than the data in the
node's left subtree - A node's data is less than the data in the node's
right subtree
34Binary Search Trees
Fig. 24-18 A binary search tree of names.
35Binary Search Trees
Fig. 24-19 Two binary search trees containing the
same names as the tree in Fig. 24-18
36Binary Search Trees
- Algorithm for searching binary search tree
Algorithm bstSearch(binarySearchTree,
desiredObject)if (binarySearchTree is
empty) return falseelse if (desiredObject
root) return trueelse if (desiredObject root) return bstSearch(left subtree,
desiredObject)else return bstSearch(right
subtree, desiredObject)
37Heaps
- A complete binary tree
- Nodes contain Comparable objects
- Each node contains no smaller (or no larger) than
objects in its descendants - Maxheap
- Object in a node is its descendant objects
- Minheap
- Object in a node is descendant objects
38Heaps
Fig. 24-20 (a) A maxheap and (b) a minheap that
contain the same values
39Examples of General Trees
Fig. 24-21 A parse tree for the algebraic
expression a (b c)
40Examples of General Trees
Fig. 24-22 A portion of a game tree for
tic-tac-toe