Presentation is loading. Please wait.

Presentation is loading. Please wait.

Foundation of Computing Systems Lecture 4 Trees: Part I.

Similar presentations


Presentation on theme: "Foundation of Computing Systems Lecture 4 Trees: Part I."— Presentation transcript:

1 Foundation of Computing Systems Lecture 4 Trees: Part I

2 31.07.09IT 60101: Lecture #42 Tree: Example

3 31.07.09IT 60101: Lecture #43 Tree: Definition A tree is a finite set of one or more nodes such that: (i) there is a specially designated node called the root (ii) remaining nodes are partitioned into n (n > 0) disjoint sets T 1, T 2,..., T n, where each T i (i = 1, 2,..., n) is a tree; T 1, T 2,..., T n are called sub trees of the root. T = (A(B(E, F(K, L))), C(G), D(H, I, J))

4 31.07.09IT 60101: Lecture #44 Binary Tree A binary tree is a special form of a tree A binary tree T is a finite set of nodes, such that (i) T is empty (called empty binary tree), or (ii) T contains a specially designated node called the root of T, and the remaining nodes of T form two disjoint binary trees T 1 and T 2 which are called left sub-tree and the right sub-trees, respectively.

5 31.07.09IT 60101: Lecture #45 Full Binary Tree

6 31.07.09IT 60101: Lecture #46 Complete Binary Tree

7 31.07.09IT 60101: Lecture #47 Skewed Binary Tree

8 31.07.09IT 60101: Lecture #48 Properties of Binary Trees Property 1 In any binary tree, maximum number of nodes on level l is 2 l, where l ≥ 0. Property 2 Maximum number of nodes possible in a binary tree of height h is 2 h – 1. Property 3 Minimum number of nodes possible in a binary tree of height h is h. Property 4 For any non-empty binary tree, if n is the number of nodes and e is the number of edges, then n = e + 1.

9 31.07.09IT 60101: Lecture #49 Properties of Binary Trees Property 5 For any non-empty binary tree T, if n 0 is the number of leaf nodes (degree = 0) and n 2 is the number of internal node (degree = 2), then n 0 = n 2 + 1. Property 6 Height of a complete binary tree with n number of nodes is Property 7 Total number of binary tree possible with n nodes is

10 31.07.09IT 60101: Lecture #410 Representation of Binary Trees Linear representation –Using array Linked representation –Using linked list structure

11 31.07.09IT 60101: Lecture #411 Linear Representation of Binary Trees 1.The root node is at location 1. 2.For any node with index i, 1 < i £ n, (for some n): (a)PARENT(i) = For the node when i = 1, there is no parent. (b)LCHILD(i) = 2 * i If 2 * i > n, then i has no left child. (c)RCHILD(i) = 2 * i + 1 If 2 * i + 1 > n, then i has no right child.

12 31.07.09IT 60101: Lecture #412 Linked Representation of Binary Trees

13 31.07.09IT 60101: Lecture #413 Linked Representation of Binary Trees

14 31.07.09IT 60101: Lecture #414 Binary Tree: Operations Major operations on a binary tree can be listed as: Insertion. To include a node into an existing (may be empty) binary tree. Deletion. To delete a node from a non-empty binary tree. Traversal. To visit all the nodes in a binary tree. Merge. To merge two binary trees into a larger one.

15 31.07.09IT 60101: Lecture #415 Binary Tree: Insertion

16 31.07.09IT 60101: Lecture #416 Binary Tree: Deletion

17 31.07.09IT 60101: Lecture #417 Binary Tree: Traversals Depth-first search Breadth-first search (level-by search) Recursive search 1. R T l T r 4.T r T l R 2.T l RT r 5.T r RT l 3.T l T r R6.RT r T l

18 31.07.09IT 60101: Lecture #418 Binary Tree: Traversals + - A B * C / E F A – B + C * E / F A B – C E F / * + F E / C * B A – + F / E * C + B – A + * / F E C – B A

19 31.07.09IT 60101: Lecture #419 Binary Tree: Traversals + - A B * C / E F A – B + C * E / F A B – C E F / * + F E / C * B A – + F / E * C + B – A + * / F E C – B A R T l T r (Preorder) T l R T r (Inorder) T l T r R (Postorder)

20 31.07.09IT 60101: Lecture #420 Binary Tree: Traversals Preorder traversal Visit the root node R. Traverse the left sub-tree of R in preorder. Traverse the right sub-tree of R in preorder. Inorder traversal Traverse the left sub-tree of the root node R is inorder. Visit the root node R. Traverse the right sub-tree of the root node R is inorder. Postorder traversal Traverse the left sub-tree of the root R in postorder Traverse the right sub-tree of the root R in postorder Visit the root node R.

21 31.07.09IT 60101: Lecture #421 Binary Tree: Merging

22 31.07.09IT 60101: Lecture #422 Different Binary Trees There are several types of binary trees possible each with its own properties. Expression tree Binary search tree Heap tree Threaded binary tree Huffman tree Height balanced tree (also known as AVL tree) Red black tree Splay tree Decision tree


Download ppt "Foundation of Computing Systems Lecture 4 Trees: Part I."

Similar presentations


Ads by Google