53
DATA STRUCTURES AND ALGORITHMS MODULE 4 Trees

1sharon4-1

  • Upload
    robinpt

  • View
    214

  • Download
    1

Embed Size (px)

DESCRIPTION

2sharon4-2

Citation preview

DATA STRUCTURES AND ALGORITHMS MODULE 4

DATA STRUCTURES AND ALGORITHMSMODULE 4Trees

DirectorDean (administration)Dean (academics)Dean (research)HOD (it)HOD (ce)HOD (cse)HOD (me)HOD (ec)TeachingNon-teachingTeachingNon-teachingF1F1F1NF1NF1NF1NF1F1F1F1TREENon-linear data structureFinite collection of special data items called nodesNodes of the tree are arranged in hierarchical structures to represent the parent-child relationshipNo restriction on the number of children for a node

1235476377DEGREE of a node: Number of nodes connected to any nodeIN-DEGREE of a node:Number of edges that ends at a particular nodeOUT-DEGREE: Number of edges that starts from a particular nodeDegree = in-degree+ out-degree1235476DEGREE of a node: Number of nodes connected to any nodeIN-DEGREE of a node:Number of edges that ends at a particular nodeOUT-DEGREE: Number of edges that starts from a particular nodeDegree = in-degree+ out-degree1235476Degree=3In-degree =1Out-degree=2Degree=1In-degree =1Out-degree=0TerminologiesDegree of a tree: Maximum degree of any node

1235476Degree =2Degree =1Degree =3Degree =3Degree =1Degree =1TerminologiesDegree of a tree: Maximum degree of any node

1235476Degree =2Degree =1Degree =3Degree =3Degree =1Degree =1Degree of tree = 3TerminologiesROOTFirst node which does not have any parentNode with In-degree =0

1235476ROOTTerminologiesLEAF NODE or TERMINAL NODELast node of the tree which does not have any descendantsNode with out-degree= o1235476TerminologiesLEAF NODE or TERMINAL NODELast node of the tree which does not have any descendantsNode with out-degree= o1235476Leaf nodesTerminologiesPATHSequence of edges between any two nodes1235476Path : 1-3, 3-7TerminologiesLEVEL of a nodeRoot is always assigned a level zeroLevel of any other node = Level of its parent +1123547689TerminologiesLEVEL of a nodeRoot is always assigned a level zeroLevel of any other node = Level of its parent +1

123547689Level 0Level 1Level 2Level 3TerminologiesDEPTH of a nodeNo. of nodes in the longest path from the root to any terminal nodeDepth of a node = level of a node +1Depth of a tree = largest level of a tree +1123547689Level 0Level 1Level 2Level 3TerminologiesSiblingsChildren of same parent node

1235476parentTerminologiesSiblingsChildren of same parent node

1235476siblingsTerminologiesSiblingsChildren of same parent node

1235476parentTerminologiesSiblingsChildren of same parent node

1235476siblingsTREE: DefinitionA tree is a finite set of one or more nodes such thatThere is a specially designated node called the rootThe remaining nodes are partitioned into n>=o disjoint sets T1, T2, Tn where each of these sets is a treeT1, T2, Tn are called the subtrees of the root12354768Is this a tree ??12354768Is this a tree ??A tree is a finite set of one or more nodes such thatThere is a specially designated node called the rootThe remaining nodes are partitioned into n>=o disjoint sets T1, T2, Tn where each of these sets is a treeT1, T2, Tn are called the subtrees of the root12354768Is this a tree ?? - NOA tree is a finite set of one or more nodes such thatThere is a specially designated node called the rootThe remaining nodes are partitioned into n>=o disjoint sets T1, T2, Tn where each of these sets is a treeT1, T2, Tn are called the subtrees of the root123547686BINARY TREE: DefinitionA binary tree is a finite set of one or more nodes such thatThere is a specially designated node called the rootThe remaining nodes are partitioned into N=0,1 or 2 disjoint sets T1 and T2 where each of these sets is a treeT1 and T2 are called the subtrees of the root1235476TerminologiesBinary TREEa tree which is either empty or each node can have maximum 2 childreneach node can have either 0 children, 1 child or 2 childrenLeftchild - Node on the left of the parentRightchild - Node on the right of the parent

