Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.

Similar presentations


Presentation on theme: "CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM."— Presentation transcript:

1 CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM

2 2 Trees A data structure which consists of a finite set of elements called nodes or vertices a finite set of directed arcs which connect the nodes If the tree is nonempty one of the nodes (the root) has no incoming arc every other node can be reached by following a unique sequence of consecutive arcs

3 3 Binary Trees Each node has at most two children Useful in modeling processes where a comparison or experiment has exactly two possible outcomes the test is performed repeatedly Example multiple coin tosses encoding/decoding messages in dots and dashes such as Mores code

4 4 Example of a Binary Tree Root Leaf Parents

5 5 Binary Trees each node may have 0, 1, or 2 children. node on the left as the left child and the node on the right as the right child Binary tree is a set of nodes that either the tree is empty, or the tree is partitioned into three disjoint subsets: a single node R, the root; two possible empty sets that are binary trees, called left and right subtrees of R

6 6 Binary Trees Degenerate tree occurs in which there is a single leaf node and each non-leaf node has only one child. It is equivalent to a linked list. A complete binary tree is a tree in which each level 0 to n-1 has full set of node and all leaf nodes at level n occupy the leftmost position in the tree.

7 7 Binary Trees - complete

8 8 Binary Trees - Degenerate tree

9 9 Binary Trees ARRAY IMPLEMENTATION

10 10 Binary Trees - Array Assume you have the below diagram:

11 11 Binary Trees - Array We can represent this binary tree using an array, arr.

12 12 Binary Trees - Array Eg: Locate parent of C C = A[9] Parents of C = A[9/2] = A[4] = B Locate right child of F F = A[5] = A[2[5] + 1] = A[11] = G

13 13 Binary Trees - Array Issue: You must prepare maximum array element to store the binary tree database on the depth (3 level = 2 3 = 8 element), even if your binary tree is not a complete binary tree. Many wasted empty spaces.

14 14 Binary Trees TREE TRAVERSAL

15 15 Tree Traversal Refers to the process of visiting all the nodes of the tree, in some specific sequence. In a tree, visiting a node generally means retrieving the data items contained in the nodes, and sending it to some process (printing, for example).

16 16 Tree Traversal Traversal orders : 1. Pre-order (VLR): the node is visited before the left and right subtrees. 2. In-order (LVR): the node is visited between the left and right subtrees. 3. Post-order (LRV): the node is visited after both of the left and right subtrees.

17 17 Preorder Traversal (VLR) 1. Visit the current node 2. Traverse the left sub- tree of the current node 3. Traverse the right sub- tree of the current node Traversal order: + - A * B C D

18 18 Preorder Traversal (VLR) Traversal order: ??

19 19 Inorder Traversal (LVR) 1. Traverse the left sub- tree of the current node 2. Visit the current node 3. Traverse the right sub-tree of the current node Traversal order: A – B * C + D

20 20 Inorder Traversal (LVR) Traversal order: ??

21 21 Postorder Traversal (LRV) 1. Traverse the left sub- tree of the current node 2. Traverse the right sub-tree of the current node 3. Visit the current node Traversal order: A B C * - D +

22 22 Postorder Traversal (LRV) Traversal order: ??

23 23 Exercise 1.Inorder? 2.Preorder? 3.Postorder? - + / x/ - w y z /p vu

24 24 Representation of Algebraic Expressions A binary tree can be used to represent an algebraic expression that involves only binary arithmetic operators +, -, /, and *. The root node holds an operator, and each of its sub-trees represents either a variable (operand), or another expression. The following tree represents the expression A*(B+C) * A+ BC

25 25 Representation of Algebraic Expressions (a + b) – (c + d) ? a + b – c + d ? ((5*(y-3)) + (2/(x+7))) ?

26 26 Binary Search Tree A binary search tree is a binary tree, with the additional condition that: If a node contains a value k, then every node in its left sub-tree contain a value less than k. And every node in its right sub-tree contains a value greater than or equal to k. The above condition implies that: Every left child must have a key less than its parent Every right child must have a key greater than or equal to its parent.

27 27 Example of a Binary Search Tree 60 3585 27427092 518030406515

