Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree.

Similar presentations


Presentation on theme: "Tree."— Presentation transcript:

1 Tree

2 Tree A tree is a collection of nodes and edges
A tree has only one root Trees are hierarchical: Parent-child relationship between two nodes basic operations of a tree: tree traversals insert node delete node searching

3 Level E R T L P M A S 1 2 3 root Child (of root)
Leaves or terminal nodes Child (of root) Depth of T: 2 Tree height: 4 1 2 3

4 Binary Trees A tree in which no node can have more than two children

5 A General Tree & A Binary Tree

6 Balanced Binary Trees A binary tree is balanced if the heights of any node’s two subtrees differ by no more than 1 Complete binary trees are balanced Full binary trees are complete and balanced B = HL - HR

7 Example: A A B A A B B C D E Balance Balance Balance Balance 2-1=1
1-1=0 3-2=1 1-2=-1 A A B Balance Balance A A B B C D E Balance Balance

8 + 3 2 Example of tree application: Represent algebraic formulas
Q: Write the following operations as binary tree and determine the (tree Depth, tree height, number of levels). A. 2+3 + 1 Tree depth 2 Tree height Number of levels 2 3

9 * / - 6 2 6 2 B. (6/2) * (20-4) 2 Tree depth 3 Tree height
Number of levels B. (6/2) * (20-4) * / - 6 2 6 2

10 Tree traversal Type of Traversal Inorder traversal Preorder traversal
Postorder traversal

11 Inorder traversal Recursively print out all data in the left subtree Print the data at the root Recursively print out all data in the right subtree Pre-order traversal Print the data at the root Recursively print out all data in the left subtree Recursively print out all data in the right subtree Postorder traversal Recursively print out all data in the left subtree Recursively print out all data in the right subtree Print the data at the root

12 Traverse the following binary tree using the three types of tree traversal
6 Preorder (NLR) 6, 2, 1, 4, 3, 7, 10 , 9, 11 b. Postorder (LRN) 1, 3, 4, 2, 9, 11 ,10, 7, 6 c. Inorder (LNR) 1, 2, 3, 4, 6, 7, 9, 10, 11 7 2 1 4 10 3 9 11


Download ppt "Tree."

Similar presentations


Ads by Google