1235476TerminologiesBinary TREEa tree which is either empty or each node can have maximum 2 childreneach node can have either 0 children, 1 child or 2 childrenLeftchild - Node on the left of the parentRightchild - Node on the right of the parent

1235476rightchildleftchildTerminologiesBinary TREEa tree which is either empty or each node can have maximum 2 childreneach node can have either 0 children, 1 child or 2 childrenLeftsubtree - Tree on the left of the parentRightsubtree - Tree on the right of the parent

1235476TerminologiesBinary TREEa tree which is either empty or each node can have maximum 2 childreneach node can have either 0 children, 1 child or 2 childrenLeftsubtree - Tree on the left of the parentRightsubtree - Tree on the right of the parent

1235476leftsubtreeTerminologiesBinary TREEa tree which is either empty or each node can have maximum 2 childreneach node can have either 0 children, 1 child or 2 childrenLeftsubtree - Tree on the left of the parentRightsubtree - Tree on the right of the parent

1235476rightsubtreeCLASSIFICATION OF BINARY TREEStrictly binary treeA binary tree in which every nonleaf node has non empty left and right sub treeA strictly binary tree with N leaves always contains 2N-1 nodes

12354CLASSIFICATION OF BINARY TREE2.Complete binary treeA binary tree with all levels except the last level contains the maximum number of possible nodes and all nodes in the last level appear as left as possible

12347612354Incomplete complete CLASSIFICATION OF BINARY TREE3.Full binary treeA binary tree that contains maximum possible number of nodes at all levelsA tree of depth d will have 2d-1 nodes1235476D=3Nodes = 23-1 =7 PROPERTIES OF BINARY TREE1.Maximum number of nodes on level l is 2l, where l>=01235476L=0, nodes = 20=1L=1, nodes = 21=2L=0, nodes = 22=4

PROPERTIES OF BINARY TREE2.Maximum number of nodes possible in a binary tree of height h is 2h-1.1235476H=3nodes= 23-1 = 7

PROPERTIES OF BINARY TREE3.Minimum number of nodes possible in a binary tree of height h is h.124H=3nodes= 3

REPRESENTATIIONS OF BINARY TREELinear or sequential representationLinked or pointer representationREPRESENTATIIONS OF BINARY TREE1.Linear or sequential representationA block of memory for an array is allocated before storing the actual tree in it.Once memory is allocated, the size of the tree is restricted as permitted by the memory.In this representation, the nodes are stored level by level, starting from the zero level where only the root node is present.

1235476A[0]1A[1]2A[2]3A[3]4A[4]5A[5]6A[6]7REPRESENTATIIONS OF BINARY TREELinear or sequential representationFollowing rules can be used to decide the location of any nodeThe root node is at index 0For any node with index I,Parent (i) is at (i 1)/2.Left child of i is at 2i+1 : Right child of i is at 2i + 2:0124365A[0]0A[1]1A[2]2A[3]3A[4]4A[5]5A[6]6REPRESENTATIIONS OF BINARY TREELinear or sequential representationLeft child of i is at 2i+1 : If 2i + 1 > n 1, then the node does not have a left child.Eg: i=3 , then 2i+1 = 7, but 7>n-1 no left childRight child of i is at 2i + 2:If 2i + 2 > n 1, then the node does have a right child.Eg: i=3 , then 2i+2 = 8, but 8>n-1 no right child

0124365A[0]0A[1]1A[2]2A[3]3A[4]4A[5]5A[6]6REPRESENTATIIONS OF BINARY TREELinear or sequential representation

012435A[0]0A[1]1A[2]2A[3]3A[4]4A[5]5A[6]-A[7]7A[8]-A[9]-A[10]-A[11]11A[12]-A[13]-A[14]-71068911121314REPRESENTATIONS OF BINARY TREELinear or sequential representation

