1 / 47

CHAPTER 5

CHAPTER 5. TREE. CSEB324 DATA STRUCTURES & ALGORITHM. 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

crete
Download Presentation

CHAPTER 5

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. CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM

  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. 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. Example of a Binary Tree Root Parents Leaf

  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. 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. Binary Trees -complete

  8. Binary Trees - Degenerate tree

  9. Binary Trees ARRAY IMPLEMENTATION

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

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

  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. Binary Trees - Array Issue: You must prepare maximum array element to store the binary tree database on the depth (3 level = 23 = 8 element), even if your binary tree is not a complete binary tree. Many wasted empty spaces.

  14. Binary Trees TREE TRAVERSAL

  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. Tree Traversal • Traversal orders : • Pre-order (VLR): the node is visited before the left and right subtrees. • In-order (LVR): the node is visited between the left and right subtrees. • Post-order (LRV): the node is visited after both of the left and right subtrees.

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

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

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

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

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

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

  23. - + / x / - w y z / p u v Exercise Inorder? Preorder? Postorder?

  24. * A + B C 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)

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

  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. 60 35 85 27 42 70 92 15 30 40 51 65 80 Example of a Binary Search Tree

  28. 60 35 85 55 27 42 70 92 15 30 40 51 65 80 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

  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. 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. 60 35 85 27 42 70 92 15 30 40 51 65 80 41 Example of deleting a node • BEFORE deletion of node 35

  32. 60 40 85 27 42 70 92 15 30 41 51 65 80 Example of deleting a node • AFTER deletion of node 35

  33. EXERCISE Refer to exercise.doc

  34. Binary Trees LINKED LIST IMPLEMENTATION

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

  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. 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. 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. 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. 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. Linked List Implementation Tree Search TreeNode * TreeSearch(TreeNode *root, int target) { if(root) { if(target < root->entry) root = TreeSearch(root->left,target); else if(target > root->entry) root = TreeSearch(root->right,target); }  return root; }

  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 < root->entry) root->left = InsertNode(root->left, newnode); else root->right = InsertNode(root->right, newnode);   return root; }

  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 8 12 1

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

  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 8 12 1

  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. The End

More Related