# Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.

## Presentation on theme: "Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University."— Presentation transcript:

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+2 2 +2 3 +s 4 + …… +2 62 +2 63 2S=2+2 2 +2 3 +2 4 + …… +2 62 +2 63 +2 64 2S-S=S=2 64 -1 2 64 =2 4 ×2 60 = 16×10 18 150 years to get this produce

Carl Sagan (1934-1996.12.20) A Pale Blue Planet Mourns the Passing of a Passionate Scientist A Pale Blue Planet Mourns the Passing of a Passionate Scientist

Download ppt "Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University."

Similar presentations