470 likes | 579 Views
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
E N D
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 • every other node can be reached by following a unique sequence of consecutive arcs
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
Example of a Binary Tree Root Parents Leaf
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
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.
Binary Trees ARRAY IMPLEMENTATION
Binary Trees - Array Assume you have the below diagram:
Binary Trees - Array We can represent this binary tree using an array, arr.
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
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.
Binary Trees TREE TRAVERSAL
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).
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.
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
Preorder Traversal (VLR) Traversal order: ??
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
Inorder Traversal (LVR) Traversal order: ??
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 +
Postorder Traversal (LRV) Traversal order: ??
- + / x / - w y z / p u v Exercise Inorder? Preorder? Postorder?
* 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)
Representation of Algebraic Expressions • (a + b) – (c + d) ? • a + b – c + d ? • ((5*(y-3)) + (2/(x+7))) ?
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.
60 35 85 27 42 70 92 15 30 40 51 65 80 Example of a Binary Search Tree
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
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.
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.
60 35 85 27 42 70 92 15 30 40 51 65 80 41 Example of deleting a node • BEFORE deletion of node 35
60 40 85 27 42 70 92 15 30 41 51 65 80 Example of deleting a node • AFTER deletion of node 35
EXERCISE Refer to exercise.doc
Binary Trees LINKED LIST IMPLEMENTATION
Linked List Implementation entry Data Structure: typedef struct treenode{ char entry; struct treenode *left, *right; }TreeNode; (char) right left * *
Linked List Implementation Create new tree: void CreateTree(TreeNode **root) { *root = NULL; } Check status of tree (empty): Boolean TreeEmpty(TreeNode *root) { return root == NULL; }
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; }
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); } }
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); } }
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); } }
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; }
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; }
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
Linked List Implementation Delete node void DeleteNodeTree(TreeNode **p) { else if(r->left == NULL) { *p = r->right; free(r); } 5 4 8 12 1
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
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