1 / 49

Data Structure

Data Structure. Data Structure:. Use / Purpose:. Store marks of 1 student. int variable. Store marks of 50 students. Array. Implement Recursion. Stack. Simulate a Ticket Counter. Queue. Store Hierarchical Relationship. ?. Non-Linear. Tree. DS. Ramesh. Suresh. Paresh. Kamlesh.

laird
Download Presentation

Data Structure

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. Data Structure Data Structure: Use / Purpose: Store marks of 1 student int variable Store marks of 50 students Array Implement Recursion Stack Simulate a Ticket Counter Queue Store Hierarchical Relationship ?

  2. Non-Linear Tree DS Ramesh Suresh Paresh Kamlesh Ajay Rahul Raj Ankit Hardik Family Tree

  3. Example Tree CEO Manager-1 Manager-2 Manager-3 Employee-1 Employee-2 Employee-3 Employee-4 Employee-5 Rank in an Organization / Organization Hierarchy

  4. Example Tree / bin usr home etc dev ls vi bash Steve Joe hd0 hd1 Linux File System

  5. Edge/Branch Basic Terminologies Root Tree Node 120 Level-0 No. of Children Parent Degree of Node Child 30 60 90 250 40 Level-1 Siblings 10 70 100 20 200 50 80 Level-2 Leaf 5 Degree of Tree = Longest path from root to leaf Max Degree out of all nodes. Height = 3 h = lmax + 1

  6. Trees Basic Terms/Terminologies: Node: Basic component of a tree which stores actual data. Parent of a Node: Immediate predecessor of a node. Child of a Node: Immediate successor of a node. Sibling: Nodes which have the same parent. Root Node: Special node which does not have any parent. Leaf Node / Terminal Node / External Node: Node which does not have any child.

  7. Trees Basic Terms/Terminologies: Level of a Node: Rank of a node in the hierarchy. Example: Root node has level 0. If a node is at level l, Its child is at level l+1 and, Its parent is at level l-1. Height / Depth of a Tree: Maximum number of nodes that is possible in a path starting from root node to a leaf node. Relationship between height and level is, h = lmax + 1 Degree of a Node: Number of children of a node. Degree of a Tree: Maximum degree out of all nodes.

  8. Answer the questions. Tree A Height of the Tree: 4 Level(H): 2 B C D Level(C): 1 Level(K): 3 E F G H I J Degree(C): 1 Degree of Tree: 3 K L M Parent(M): H Sibling(I): J Child(B): E,F,G Root Node: A Leaf Nodes: E,K,L,G,M,I,J

  9. How the tree is formed / What does it compose of? Tree A T2 T3 T1 B C D E F G H I J K L M

  10. A A B C D B C G E F G H I J D E F K L M H I J K L M Tree Left Child Right Child Left Subtree Right Subtree Node may have at most 2 children. Node may have any number of children. General / Normal Tree. Binary Tree. (More important and frequently used.)

  11. Trees Binary Tree: A tree which contains, A special node called the root and, Remaining nodes form, Two (2) disjoint binary trees T1 and T2 which are called, Left sub-tree and Right sub-tree respectively. OR A special tree where, A node can have at most 2 children called, Left Child and Right Child.

  12. A A B C B C D E G D E F F G H I J K L M N O Binary Tree Binary Tree Full Binary Tree

  13. A B C A D E F A B C B C G H I G D E F G D E F H I J K L H I J K L Binary Tree Binary Tree Binary Tree Complete Binary Tree

  14. Binary Tree Situations of Binary Tree: Full Binary Tree: A binary tree is a full binary tree if, It contains the maximum possible number of nodes at all levels. Complete Binary Tree: A binary tree is a complete binary tree if, All its levels, except possibly the last level, have the maximum number of nodes and, All the nodes in the last level appear as fast left as possible. Note: Every ‘Full Binary Tree’ is always a ‘Complete Binary Tree’ but, Every ‘Complete Binary Tree’ might not be a ‘Full Binary Tree’.

  15. A A A B C B B A A A B C D B C B C D E F D E F Identify the situations of Binary Tree. Binary Tree A Full Binary Tree Full Binary Tree Complete Binary Tree Binary Tree Not a Binary Tree General Tree Binary Tree Complete Binary Tree

  16. A A A B C B C B C D E D E D E F G Properties / characteristics Binary Tree Similarities?

  17. Properties of Binary Tree 1. In any Binary tree, maximum number of nodes on level l is: 2l Level Max. Nodes Formula 20 0 1 A 1 2 B C 21 4 2 22 D E F G 23 3 8 H I J K L M N O 2l l

  18. 1. In any Binary tree, maximum number of nodes on level l is: 2l Proof: Properties of Binary Tree It can be proved using method of induction by taking induction on the value of l. Proving for level, l = 0: At level l = 0, there is only 1 node, i.e. Root node. So maximum nodes on l = 0 is 2l = 20 = 1 (1) Assuming it to be true for any level, l = i: Maximum number of nodes at level i = 2i (2) Proving for level, l = i + 1: From each node at level i, There can be at most (maximum) 2 nodes at level i + 1 because, The degree of binary tree is 2 and it is a Full Binary Tree. (3) Using (2), Maximum number of nodes at level i + 1 = 2i x 2 = 2i+1 Hence proved due to (1), (2), (3).

  19. Properties of Binary Tree 2h – 1 2. Maximum number of nodes possible in a binary tree of height h is: Height Max. Nodes Formula 21 – 1 A 1 1 A 2 3 22 – 1 A B C B C 7 3 23 – 1 D E F G 2h – 1 h

  20. 2. Maximum number of nodes possible in a binary tree of height h is: 2h – 1 Proof: Properties of Binary Tree Maximum number of nodes are possible in a binary tree only if, It is a Full Binary Tree which means, Each level of the tree has maximum number of nodes. So assuming that levels are from 0 to lmax, then Maximum number of nodes in the entire binary tree, n, is, Max. no. of nodes at level 0 + Max. no. of nodes at level 1 + … + Max. no. of nodes at level lmax. n = 20 + 21 + 22 + … + 2lmax [Geometric Series] [Because of Property-1] lmax a(rn – 1) Formula of Geometric Series = n = 2k r - 1 k = 0 n = 20 (2lmax+1 – 1) 1 (2lmax+1 – 1) = 2lmax+1 – 1 = 2h – 1 = 2 - 1 1 Hence proved.

  21. Properties of Binary Tree h 3. Minimum number of nodes possible in a binary tree of height h is: Height Min. Nodes Formula 1 A 1 1 A A 2 2 2 B B A A 3 3 3 B B h h C C

  22. 3. Minimum number of nodes possible in a binary tree of height h is: h Proof: Properties of Binary Tree A binary tree has minimum number of nodes only if, Each level has minimum number of nodes and, Skew Binary Trees Minimum nodes to make a level is 1. OR When every parent has only 1 child. So assuming that levels are from 0 to lmax, then Minimum number of nodes in the entire binary tree, n, is, Min. no. of nodes at level 0 + Min. no. of nodes at level 1 + … + Min. no. of nodes at level lmax. n = 1 + 1 + 1 + … + 1 [Add 1 for 0 to lmax] n = lmax + 1 n = h Hence proved.

  23. Question: In any binary tree, if lmax = 5, the find out: Properties of Binary Tree 1) Minimum number of nodes in the entire binary tree. 2) Maximum number of nodes in the entire binary tree. 3) Maximum number of nodes on level 4. 4) Minimum number of nodes on level 4. Answer: If lmax = 5, then height of binary tree h = lmax + 1 So, h = 5 + 1 = 6. 1) Minimum number of nodes in the entire binary tree of height h = h = 6 2) Maximum number of nodes in the entire binary tree of height h = 2h – 1 = 26 – 1 = 64 – 1 = 63 3) Maximum number of nodes on level 4 = 2l = 24 = 16 4) Minimum number of nodes on level 4 = 1

  24. A B A A B C B C D E F G Properties of Binary Tree 4. For any binary tree, if n is the number of nodes and e is the number of edges, then : n = e + 1 No. of nodes (n) No. of edges (e) n = e + 1 1 = 0 + 1 1 0 A 2 1 2 = 1 + 1 2 3 3 = 2 + 1 6 7 7 = 6 + 1 n-1 n n = (n-1) + 1

  25. 4. For a binary tree, if n is the number of nodes and e is the number of edges, then n = e + 1. Proof: Properties of Binary Tree It can be proved using method of induction. Proving for n = 1: If there is only one node, then it can be the ‘Root’ node and hence, Number of edges will be 0. So, n = 1 and e = 0 and hence n = e + 1. (1) Assuming it to be true for any n = n’ and e = e’: n’ = e’ + 1 (2) Proving for n = n’ + 1: (n’ + 1) means 1 extra node than n’ and, If 1 node is added in the binary tree, then 1 edge will also be added. n’ + 1 = (e’ + 1) + 1 Hence proved due to (1), (2), (3). From eq.(2), e’ = n’ – 1 n’ + 1 = ((n’ – 1) + 1) + 1 n’ + 1 = ((n’ + 1) - 1) + 1 (3)

  26. A B A A B C B C D E F G Properties of Binary Tree 5. For any binary tree, if n0 is the number of leaf nodes with degree 0 and n2 is the number of internal nodes with degree 2, then: n0 = n2 + 1. n0 n2 n0 = n2 + 1 1 = 0 + 1 1 0 A 1 0 1 = 0 + 1 1 2 2 = 1 + 1 3 4 4 = 3 + 1

  27. 5. For any binary tree, if n0 is the number of leaf nodes with degree 0 and n2 is the number of internal nodes with degree 2, then n0 = n2 + 1. Proof: Properties of Binary Tree Different types of nodes possible in a binary tree can be: n0: Number of nodes with degree 0 / Number of leaf nodes. n1: Number of nodes with degree 1. n2: Number of nodes with degree 2. If n is the total number of nodes in any binary tree, then, n = n0 + n1 + n2 (1) If e is the number of edges, then, (2) e = (n0 x 0) + (n1 x 1) + (n2 x 2) e = n1 + 2n2 Moreover, we already know that, n = e + 1 (3) Putting the value of eq.(2) in eq.(3) n = n1 + 2n2 + 1 (4) Comparing eq.(1) and eq.(4) n0 + n1 + n2 = n1 + 2n2 + 1 Hence proved. n0 = n2 + 1

  28. Binary Tree Representations of Binary Tree: 2 common ways/methods to represent/store a Binary Tree in memory: Using an Array. Linear/Sequential representation. Using a Linked List. Linked representation. Whatsoever be the representation, the main objective is: We should be able to get the tree back whenever required and, We should be able to ‘do processing’ / ‘extract meaningful information’ / ‘answer all the questions’ from the structure.

  29. A B C D E F G H I J Linear Representation / Using an Array Representations of Binary Tree How to store so that we can get it back? Leave blank spaces for children possible in future. Array A A B C E I D F G J H A B C E - D F G - - - - H I J

  30. A B C D E F G H I Linear Representation / Using an Array Representations of Binary Tree Questions? Size of array, n? Max. nodes = 2h – 1 = 24 – 1 = 15 Rules: 1. The root node is at index/location 1. 2. For any node with index i, (i / 2) a. Parent(i) = (2 * i) b. LChild(i) = If (2 * i) > n, then i has no left child. (2 * i + 1) c. RChild(i) = If (2 * i + 1) > n, then i has no right child. Array A A B C E - D - F G H - - - I -

  31. A B C D E F Linked Representation / Using Linked List Representations of Binary Tree Node Structure DATA RC LC 2000 A 4000 1000 3000 B X 5000 C 6000 2000 4000 X D X X E X X F X 3000 5000 6000

  32. A B C D E Sequential Representation v/s Linked Representation Representations of Binary Tree Sequential / Linear Representation A B C - D - E v/s 2000 A 4000 1000 3000 B X X C 5000 2000 4000 Linked Representation X D X X E X 3000 5000

  33. Representations of Binary Tree Sequential/Linear Representation: Advantages: Any node can be accessed from, Any other node by just, Calculating the index using some rules/formulas. Need to store only the data and no need to store, Any links/pointers to the successor/predecessor nodes. Disadvantages: Majority of the array entries may be, Empty except for a, Full binary tree. Allows only static representation and hence, Not possible to enhance (grow/shrink) the tree structure on demand.

  34. Representations of Binary Tree Linked Representation: Advantages: Allows dynamic memory allocation and hence, The size of the tree can increase/decrease on demand. Disadvantages: Along with the data, Also need to store addresses of the predecessors/successors and hence, More memory is required. Some links/pointers/addresses will be having, NULL values which will be, Wastage of memory. In a linked representation of a binary tree, if there are n number of nodes, then the number of null links = n + 1.

  35. A B C Traversals on a Binary Tree Binary Tree Question: Tell all the data/elements present in the binary tree. PreOrder Converse PreOrder A B C C B A [R Lc Rc] [Rc Lc R] InOrder Converse InOrder B A C [Lc R Rc] C A B [Rc R Lc] PostOrder Converse PostOrder B C A [Lc Rc R] [R Rc Lc] A C B

  36. 20 10 30 Traversals on a Binary Tree Binary Tree PreOrder Converse PreOrder 20 10 30 30 10 20 [R Lc Rc] [Rc Lc R] InOrder Converse InOrder 10 20 30 [Lc R Rc] 30 20 10 [Rc R Lc] PostOrder Converse PostOrder 10 30 20 [Lc Rc R] [R Rc Lc] 20 30 10

  37. Traversals on a Binary Tree PreOrder A R Lc Rc B C A B C Have we visited each and every element? D E No.

  38. TR PreOrder R TL Lc Rc Traversals on a Binary Tree (A) R (B) R (D) R A Lc - - Lc Lc Rc B C (E) R Rc Lc - D E - Rc Rc (C) R - Lc PreOrder: A - B D E C Rc

  39. TR InOrder TL Lc R Rc Traversals on a Binary Tree Lc - R (D) Lc Rc - Lc A R (B) Lc - Rc (E) R B C - Rc A R D E Lc - Rc R (C) InOrder: D B E A C Rc -

  40. PostOrder TL Lc TR Rc R Traversals on a Binary Tree Lc - Rc - Lc R (D) Lc - Lc A - Rc Rc (E) R B C R (B) D E Lc - Rc Rc - R (C) (A) R PostOrder: D E B C A

  41. A B C D E F G A A B B C C D E D Traversals on a Binary Tree PreOrder: A B D E C F G D B E A F C G InOrder: D E B F G C A PostOrder: PreOrder: A B C D PreOrder: A B D C E A B C D InOrder: D B A C E InOrder: D C B A PostOrder: D B E C A PostOrder:

  42. A B C Algorithm: PreOrder(Root) 1. Visit the root node. 2. Traverse the left sub-tree in PreOrder. 3. Traverse the right sub-tree in PreOrder. R TL TR Steps: If(Root != NULL) Visit(Root) //print Root->Data PreOrder(Root->LC) PreOrder(Root->RC) EndIf Root A X B X X C X Visit(A) (A) Visit(B) (B) PreOrder(A) PreOrder(B) PreOrder(NULL) - - PreOrder(NULL) Visit(C) (C) PreOrder: - PreOrder(NULL) PreOrder(C) A B C PreOrder(NULL) -

  43. Traversals on a Binary Tree Algorithm: PreOrder Input: Root: Pointer to the root node of the binary tree. Output: Visiting all the nodes in PreOrder manner. Data Structure: Binary Tree implemented using a Linked structure.

  44. Algorithm: PreOrder Steps: If(Root != NULL) Visit(Root) //print Root->Data PreOrder(Root->LC) PreOrder(Root->RC) EndIf

  45. Traversals on a Binary Tree Algorithm: InOrder Input: Root: Pointer to the root node of the binary tree. Output: Visiting all the nodes in InOrder manner. Data Structure: Binary Tree implemented using a Linked structure.

  46. Algorithm: InOrder Steps: If(Root != NULL) InOrder(Root->LC) Visit(Root) //print Root->Data InOrder(Root->RC) EndIf

  47. Traversals on a Binary Tree Algorithm: PostOrder Input: Root: Pointer to the root node of the binary tree. Output: Visiting all the nodes in PostOrder manner. Data Structure: Binary Tree implemented using a Linked structure.

  48. Algorithm: PostOrder Steps: If(Root != NULL) PostOrder(Root->LC) PostOrder(Root->RC) Visit(Root) //print Root->Data EndIf

  49. P Q R S T U V W X Traversals on a Binary Tree PreOrder: P Q S T R U V W X InOrder: S Q T P U R W V X PostOrder: S T Q U W X V R P

More Related