Docsity

Tree algorithm Node: A node is a structure that may contain a value or condition, or repre, Study notes of Computer Science

Toggle sidebar
bg1
26
UNIT II
3
TREE ALGORITHMS
Unit Structure
3.0 Objectives
3.1 Introduction
3.2 An Overview
3.2.1 What is a tree?
3.2.2 Systems study, Systems analysis and Systems approach
3.2.3 Characteristics of a System
3.2.4 Elements of system analysis
3.2.5 Types of Systems
3.3 System Development Life Cycle
3.3.1 Preliminary investigation
3.3.2 Determination of System requirements
3.3.3 Design of system
3.3.4 Development of software
3.3.5 Systems testing
3.3.6 Implementation, Evaluation and Maintenance
3.4 Software Crisis
3.4.1 From programmers point of view
3.4.2 From users point of view
3.5 Role of System Analyst
3.5.1 Who is System Analyst?
3.5.2 What does a System Analyst do?
3.5.3 Attributes of an effective System Analyst
3.6 Let us Sum Up
3.7 List of References
3.8 Bibliography
3.9 Unit End Exercise
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38

Partial preview of the text

Download Tree algorithm Node: A node is a structure that may contain a value or condition, or repre and more Study notes Computer Science in PDF only on Docsity!

UNIT II

TREE ALGORITHMS Unit Structure 3.0 Objectives 3.1 Introduction 3.2 An Overview 3.2.1 What is a tree? 3.2.2 Systems study, Systems analysis and Systems approach 3.2.3 Characteristics of a System 3.2.4 Elements of system analysis 3.2.5 Types of Systems 3.3 System Development Life Cycle 3.3.1 Preliminary investigation 3.3.2 Determination of System requirements 3.3.3 Design of system 3.3.4 Development of software 3.3.5 Systems testing 3.3.6 Implementation, Evaluation and Maintenance 3.4 Software Crisis 3.4.1 From programmers point of view 3.4.2 From users point of view 3.5 Role of System Analyst 3.5.1 Who is System Analyst? 3.5.2 What does a System Analyst do? 3.5.3 Attributes of an effective System Analyst 3.6 Let us Sum Up 3.7 List of References 3.8 Bibliography 3.9 Unit End Exercise

Fundamental of Algorithms

3.0 OBJECTIVES

What is the objective of algorithms?

  1. Algorithms define how a particular task is to be performed. They provide computers with instructions. They tell machines on assembly lines how specific parts should be put together. Or they evaluate application forms and recommend the best candidates.
  2. Provides a hierarchical way of storing data. Reflects structural relationship in a data set. Allows insertion, deletion and searching operations that yield results faster than an array or linked list. Provides a flexible way to hold and move data.
  3. Hierarchical Structure: Trees are used to model hierarchical structures, such as the file system in a computer or the organizational chart in a company. The tree structure allows for a natural representation of parent-child relationships, making it easy to understand and visualize the data.

3.2 INTRODUCTION OF TREE ALGORITHMS?

A tree is a non-linear abstract data type with a hierarchy-based structure. It consists of nodes (where the data is stored) that are connected via links. The tree data structure stems from a single node called a root node and has subtrees connected to the root. Important Terms Following are the important terms with respect to tree.  Path − Path refers to the sequence of nodes along the edges of a tree.  Root − The node at the top of the tree is called root. There is only one root per tree and one path frocm the root node to any node.

Fundamental of Algorithms Binary Trees Binary Trees are general trees in which the root node can only hold up to maximum 2 subtrees: left subtree and right subtree. Based on the number of children, binary trees are divided into three types. Full Binary Tree  A full binary tree is a binary tree type where every node has either 0 or 2 child nodes. Complete Binary Tree  A complete binary tree is a binary tree type where all the leaf nodes must be on the same level. However, root and internal nodes in a complete binary tree can either have 0, 1 or 2 child nodes. Perfect Binary Tree  A perfect binary tree is a binary tree type where all the leaf nodes are on the same level and every node except leaf nodes have 2 children. Binary Search Trees Binary Search Trees possess all the properties of Binary Trees including some extra properties of their own, based on some constraints, making them more efficient than binary trees. The data in the Binary Search Trees (BST) is always stored in such a way that the values in the left subtree are always less than the values in the root node and the values in the right subtree are always greater than the values in the root node, i.e. left subtree < root node ≤ right subtree.

