Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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:

1 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

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

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

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

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

6 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.

7 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.

8 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

9 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.

10 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.

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

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

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

14 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

15 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?

16 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

17 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

18 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

19 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

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

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

22 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; }

23 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


Ads by Google