1 / 23

Introduction to Data Structure

Introduction to Data Structure. Chapter 10 Ming Li Department of Computer Science California State University, Fresno Fall 2006. Tree Structures. Tree Structures. Tree Structures. Tree Node Level and Path Length. Binary Tree Definition.

lorettag
Download Presentation

Introduction to Data Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Tree Structures

  3. Tree Structures

  4. Tree Structures

  5. Tree Node Level and Path Length

  6. 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 TL and the right subtreeTR. The nodes in T consist of node R and all the nodes in TL and TR.

  7. 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 • 2d (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. Selected Samples of Binary Trees Density = 5/4 = 1.25 Density = 9/3 = 3

  9. Selected Samples of Binary Trees A 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. B C D E Tree B Size 5 Depth 4

  10. 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. Tree Node Level and Path Length – Complete Tree

  12. Tree Node Level and Path Length – Complete Tree

  13. Tree Node Level and Path Length – Complete Tree

  14. 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+…+ 2d-1 = 2d - 1 For level d, the number of nodes varies from 1 to 2d . So, the total size of a tree varies from: 2d-1+1 to 2d-1+2d, or 2d to 2d+1-1

  15. Density of Complete Binary Trees Let the number of nodes of the complete tree is n, then 2d <= n <= 2d+1-1 < 2d+1 That is d <= logn < d+1 What does this mean? d = int(logn) Why is it true only for complete tree?

  16. Binary Tree Traversal – Preorder • Visit the node • Traverse the left subtree (“go left”). • Traverse the right subtree (“go right”). A B C D E G F H I J

  17. Binary Tree Traversal – Preorder void Preorder(struct tnode* root) { if(root==NULL) return; visit(root); Preorder(root->left); Preorder(root->right); } A B C D E G F H I J

  18. Binary Tree Traversal – Inorder • Traverse the left subtree (“go left”). • Visit the node • Traverse the right subtree (“go right”). A B C D E G F H I J

  19. Binary Tree Traversal – Postorder • Traverse the left subtree (“go left”). • Traverse the right subtree (“go right”). • Visit the node A B C D E G F H I J

  20. Binary Tree Nodes

  21. Binary Tree Nodes

  22. Binary Trees – Coding struct node {     int data;     struct node* left;     struct node* right; }

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

More Related