Trees and Tree Traversals
Prof. Sin-Min Lee
Department of Computer Science
San Jose State University
Outline
• Graph-Tree– Basic Definition– Rooted Trees– Applications of Trees– Game Tree
Basic Definition
• Tree– a connected graph with no simple circuits– No multiple edges or loops
• Theorems– a graph is a tree iff a unique simple path
between any two of its vertices– A tree with n vertices has n – 1 edges
• Forest
Rooted Tree
• Is a directed graph T satisfying– It is a tree when the directions of the edges are ignored.– There is a unique vertex r (called the root) such that the
indegree of r is 0 and the indegree of any other vertex is 1.
• Examples: family tree, position tree• Terminology: parent, child, terminal vertex,
internal vertex, subtree, ancestor, descendant• An m-ary tree, A full m-ary tree, A balanced m-
ary tree with height h
Order Rooted Tree
• Is a rooted tree where the children of each internal vertex are ordered.
• ordered binary tree: two child, left/right
Tree Traversal
• Is a procedure that systematically visits every vertex of an ordered rooted tree
• Three most commonly used algorithms– Preorder traversal.
– Inorder traversal.
– Postorder traversal.
Example#1
• List three kinds of traversals.
Binary Search Trees
• Is a binary tree in which each vertex is labeled with a key such that– No two vertices have the same key– If vertex u belongs to the left subtree of vertex v, then u v
– If vertex w belongs to the right subtree of vertex v, then v w
• construction algorithm
S=1+2+22+23+s4+……+262+263
2S=2+22+23+24+……+262+263+264
2S-S=S=264-1 264=24×260 =16×1018
150 years to get this produce
Carl Sagan (1934-1996.12.20)
A Pale Blue Planet Mourns the Passing of a Passionate Scientist