Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure Use / Purpose: Data Structure: Store marks of 1 student

Similar presentations


Presentation on theme: "Data Structure Use / Purpose: Data Structure: Store marks of 1 student"— Presentation transcript:

1 Data Structure Use / Purpose: Data Structure: 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 Tree Non-Linear DS Ramesh Suresh Paresh Kamlesh Ajay Rahul Raj Ankit
Hardik Family Tree

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

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

5 Tree Edge/Branch Basic Terminologies Root 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 Degree of Tree = 5 Longest path from root to leaf Max Degree out of all nodes. Height = 3 h = lmax + 1

6 Trees Basic Terms/Terminologies: Node: Parent of a 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 Tree Answer the questions. 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?
B C D E F G H I J K L M

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

11 Trees Binary Tree: A tree which contains, OR A special tree where,
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 Binary Tree B A C D E F G B A C D E F G H I J K M L N O Binary Tree
Full Binary Tree

13 Binary Tree B A C D E F G H I J K L B A C D E F G H I B A C D E F G H
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 Identify the situations of Binary Tree.
C B A B A A Full Binary Tree Full Binary Tree Complete Binary Tree Binary Tree B A C D E F B A C D E F B A C D Not a Binary Tree General Tree Binary Tree Complete Binary Tree

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

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

18 Properties of Binary Tree
1. In any Binary tree, maximum number of nodes on level l is: 2l Proof: 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. Using (2), Maximum number of nodes at level i + 1 = 2i x 2 = 2i+1 (3) Hence proved due to (1), (2), (3).

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

20 Properties of Binary Tree
2. Maximum number of nodes possible in a binary tree of height h is: 2h – 1 Proof: 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 = … + 2lmax [Because of Property-1] [Geometric Series] lmax a(rn – 1) n = 2k Formula of Geometric Series = 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
3. Minimum number of nodes possible in a binary tree of height h is: h Height Min. Nodes Formula A 1 1 1 A A 2 2 2 B B A A 3 3 3 B B h h C C

22 Properties of Binary Tree
3. Minimum number of nodes possible in a binary tree of height h is: h Proof: 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 [Add 1 for 0 to lmax] n = lmax + 1 n = h Hence proved.

23 Properties of Binary Tree
Question: In any binary tree, if lmax = 5, the find out: 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 = = 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 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 A 1 1 = 0 + 1 B A 2 1 2 = 1 + 1 B A C 3 2 3 = 2 + 1 B A C D E F G 7 6 7 = 6 + 1 n n-1 n = (n-1) + 1

25 Properties of Binary Tree
4. For a binary tree, if n is the number of nodes and e is the number of edges, then n = e + 1. Proof: 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 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 A 1 1 = 0 + 1 B A 1 1 = 0 + 1 B A C 2 1 2 = 1 + 1 B A C D E F G 4 3 4 = 3 + 1

27 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. Proof: 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, e = (n0 x 0) + (n1 x 1) + (n2 x 2) e = n1 + 2n2 (2) 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 Representations of Binary Tree
Linear Representation / Using an Array B A C D E F G H I J How to store so that we can get it back? Leave blank spaces for children possible in future. Array A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 A B C D E F G H I J 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 A B C D E F G - - - - H - I J

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

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

32 Representations of Binary Tree
Sequential Representation v/s Linked Representation 1 2 3 4 5 6 7 Sequential / Linear Representation A B C D - - E v/s B A C D E 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 Traversals on a Binary Tree
Question: Tell all the data/elements present in the binary tree. PreOrder Converse PreOrder A B C [R Lc Rc] C B A [Rc Lc R] B A C InOrder Converse InOrder B A C [Lc R Rc] C A B [Rc R Lc] PostOrder Converse PostOrder B C A [Lc Rc R] A C B [R Rc Lc]

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

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 Traversals on a Binary Tree
PreOrder R TL Lc TR Rc R (A) R (B) R (D) A Lc - Lc Lc Rc - B C Rc R (E) Lc - D E Rc - Rc R (C) - Lc - PreOrder: A B D E C Rc

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

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

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

42 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 B A C A X B X X C X Visit(A) (A) Visit(B) (B) PreOrder(A) PreOrder(B) PreOrder(NULL) - PreOrder(NULL) - PreOrder: Visit(C) (C) - A B C PreOrder(C) PreOrder(NULL) 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 Traversals on a Binary Tree
P Q R T S U V W X 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


Download ppt "Data Structure Use / Purpose: Data Structure: Store marks of 1 student"

Similar presentations


Ads by Google