230 likes | 253 Views
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.
E N D
Introduction to Data Structure Chapter 10 Ming Li Department of Computer Science California State University, Fresno Fall 2006
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.
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.
Selected Samples of Binary Trees Density = 5/4 = 1.25 Density = 9/3 = 3
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
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.
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
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?
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
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
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
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
Binary Trees – Coding struct node { int data; struct node* left; struct node* right; }
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.