Tree Algorithms Advantages of BST  Binary Search Trees are more efficient than Binary Trees since time complexity for performing various operations reduces.  Since the order of keys is based on just the parent node, searching operation becomes simpler.  The alignment of BST also favors Range Queries, which are executed to find values existing between two keys. This helps in the Database Management System. Disadvantages of BST The main disadvantage of Binary Search Trees is that if all elements in nodes are either greater than or lesser than the root node, the tree becomes skewed. Simply put, the tree becomes slanted to one side completely. This skewness will make the tree a linked list rather than a BST, since the worst case time complexity for searching operation becomes O(n). To overcome this issue of skewness in the Binary Search Trees, the concept of Balanced Binary Search Trees was introduced. Balanced Binary Search Trees Consider a Binary Search Tree with ‘m’ as the height of the left subtree and ‘n’ as the height of the right subtree. If the value of (m-n) is equal to 0,1 or -1, the tree is said to be a Balanced Binary Search Tree. The trees are designed in a way that they self-balance once the height difference exceeds 3. Binary Search Trees use rotations as self-balancing algorithms. There are four different types of rotations: Left Left, Right Right, Left Right, Right Left.

Tree Algorithms T1, ..., Tn, where each of these sets is a tree. We call T1, ..., Tn are the subtrees of the root. A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the top of the hierarchy. Each node can have at most one link coming into it. The node where the link originates is called the parent node. The root node has no parent. The links leaving a node (any number of links are allowed) point to child nodes. Trees are recursive structures. Each child node is itself the root of a subtree. At the bottom of the tree are leaf nodes, which have no children. Advantages of trees Trees are so useful and frequently used, because they have some very serious advantages:  Trees reflect structural relationships in the data  Trees are used to represent hierarchies  Trees provide an efficient insertion and searching  Trees are very flexible data, allowing to move sub trees around with minimum effort Introduction Terminology In a Tree, Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure. Example

Fundamental of Algorithms

1. Root In a tree data structure, the first node is called as Root Node. Every tree must have root node. We can say that root node is the origin of tree data structure. In any tree, there must be only one root node. We never have multiple root nodes in a tree. In above tree, A is a Root node 2. Edge In a tree data structure, the connecting link between any two nodes is called as EDGE. In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges. 3. Parent In a tree data structure, the node which is predecessor of any node is called as PARENT NODE. In simple words, the node which has branch from it to any other node is called as parent node. Parent node can also be defined as "The node which has child / children". e.g., Parent (A,B,C,D). 4. Child In a tree data structure, the node which is descendant of any node is called as CHILD Node. In simple words, the node which has a link from its parent node is called as child node. In a tree, any parent node can have any number of child nodes. In a tree, all the nodes except root are child nodes. e.g., Children of D are (H, I,J). 5. Siblings In a tree data structure, nodes which belong to same Parent are called as SIBLINGS. In simple words, the nodes with same parent are called as Sibling nodes. Ex: Siblings (B,C, D) 6. Leaf In a tree data structure, the node which does not have a child (or) node with degree zero is called as LEAF Node. In simple words, a leaf is a node with no child.In a tree data structure, the leaf nodes are also called as External Nodes. External node is also a node with no child. In a tree, leaf node is also called as 'Terminal' node. Ex: (K,L,F,G,M,I,J)

Fundamental of Algorithms

12. Path In a tree data structure, the sequence of Nodes and Edges from one node to another node is called as PATH between that two Nodes. Length of a Path is total number of nodes in that path. In below example the path A - B - E - J has length 4. 13. Sub Tree In a tree data structure, each child from a node forms a subtree recursively. Every child node will form a subtree on its parent node. Tree Representations A tree data structure can be represented in two methods. Those methods are as follows... 1.List Representation

  1. Left Child - Right Sibling Representation Consider the following tree... 1. List Representation In this representation, we use two types of nodes one for representing the node with data and another for representing only references. We start with

