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

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

Embed Size (px)

Citation preview

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

Trees and Tree Traversals

Prof. Sin-Min Lee

Department of Computer Science

San Jose State University

Page 2: 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

Page 3: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 4: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 5: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 6: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 7: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 8: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 9: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University

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

Page 10: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 11: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 12: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University

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

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

Order Rooted Tree

• Is a rooted tree where the children of each internal vertex are ordered.

• ordered binary tree: two child, left/right

Page 14: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 15: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 16: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 17: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University

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.

Page 18: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 19: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 20: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University

Example#1

• List three kinds of traversals.

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

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

Page 22: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 23: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University

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

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

Carl Sagan (1934-1996.12.20)

 

  A Pale Blue Planet Mourns the Passing of a Passionate Scientist

Page 25: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 26: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 27: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 28: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 29: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 30: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 31: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 32: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 33: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 34: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 35: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 36: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 37: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 38: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 39: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 40: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 41: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 42: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 43: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 44: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 45: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University
Page 46: Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University