28 28 Inserting a New Node The operation if inserting a new node proceeds in the same sequence as the search operation until it encounters a “NULL” New node is inserted and connected to the previous node as its parent’ Example insert node 55 6060 3535 8585 2727 4242 7070 9292 5151 8080 3030 4040 6565 1515 55

29 29 Deleting a Node The node to be deleted is a leaf (has no children). Node is deleted straight from the binary tree. The node to be deleted has one child. Change the appropriate reference in the node’s parent to point to the child if the deleted node. The child along with its sub-trees, now take the place of the deleted node.

30 30 Deleting a Node The node to be deleted has two children. Step 1: Replace the node with its inorder successor. Since the node to be deleted has two children, it has a right sub-tree, and its inorder successor is the last left node in this sub-tree. Step 2: Since the inorder successor is the last left node in a sub-tree, it can not has a left child. Therefore it can have at most one child. If it has a right child, the right child will occupy the position of the inorder successor.

31 31 Example of deleting a node BEFORE deletion of node 35 60 3585 27427092 518030651540 41

32 32 Example of deleting a node AFTER deletion of node 35 60 4085 27427092 518030416515

33 33 EXERCISE Refer to exercise.doc

34 34 Binary Trees LINKED LIST IMPLEMENTATION

35 35 Linked List Implementation Data Structure: typedef struct treenode{ char entry; struct treenode *left, *right; }TreeNode; left right ** entry (char)

36 36 Linked List Implementation Create new tree: void CreateTree(TreeNode **root) { *root = NULL; } Check status of tree (empty): Boolean TreeEmpty(TreeNode *root) { return root == NULL; }

37 37 Linked List Implementation Create new node: TreeNode *MakeTreeNode(TreeEntry x) { TreeNode *p; p = (TreeNode *) malloc(sizeof(TreeNode)); if(!p) printf("Failed – not enough space"); else { p->left = NULL; p->right = NULL; p->entry = x; } return p; }

38 38 Linked List Implementation Traversal Orders of a binary tree (Preorder) void Preorder(TreeNode *root) { if(root) { printf("%c", root->entry); Preorder(root->left); Preorder(root->right); }

39 39 Linked List Implementation Traversal Orders of a binary tree (Inorder) void Inorder(TreeNode *root) { if(root) { Inorder(root->left); printf("%c", root->entry); Inorder(root->right); }

40 40 Linked List Implementation Traversal Orders of a binary tree (Postorder) void Postorder(TreeNode *root) { if(root) { Postorder(root->left); Postorder(root->right); printf("%c", root->entry); }

41 41 Linked List Implementation Tree Search TreeNode * TreeSearch(TreeNode *root, int target) { if(root) { if(target entry) root = TreeSearch(root->left,target); else if(target > root->entry) root = TreeSearch(root->right,target); } return root; }

42 42 Linked List Implementation Insert node TreeNode *InsertNode (TreeNode *root, TreeNode *newnode) { if (root == NULL) { root = newnode; root->left = root->right = NULL; } else if (newnode->entry entry) root->left = InsertNode(root->left, newnode); else root->right = InsertNode(root->right, newnode); return root; }

43 43 Linked List Implementation Delete node void DeleteNodeTree(TreeNode **p) { TreeNode *r,*q; r = *p; if(r == NULL) printf("Attempt to delete nonexistent node"); else if(r->right == NULL) { *p = r->left; free(r); } 5 4 1 8 12

44 44 Linked List Implementation Delete node void DeleteNodeTree(TreeNode **p) { else if(r->left == NULL) { *p = r->right; free(r); } 5 4 1 8 12

45 45 Linked List Implementation Delete node void DeleteNodeTree(TreeNode **p) { for(q = r->right; q->left; q = q->left) q->left = r->left; *p = r->right; free(r); } 5 4 1 8 12

46 46 Announcement Midterm Test Date : 22/09/2010 (Wednesday) Time : 12.00-1.30pm Venue : BW-3-L15/16 Topic : Chapter 1 – Chapter 4 Tips : Study quizzes and exercises

47 47 The End


Download ppt "CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM."

Similar presentations


Ads by Google