Tree Algorithms a node with data from root node in the tree. Then it is linked to an internal node through a reference node and is linked to any other node directly. This process repeats for all the nodes in the tree. The above tree example can be represented using List representation as follows... Fig: List representation of above Tree Fig: Possible node structure for a tree of degree k

2. Left Child - Right Sibling Representation In this representation, we use list with one type of node which consists of three fields namely Data field, Left child reference field and Right sibling reference field. Data field stores the actual value of a node, left reference field stores the address of the left child and right reference field stores the address of the right sibling node. Graphical representation of that node is as follows... In this representation, every node's data field stores the actual value of that node. If that node has left child, then left reference field stores the address of that left child node otherwise that field stores NULL. If that node has right sibling then right reference field stores the address of right sibling node otherwise that field stores NULL. The above tree example can be represented using Left Child - Right Sibling representation as follows...

Tree Algorithms In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every node should have exactly two children or none. That means every internal node must have exactly two children. A strictly Binary Tree can be defined as follows... A binary tree in which every node has either two or zero number of children is called Strictly Binary Tree. Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree

1. Complete Binary Tree In a binary tree, every node can have a maximum of two children. But in strictly binary tree, every node should have exactly two children or none and in complete binary tree all the nodes must have exactly two children and at every level of complete binary tree there must be 2 level number of nodes. For example at level 2 there must be 2^2 = 4 nodes and at level 3 there must be 2^3 = 8 nodes. A binary tree in which every internal node has exactly two children and all leaf nodes are at same level is called Complete Binary Tree. Complete binary tree is also called as Perfect Binary Tree 2. Extended Binary Tree A binary tree can be converted into Full Binary tree by adding dummy nodes to existing nodes wherever required. The full binary tree obtained by adding dummy nodes to a binary tree is called as Extended Binary Tree. Abstract Data Type Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called left subtree and right subtree. ADT contains specification for the binary tree ADT. Structure Binary_Tree (abbreviated BinTree) is objects: a finite set of nodes either empty or consisting of a root node, left

Fundamental of Algorithms Binary_Tree, and right Binary_Tree. Functions: for all bt, bt1, bt2 BinTree, item element Bintree Create()::= creates an empty binary tree Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right subtree is bt2, and whose root node contains the data item Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of bt element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt

2. Linked Representation We use linked list to represent a binary tree. In a linked list, every node consists of three fields. First field, for storing left child address, second for storing actual data and third for storing right child address. In this linked list representation, a node has the following structure... Type def struct node *tree_pointer; typedef struct node { int data; tree_pointer left_child, right_child;}; Binary Tree Traversals When we wanted to display a binary tree, we need to follow some order in which all the nodes of that binary tree must be displayed. In any binary

Fundamental of Algorithms the right most child in the tree so we stop the process. That means here we have visited in the order of I - D - J - B - F - A - G - K

  • C - H using In-Order Traversal. In-Order Traversal for above example of binary tree is I - D - J - B - F - A
  • G - K - C – H Algorithm Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Visit root node. Step 3 − Recursively traverse right subtree. void inorder(tree_pointer ptr) / inorder tree traversal / Recursive { if (ptr) { inorder(ptr->left_child); printf(“%d”, ptr->data); indorder(ptr-

right_child); } **}

  1. Pre - Order Traversal ( root - leftChild - rightChild )** In Pre-Order traversal, the root node is visited before left child and right child nodes. In this traversal, the root node is visited first, then its left child and later its right child. This pre-order traversal is applicable for every root node of all subtrees in the tree. In the above example of binary tree, first we visit root node 'A' then visit its left child 'B' which is a root for D and F. So we visit B's left child 'D' and again D is a root for I and J. So we visit D's left child'I' which is the left most child. So next we go for visiting D's right child 'J'. With this we have completed root, left and right parts of node D and root, left parts of node B. Next visit B's right child'F'. With this we have completed root and left parts of node A. So we go for A's right child 'C' which is a root node for G and H. After visiting C, we go for its left child 'G' which is a root for node K. So next we visit left of G, but it does not have left child so we go for G's right child 'K'. With this we have completed node C's root and left parts. Next visit C's right child 'H' which is the right most child in the tree. So we stop the process. That means here we have visited in the order of A-B-D-I-J-F-C-G-K-H using Pre-Order Traversal. Algorithm Until all nodes are traversed − Step 1 − Visit root node. Step 2 − Recursively traverse left subtree. Step 3 − Recursively traverse

