cs-2852 data structures lecture 11 andrew j. wozniewicz image copyright © 2010 andyjphoto.com
TRANSCRIPT
CS-2852Data StructuresLECTURE 11
Andrew J. Wozniewicz
Image copyright © 2010 andyjphoto.com
CS-2852 Data Structures, Andrew J. Wozniewicz
Agenda• Tree Overview • Binary Tree– Binary Search Tree
• Searching• Traversals– Pre-Order– In-Order– Post-Order
CS-2852 Data Structures, Andrew J. Wozniewicz
Trees
Leaves
Branches
Root
COMPUTER SCIENCE
Root
Branches
Leaves
NATURE
CS-2852 Data Structures, Andrew J. Wozniewicz
Trees
COMPUTER SCIENCE
Leaves
Branches
Root
What is a Tree?Vice-
President
Team Lead
SE1
SE2
SE3
Team Lead
SE4
SE5
Manager
BA1
BA2
Consultant
What is a Tree?Vice-
President
Team Lead
SE1
SE2
SE3
Team Lead
SE4
SE5
Manager
BA1
Consultant Secretary
ROOT
What is a Tree?Vice-
President
Team Lead
SE1
SE2
SE3
Team Lead
SE4
SE5
Manager
BA1
Consultant Secretary
ROOT
CHILD
What is a Tree?Vice-
President
Team Lead
SE1
SE2
SE3
Team Lead
SE4
SE5
Manager
BA1
Consultant Secretary
ROOT
CHILD
LEAVES
CS-2852 Data Structures, Andrew J. Wozniewicz
What is a Tree?
• 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.
Each Child has exactly one Parent
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
• 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
Number of nodes in the longest path from the root node to a leaf node.
DEFINITION
CS-2852 Data Structures, Andrew J. Wozniewicz
Tree Terminology• Node (External/Internal)• Root, Parent, Child• Ancestor, Descendant, Sibling• Branch, Subtree
Binary Tree
• Hierarchy of data with some constraints
• A Root node• 0-2 children– Left Child– Right Child
• Each child is itself a tree
CS-2852 Data Structures, Andrew J. Wozniewicz
(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 3
2
4
5 7
6
261375
CS-2852 Data Structures, Andrew J. Wozniewicz
Adding Data• Recursive algorithm:• Case 1: Empty Tree
– Becomes the root node• Case 2: Smaller Value
– Recursively add to left• Case 3: Larger Value
– Recursively add to right• Case: Equal Value
– Treat as larger value 1
2
4
7
6
2617
4
4
CS-2852 Data Structures, Andrew J. Wozniewicz
Searching
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);}
1 3
2
4
5 7
6
theRoot
• find(theRoot, 3);• find(theRoot, 5);• find(theRoot, 8);
CS-2852 Data Structures, Andrew J. Wozniewicz
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
• Linked List, Array: – Left-to-Right– Right-to-Left
• Stack– Last-In, First-Out
• Queue– First-In, First-Out
• Tree?– Left-First?– Right-First?
1 3
2
4
5 7
6
theRoot
CS-2852 Data Structures, Andrew J. Wozniewicz
Tree Traversals• Enumerate nodes in a well-known
order…• Basic algorithm:
–Process Current Node–Visit Left–Visit Right
• What varies is the order No
parti
cula
r Ord
er Im
plie
d
CS-2852 Data Structures, Andrew J. Wozniewicz
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 Traversalvoid visit(Node current) { if (current==null) return; process(current.value); visit(current.left); visit(current.right);}
2 8
1 3 6
5 7
4
4 2 1 3 8 6 5 7
CS-2852 Data Structures, Andrew J. Wozniewicz
In-Order Traversalvoid visit(Node current) { if (current==null) return; visit(current.left); process(current.value); visit(current.right);}
2 8
1 3 6
5 7
4
1 2 3 4 5 6 7 8
CS-2852 Data Structures, Andrew J. Wozniewicz
Post-Order Traversalvoid visit(Node current) { if (current==null) return; visit(current.left); visit(current.right); process(current.value);}
2 8
1 3 6
5 7
4
1 3 2 5 7 6 8 4
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
Questions?
Image copyright © 2010 andyjphoto.com