Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 25
Unit 4
Trees and Graph Algorithms
What is a Tree? A tree is a data structure similar to a linked list but instead of each node pointing simply to the next node in a linear fashion, each node points to a number of nodes. nonlinear data structure Represents the hierarchical nature of a structure in a graphical form Contd… The depth of a node is the length of the path from the root to the node (depth of G is 2, A – C – G). In the previous example, the height of B is 2 (B – F – J). For a given tree, depth and height returns the same value. Contd… If every node in a tree has only one child (except leaf nodes) then we call such trees skew trees. If every node has only left child then we call them left skew trees. Similarly, if every node has only right child then we call them right skew trees. Binary Trees A tree is called binary tree if each node has zero child, one child or two children. Empty tree is also a valid binary tree Types of Binary Trees Strict Binary Tree:each node has exactly two children or no children. Full Binary Tree each node has exactly two children and all leaf nodes are at the same level. Complete Binary Tree A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. Properties of Binary Trees Contd… The number of nodes n in a full binary tree is 2 h+1 – 1. Since, there are h levels we need to add all nodes at each level [2 0 + 2 1+ 2 2 + ··· + 2 h = 2 h+1 – 1]. • The number of nodes n in a complete binary tree is between 2 h (minimum) and 2 h+1 – 1 (maximum). The number of leaf nodes in a full binary tree is 2 h . Operations on Binary Trees Basic Operations ◦ Inserting an element into a tree ◦ Deleting an element from a tree ◦ Searching for an element ◦ Traversing the tree Binary Tree Traversals The process of visiting all nodes of a tree is called tree traversal. ◦ Preorder Traversal ◦ Inorder Traversal ◦ Postorder Traversal PreOrder Traversal Visit the root. Traverse the left subtree in Preorder. Traverse the right subtree in Preorder. Example: The nodes of tree would be visited in the order: 1 2 4 5 3 6 InOrder Traversal Traverse the left subtree in Inorder. Visit the root. Traverse the right subtree in Inorder. Example: The nodes of tree would be visited in the order: 4 2 5 1 6 3 7 PostOrder Traversal Traverse the left subtree in Postorder. Traverse the right subtree in Postorder. Visit the root. Example: The nodes of the tree would be visited in the order: 4 5 2 6 7 3 1 Level Order Traversal Visit the root. While traversing level (, keep all the elements at level ( + 1 in queue. Go to the next level and visit all the nodes at that level. Repeat this until all levels are completed. Example: The nodes of the tree are visited in the order: 1 2 3 4 5 6 7 finding maximum element in binary tree Binary Search Trees (BSTs) The main use of this representation is for searching In this representation we impose restriction on the kind of data a node can contain. ◦ The left subtree of a node contains only nodes with keys less than the nodes key. ◦ The right subtree of a node contains only nodes with keys greater than the nodes key. ◦ Both the left and right subtrees must also be binary search trees. Finding an Element in bst