Advantages of linear representationAny node can be accessed by calculating the indexFor each node, only data is stored no need to store any pointers

DisadvantagesOther than for full binary tree, majority of the array entries may be emptyNo way to increase the size of the tree during executionInserting or deleting a node is inefficient

REPRESENTATIIONS OF BINARY TREE2.Linked or pointer representationTree can also be defined as a finite collection of nodes where each node is divided into 3 parts containingLeft child addressInformation (data)Right child address

Leftinfo rightBINARY TREE112347632xx7xx6xx4xrootrootOperations on binary treeInsertion as a leaf nodeDeletion of a leaf nodetraversalBinary Tree CreationCreateBinaryTreeif(ROOT=NULL)New =getnode()Read new->dataNew->lc=new->rc=nullRoot=newElseRead key and itemPtr=search(root,key)If(ptr=null)Print search unsuccessfullElse if(ptr->lc=NULL and ptr->rc=NULL)Read option to insert L or RNew=getnode()New->data=itemNew->lc=new->rc=NULLIf(option=L)If(ptr->lc=NULL)Ptr->lc=newElsePrint insertion not possibleEnd ifElseIf(ptr->rc=NULL)Ptr->rc=newElsePrintinsertion not possibleEnd ifEnd ifElsePrint insertion not possible. Key node already have two children.End ifEnd ifStop

Search a keysearch(node *ptr1,int key)ptr=ptr1, p=NULLIf(ptr->data!=key)If(ptr->lchild=NULL and ptr->rchild=NULL)Return NULLEnd ifIf(ptr->lchild!=NULL)p-=search(ptr->lchild,key)If(p!=NULL)Return pEnd ifEnd ifIf(ptr->rchild!=NULL)P=search(ptr->rchild,key)Return pEnd ifElseReturn ptrEnd ifstopTraversalVisit each node in the tree exactly once.Preorder traversalVisit the root.Traverse the left subtree of the root in preorder.Traverse the right subtree of the root in preorder.Inorder traversalTraverse the left subtree of the root in inorder.Visit root.Traverse the right subtree of the root in inorder.Postorder traversalTraverse the left subtree of the root in postorder.Traverse the right subtree of the root in postorder.Visit the root.

Inorder traversal

Recursive implementationinorder(root) if (root!=NULL)Inorder(root->lchild)Visit rootInorder(root->rchild)End ifstop

Inorder traversalNon-Recursive implementationPtr=rootWhile(true)If(ptr!=NULL)Push(ptr)Ptr=ptr->lchildElse if (stack is not empty)Ptr=pop()Visit(ptr) // print ptr->dataPtr=ptr->rchildElse exitEnd ifEnd whilestopPreorder traversalRecursive implementationpreorder(root) if (root!=NULL)Visit root // print ptr->datapreorder(root->lchild)preorder(root->rchild)End ifstop

Preorder traversalNon-Recursive implementationPtr=rootWhile(true)If(ptr!=NULL)Visit(ptr) // print ptr->dataPush(ptr->rchild)Ptr=ptr->lchildElse if (stack is not empty)Ptr=pop()Else exitEnd if End whilestop

Postorder traversalRecursive implementationpostorder(root) if (root!=NULL)postorder(root->lchild)postorder(root->rchild)Visit root // print ptr->dataEnd ifstop

Postorder traversalNon-Recursive implementation

Ptr=rootWhile(true)If(ptr!=NULL)Push(ptr->rchild)Push(ptr)Ptr=ptr->lchildElse if(stack not empty) Ptr=pop()If(ptr->rchild!=0 and stack[top]=ptr->rchild)T=ptrPtr=pop()Push(T)ElsePrint ptr->dataPtr=NULLEnd if ElseexitEnd ifEnd whilestop

TYPES OF BINARY TREEExpression treeBinary search treeHeap treeThreaded binary treeHuffman treeHeight balanced tree (AVL tree)Red black treeSplay treeDecision tree