Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS-2852
Data Structures
LECTURE 11
Andrew J. Wozniewicz
Image copyright © 2010 andyjphoto.com
Agenda
• Tree Overview
• Binary Tree
– Binary Search Tree
• Searching
• Traversals
– Pre-Order
– In-Order
– Post-Order
CS-2852 Data Structures, Andrew J. Wozniewicz
Trees
NATURE
COMPUTER SCIENCE
Root
Leaves
Branches
Root
Branches
Leaves
CS-2852 Data Structures, Andrew J. Wozniewicz
Trees
COMPUTER SCIENCE
Root
Leaves
Branches
CS-2852 Data Structures, Andrew J. Wozniewicz
What is a Tree?
VicePresident
Consultant
Team Lead
Team Lead
Manager
SE1
SE4
BA1
SE2
SE5
BA2
SE3
What is a Tree?
VicePresident
ROOT
Consultant
Secretary
Team Lead
Team Lead
Manager
SE1
SE4
BA1
SE2
SE5
SE3
What is a Tree?
VicePresident
ROOT
Consultant
Secretary
Team Lead
Team Lead
Manager
SE1
SE4
BA1
SE2
SE5
SE3
CHILD
What is a Tree?
VicePresident
ROOT
Consultant
Secretary
Team Lead
Team Lead
Manager
SE1
SE4
BA1
SE2
SE5
SE3
LEAVES
CHILD
What is a Tree?
Each Child has exactly one
Parent
• There is exactly one (i.e. unique) path from
the root to each node.
• There is exactly one path from each node to
the root.
• There is exactly one path between any nodes.
• Each node can have arbitrarily many children.
CS-2852 Data Structures, Andrew J. Wozniewicz
What is a Tree?
• Like Linked Lists, Trees are “linked”
data structures.
• Hierarchical, rather than linear
chaining.
• A node can be a “child” of another
node; the only node that is not a
“child” is the “root”.
• Nodes with children are referred to as
“Parents”.
CS-2852 Data Structures, Andrew J. Wozniewicz
Height of a Tree
DEFINITION
Number of nodes in the longest path
from the root node to a leaf node.
• Depth (level): Measures the distance
from its root
– Root node has level 1
– Any other node’s level is the level of its
parent + 1
CS-2852 Data Structures, Andrew J. Wozniewicz
Tree Terminology
•
•
•
•
Node (External/Internal)
Root, Parent, Child
Ancestor, Descendant, Sibling
Branch, Subtree
CS-2852 Data Structures, Andrew J. Wozniewicz
Binary Tree
• Hierarchy of data with
some constraints
• A Root node
• 0-2 children
– Left Child
– Right Child
• Each child is itself a tree
(Binary) Tree Node
protected static class Node<E> {
protected E data;
protected Node<E> left;
protected Node<E> right;
public Node(E data) {
this.data = data;
this.left = null;
this.right = null;
}
What does it resemble?
}
CS-2852 Data Structures, Andrew J. Wozniewicz
Binary Search Tree
• Same structural
rules as Binary
Tree…
• A sorted hierarchy
of data
– Left child less than
parent
– Right child greater
than parent
1
26
573
4
2
1
6
3
CS-2852 Data Structures, Andrew J. Wozniewicz
5
7
Adding Data
• Recursive algorithm:
• Case 1: Empty Tree
4126
7
4
– Becomes the root
node
• Case 2: Smaller Value
2
– Recursively add to
left
6
• Case 3: Larger Value
– Recursively add to
right
1
• Case: Equal Value
– Treat as larger value
CS-2852 Data Structures, Andrew J. Wozniewicz
4
7
Searching
theRoot
Node find(Node root, E value) {
if (root == null)
return null;
if (root.value==value)
return root;
if (value < root.value)
return find(current.left, value);
return find(current.right,value);
}
• find(theRoot, 3);
• find(theRoot, 5);
• find(theRoot, 8);
4
2
1
6
3
CS-2852 Data Structures, Andrew J. Wozniewicz
5
7
Advantages of Trees
• Don’t ever have to examine every
node to determine if a value is in.
– Unlike in a Linked List
• Only looking at a subset of the data in
the tree
• The largest possible number of
comparisons is equal to the height of
the tree.
CS-2852 Data Structures, Andrew J. Wozniewicz
Full, Perfect, and Complete BT
• Full:
– All nodes have either 0 or 2 children
• Perfect
– Full, with 2height-1 nodes
• Complete
– Perfect through level height-1
CS-2852 Data Structures, Andrew J. Wozniewicz
Linked Structure Traversals
theRoot
• Linked List, Array:
– Left-to-Right
– Right-to-Left
4
• Stack
– Last-In, First-Out
• Queue
2
6
– First-In, First-Out
• Tree?
– Left-First?
– Right-First?
1
3
CS-2852 Data Structures, Andrew J. Wozniewicz
5
7
• Enumerate nodes in a well-known
order…
• Basic algorithm:
–Process Current Node
–Visit Left
–Visit Right
• What varies is the order
CS-2852 Data Structures, Andrew J. Wozniewicz
No particular Order Implied
Tree Traversals
Three Common Traversal Orders
• Pre-Order
– Process Current
– Visit Left Child
– Visit Right Child
• In-Order
– Vist Left Child
– Process Current
– Visit Right Child
• Post-Order
– Visit Left Child
– Visit Right Child
– Process Current Child
CS-2852 Data Structures, Andrew J. Wozniewicz
Pre-Order Traversal
void visit(Node current) {
if (current==null)
return;
process(current.value);
visit(current.left);
visit(current.right);
}
42138657
CS-2852 Data Structures, Andrew J. Wozniewicz
In-Order Traversal
void visit(Node current) {
if (current==null)
return;
visit(current.left);
process(current.value);
visit(current.right);
}
12345678
CS-2852 Data Structures, Andrew J. Wozniewicz
Post-Order Traversal
void visit(Node current) {
if (current==null)
return;
visit(current.left);
visit(current.right);
process(current.value);
}
13257684
CS-2852 Data Structures, Andrew J. Wozniewicz
Summary
• What is a Tree?
• Binary Tree
– Binary Search Tree
• Searching
• Traversals
– Pre-Order
– In-Order
– Post-Order
CS-2852 Data Structures, Andrew J. Wozniewicz
Questions?
Image copyright © 2010 andyjphoto.com