Tree Algorithms right subtree. void preorder(tree_pointer ptr) / preorder tree traversal / Recursive { if (ptr) { printf(“%d”, ptr->data); preorder(ptr->left_child); preorder(ptr-

right_child); } }

2. Post - Order Traversal ( leftChild - rightChild - root ) In Post-Order traversal, the root node is visited after left child and right child. In this traversal, left child node is visited first, then its right child and then its root node. This is recursively performed until the right most node is visited. Here we have visited in the order of I - J - D - F - B - K - G - H - C - A using Post-Order Traversal. Algorithm Until all nodes are traversed − Step 1 − Recursively traverse left subtree. Step 2 − Recursively traverse right subtree. Step 3 − Visit root node. void postorder(tree_pointer ptr) / postorder tree traversal / Recursive { if (ptr) { postorder(ptr->left_child); postorder(ptr->right_child); printf(“%d”, ptr-

data); } } Arithmetic Expression Using BT inorder traversal A / B * C * D + E infix expression preorder traversal

      • / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal
    • E * D / C A B

Tree Algorithms ptr = deleteq(&front, rear); if (ptr) { printf(“%d”, ptr->data); if (ptr->left_child) addq(front, &rear, ptr->left_child); if (ptr->right_child) addq(front, &rear, ptr->right_child); } else break; } } Level order Traversal is implemented with circular queue. In this order, we visit the root first, then root’s left child followed by root’s right child. We continue in this manner, visiting the nodes at each new level from left most to right most nodes. We begin by adding root to the queue. The function operates by deleting the node at the front of the queue, printing out the node’s data field, and adding the node’s left and right children to the queue. The level order traversal for above arithmetic expression is + * E * D / C A B. Binary Search Trees Binary Search Tree Representation Binary Search tree exhibits a special behavior. A node's left child must have value less than its parent's value and node's right child must have value greater than it's parent value. We're going to implement tree using node object and connecting them through references. Definition: A binary search tree (BST) is a binary tree. It may be empty. If it is not empty,then all nodes follows the below mentioned properties −  Every element has a unique key.  The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree.  The keys in a nonempty right subtree larger than the key in the root of subtree.

Fundamental of Algorithms  The left and right subtrees are also binary search trees. left sub-tree and right sub-tree and can be defined as − left_subtree (keys) ≤ node (key) ≤ right_subtree (keys) Fig: Example Binary Search Trees ADT for Dictionary: BST Basic Operations The basic operations that can be performed on binary search tree data structure, are following −  Search − search an element in a binary search tree.  Insert − insert an element into a binary search tree / create a tree.  Delete − Delete an element from a binary search tree.  Height -- Height of a binary search tree. Searching a Binary Search Tree Let an element k is to search in binary search tree. Start search from root node of the search tree. If root is NULL, search tree contains no nodes and search unsuccessful. Otherwise, compare k with the key in the root. If k equals the root’s key, terminate search, if k is less than key value, searchelement k in left subtree otherwise search element k in right subtree. The function search recursively searches the subtrees. Algorithm:Recursive search of a Binary Search Tree tree_pointer search(tree_pointer root, int key) { /* return a pointerto the node that contains key. If there is no such node, return NULL */ if (!root) return NULL; if (key == root->data) return root; if (key < root->data) return search(root->left_child, key); return search(root->right_child,key); }