Download Trees - GearBox

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

Quadtree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Red–black tree wikipedia , lookup

Interval tree wikipedia , lookup

B-tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
Trees
CS 110: Data Structures and Algorithms
First Semester, 2010-2011
Definition
► The
Tree Data Structure
stores objects (nodes) hierarchically
► nodes have parent-child relationships
► operations include accessing the parent or
children of a given node
►
►A
tree T is a set of nodes such that
there is a distinguished node r
(called the root) of T that has no parent
► each node v of T except r has a parent node
►
Visualizing a Tree
Root
R
Child
T
P
O
I
A
N
M
G
Sample Uses
►A
company’s organizational structure
► Family tree
► The Java class hierarchy
► O/S directory structure
► Book (parts, chapters, sections)
► Arithmetic expressions
► Web page links
► Priority queues and search trees
Tree Terminology
► Root
► the
► Leaf
only node that has no parent
(External node)
► node
that has no children
► Internal
► node
node
that has at least one child
► Siblings
► nodes
that have a common parent
More Tree Terminology
► Ancestor
recursive definition: ancestors of a node v are v
itself and the ancestors of its parent
► proper ancestors: ancestors excluding itself
►
► Descendant
►
v is a descendant of u if u is an ancestor of v
► Subtree
►
of T rooted at v
set of all descendants of v
Even More Terminology
► Ordered
►
children of a node have a strict linear order
► Binary
►
Tree
Tree
an ordered tree where nodes have at most two
children (left and right)
► Depth
and Height of a node
depth: distance from node to root
► height: from node to its farthest descendant
►
Tree Implementations
► Array-based
implementation
elements (or references to them) are stored in an
array
► parent-child relationships derived from indices
► For a n-ary tree, suppose array indices start at 1:
►
► firstChild(i)
= n * i - (n-2)
► The
rest of the children can be accessed by
firstChild(i) + r where 0 ≤ r < n
► parent(i)
= floor((i + n-2)/n)
Linked List Implementation
► Linked
implementation
elements stored in nodes
► nodes contain pointers to parent and children
► Alternate: pointer to the parent, first child and
next sibling
►
Tree Operations
► Get
the root node of the tree
► Go to parent or children from a given node
► Add a root to an empty tree
► Add a child to a node
► Remove a node (can impose that the node be
a leaf, for simplicity)
► Get the element associated to a node
► Replace the element associated to a node
► Many more…
Binary Tree Operations
► For
a binary tree, a node has at most two
children and are ordered
► Distinguish between the left and the right
child of a node
► Implementations
are simpler for binary
trees but many concepts apply to general
trees
Tree Traversals
►A
traversal visits each node of the tree in
a systematic manner
► A “visit” denotes some type of action on
the current node
► Types
► Preodrer
► Postorder
► Inorder
Preorder Traversal
► Visit
a node first, then its children
public void preorder( TreeNode root ) {
visit( root ); // do what is needed
if ( root.getLeft() != null ) {
preorder( root.getLeft() );
}
if ( root.getRight() != null ) {
preorder( root.getRight() );
}
}
Postorder Traversal
► Visit
the children first, then the node
public void postorder( TreeNode root ) {
if( root.getLeft() != null ) {
postorder( root.getLeft() );
}
if( root.getRight() != null ) {
postorder( root.getRight() );
}
visit( root ); // do what is needed
}
Inorder Traversal
► Visit
left child, then node, then right child
public void inorder( TreeNode root ) {
if ( root.getLeft() != null ) {
inorder( root.getLeft() );
}
visit( root ); // do what has to be done
if ( root.getRight() != null ) {
inorder( root.getRight() );
}
}
Example: Expression Trees
► Used
to represent an arithmetic
expression
► Internal
nodes  operators
► External nodes  operands
► Hierarchy
denotes the order in which the
operations are done  children first
Example: Expression Trees
+
/
8
*
-
4
► Expression
3
2
Tree for
((8/(4–2))+(3*2))
2
Example: Prefix Notation
►A
way of writing expressions where the
operator comes first, before the two
operands
► Let’s use * / + - for operators, and one
digit numbers for operands
► Examples
► “+52”
is the prefix notation for “5+2”
► “/63” is “6/3”  order matters
Example: Prefix Notation
► When
the operand is another operator, evaluate it
first, using the next two characters
► Examples
►
“*5+23” means 5 * (2 + 3)
►
►
►
The second operand for * is +, so we evaluate + first before
evaluating *
“*-32+14” means (3-2) * (1+4)
►
The first operand for * is -, evaluate it first
►
Second operand for * is +, NOT 3
Allows us to get rid of parentheses
Example: Prefix Notation
► Given
an expression tree, we can
generate the prefix notation for the
expression by performing preorder
traversal
► Do a standard preorder traversal but
change visit() to print the character
► Since it is preorder, print the parent first,
then the children
Example: Prefix Notation
►
Code for printing prefix notation given an expression tree
public void preorder( TreeNode exprTree ) {
//replace visit() with print
//assume the element of the TreeNode is a character
System.out.print( exprTree.getElement() );
if( exprTree.getLeft() != null ) {
preorder( exprTree.getLeft() );
}
if( exprTree.getRight() != null ) {
preorder( exprTree.getRight() );
}
}
Example: Prefix (Preorder)
+
/
8
*
-
4
+
3
2
2
Example: Prefix (Preorder)
+
/
8
*
-
4
+/
3
2
2
Example: Prefix (Preorder)
+
/
8
*
-
4
+/8
3
2
2
Example: Prefix (Preorder)
+
/
8
*
-
4
+/8-
3
2
2
Example: Prefix (Preorder)
+
/
8
*
-
4
+/8–4
3
2
2
Example: Prefix (Preorder)
+
/
8
*
-
4
3
2
+/8–42
2
Example: Prefix (Preorder)
+
/
8
*
-
4
3
2
+/8–42*
2
Example: Prefix (Preorder)
+
/
8
*
-
4
3
2
+/8–42*3
2
Example: Prefix (Preorder)
+
/
8
*
-
4
3
2
+/8–42*32
2
Other Examples
► Postfix
notation – left and right operands first,
before operator
►
►
For the same expression three, the postfix notation is
842-/32*+
Postorder traversal can be used
► Infix
notation – the usual way, with parentheses
to denote sequence
►
(8/(4–2))+(3*2)
►
Inorder traversal can be used (with modifications)
Try it out…
►Create
an expression tree given an
expression in postfix notation
Use a stack, push for any operand, pop when
an operator is seen
► How about prefix notation?
►
► Write
tree
►
code that will compute an expression
What would you use?
Data Structures that use Trees
► Priority
queues
► Heap:
tree structure (usually array-based)
where the hierarchy imply the priorities of
the elements
► Dictionary
► Binary
Search Tree: tree structure (usually
node-based) that facilitates “binary search”
of the nodes