# Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.

## Presentation on theme: "Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of."— Presentation transcript:

Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of Computer Science California State University, Fresno Fall 2006

Introduction to Data Structure, Fall 2006 Slide- 2 California State University, Fresno Tree Structures

Introduction to Data Structure, Fall 2006 Slide- 3 California State University, Fresno Tree Structures

Introduction to Data Structure, Fall 2006 Slide- 4 California State University, Fresno Tree Structures

Introduction to Data Structure, Fall 2006 Slide- 5 California State University, Fresno Tree Node Level and Path Length

Introduction to Data Structure, Fall 2006 Slide- 6 California State University, Fresno Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties: –(a) T is a tree if the set of nodes is empty. (An empty tree is a tree.) –(b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree T L and the right subtreeT R. The nodes in T consist of node R and all the nodes in T L and T R.

Introduction to Data Structure, Fall 2006 Slide- 7 California State University, Fresno Density of Binary Trees For a tree with depth d, the maximum number of nodes on each level n varies from 1 (if there is at most one node having one leaf node (or child) to 2 d (if all nodes have two leaf nodes (or children) Density is a measure of the size of a tree (number of nodes) relative to the depth of the tree. If density high, pack more nodes near the root. Search, insert, delete will be convenient. If density low, many nodes will be far from the root. Search, insert, delete will be difficult.

Introduction to Data Structure, Fall 2006 Slide- 8 California State University, Fresno Selected Samples of Binary Trees Density = 9/3 = 3 Density = 5/4 = 1.25

Introduction to Data Structure, Fall 2006 Slide- 9 California State University, Fresno Selected Samples of Binary Trees E D C B A Tree B Size 5 Depth 4 Degenerate tree: A tree in which there is a single leaf node and each interior (internal) node has only one child. Equivalent to linked list.

Introduction to Data Structure, Fall 2006 Slide- 10 California State University, Fresno Tree Node Level and Path Length – Complete Tree A tree in which all leaf nodes are at some depth n or n-1, and all leaves at depth n are toward the left.

Introduction to Data Structure, Fall 2006 Slide- 11 California State University, Fresno Tree Node Level and Path Length – Complete Tree

Introduction to Data Structure, Fall 2006 Slide- 12 California State University, Fresno Tree Node Level and Path Length – Complete Tree

Introduction to Data Structure, Fall 2006 Slide- 13 California State University, Fresno Tree Node Level and Path Length – Complete Tree

Introduction to Data Structure, Fall 2006 Slide- 14 California State University, Fresno Density of Complete Binary Trees For a complete binary tree with depth d At levels from 0 to d-1, since all nodes have two leaf nodes, the total number of nodes is 1+2+4+…+ 2 d-1 = 2 d - 1 For level d, the number of nodes varies from 1 to 2 d. So, the total size of a tree varies from: 2 d -1+1 to 2 d -1+2 d, or 2 d to 2 d+1 -1

Introduction to Data Structure, Fall 2006 Slide- 15 California State University, Fresno Density of Complete Binary Trees Let the number of nodes of the complete tree is n, then 2 d <= n <= 2 d+1 -1 < 2 d+1 That is d <= logn < d+1 What does this mean? d = int(logn) Why is it true only for complete tree?

Introduction to Data Structure, Fall 2006 Slide- 16 California State University, Fresno Binary Tree Traversal – Preorder 1.Visit the node 2.Traverse the left subtree (“go left”). 3.Traverse the right subtree (“go right”). A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 17 California State University, Fresno Binary Tree Traversal – Preorder void Preorder(struct tnode* root) { if(root==NULL) return; visit(root); Preorder(root->left); Preorder(root->right); } A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 18 California State University, Fresno Binary Tree Traversal – Inorder 1.Traverse the left subtree (“go left”). 2.Visit the node 3.Traverse the right subtree (“go right”). A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 19 California State University, Fresno Binary Tree Traversal – Postorder 1.Traverse the left subtree (“go left”). 2.Traverse the right subtree (“go right”). 3.Visit the node A E D C B G F JI H

Introduction to Data Structure, Fall 2006 Slide- 20 California State University, Fresno Binary Tree Nodes

Introduction to Data Structure, Fall 2006 Slide- 21 California State University, Fresno Binary Tree Nodes

Introduction to Data Structure, Fall 2006 Slide- 22 California State University, Fresno Binary Trees – Coding struct node { int data; struct node* left; struct node* right; }

Introduction to Data Structure, Fall 2006 Slide- 23 California State University, Fresno Binary Trees – Typical Problems Build (insert/delete) a tree. Count the number of nodes in a tree. Count the maximum depth in a tree. Print a tree in preorder. Print a tree in inorder. Print a tree in Postorder. Print all possible paths in a tree. Check if two binary trees are the same.

Download ppt "Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of."

Similar presentations