Upcoming SlideShare
×

# Ds 5

1,001 views

Published on

Published in: Technology, Business
1 Comment
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here

Are you sure you want to  Yes  No
Your message goes here
• Be the first to like this

Views
Total views
1,001
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
41
1
Likes
0
Embeds 0
No embeds

No notes for slide
• Lower Bound and Upper Bound to denote the first element number and the last element number respectively
• Lower Bound and Upper Bound to denote the first element number and the last element number respectively
• ### Ds 5

1. 1. Implementing Binary TreeObjectivesIn this lesson, you will learn to: Define binary tree structure Identify applications of binary tree Implement the following operations on binary trees Inserting nodes in a binary tree Searching in a binary tree Traversing a binary tree Deleting nodes from a binary tree ©NIIT Implementing Binary Tree/Lesson 5/Slide 1 of 26
2. 2. Implementing Binary Tree Introducing Binary Tree A tree is a data structure used to represent data containing an hierarchical relation between its elements. The sample tree is as follows: ©NIIT Implementing Binary Tree/Lesson 5/Slide 2 of 26
3. 3. Implementing Binary Tree Binary Tree (Contd..) Each item is called a node or leaf. The first node in the tree is called the root. Each piece under a node is called a subtree. A node under which there is a subtree is called the parent node. A node that has no subtrees is called a terminal node. The height of the tree is the number of layers in the tree. ©NIIT Implementing Binary Tree/Lesson 5/Slide 3 of 26
4. 4. Implementing Binary Tree Application of Trees Trees are used in applications in which the relation between data elements has to be represented as a hierarchy. A tree represent a non-linear data structure. Trees are used to represent the records in a file in which elementary items are stored under group items. ©NIIT Implementing Binary Tree/Lesson 5/Slide 4 of 26
5. 5. Implementing Binary Tree Just a Minute: 2. Can a linked list be used to represent the UNIX file system? 3. Refer the following figure to answer the following: ©NIIT Implementing Binary Tree/Lesson 5/Slide 5 of 26
6. 6. Implementing Binary Tree Just a Minute(Contd..): a. The number of nodes in the tree is _______________. b. The root contains the INFO _____________. c. The number of subtrees which the root has is _______. d. The number of terminal nodes is ________________. ©NIIT Implementing Binary Tree/Lesson 5/Slide 6 of 26
7. 7. Implementing Binary Tree Just a Minute (Contd..): a. The height of the tree is _________________. b. A terminal node is the ___________ node in the tree. c. The address part of the terminal node stores the value __________ , just like the last node in a linked list. d. The number of subtrees which the node with INFO c has is ©NIIT Implementing Binary Tree/Lesson 5/Slide 7 of 26
8. 8. Implementing Binary Tree Categories of Binary Tree There are various types of binary tree,out of which two are as follows: Balanced binary tree: It is a binary tree in which the heights of the two subtrees of every node never differ by more than one. The figure of balanced binary tree is as follows: ©NIIT Implementing Binary Tree/Lesson 5/Slide 8 of 26
9. 9. Implementing Binary Tree Categories of Binary Tree (Contd..) Unbalanced binary tree: In an unbalanced binary tree, each node can have a maximum of two subtrees (the left subtree and the right subtree). It may also have zero nodes (unlike a tree). The figure of unbalanced binary tree is as follows: ©NIIT Implementing Binary Tree/Lesson 5/Slide 9 of 26
10. 10. Implementing Binary Tree Categories of Binary Tree (Contd..) Binary sorted tree It is the most common form of binary tree. It has the data in organized manner. Through this, the data can be searched, inserted, or deleted much faster. The structure of sorted binary tree is as follows: ©NIIT Implementing Binary Tree/Lesson 5/Slide 10 of 26
11. 11. Implementing Binary Tree Representing Binary Trees The representation of a binary tree is similar to a linked list. Through this, more efficient searches, insertions and deletions in a binary tree can be performed as compared to other data structures like linked lists and arrays. The figure for sample sorted binary tree is as follows: ©NIIT Implementing Binary Tree/Lesson 5/Slide 11 of 26
12. 12. Implementing Binary Tree Just a Minute: 7. The tree represented in above figure, the left pointer of the node whose INFO is e points to the node whose INFO is __________ while the right pointer points to the node whose INFO is_____________. ©NIIT Implementing Binary Tree/Lesson 5/Slide 12 of 26
13. 13. Implementing Binary Tree Just a Minute (Contd..): 2. In above figure, which of the pointers have NULL value? The left and right pointers of the node with INFO A. The left and right pointers of the node with INFO D. The left and right pointers of the node with INFO K. (a), (b) and (c). 3. Using the example of the linked list containing only item numbers, give a declaration for a binary tree which can store these item numbers. Also give the declaration for a pointer to the root node of this tree. Item numbers are 4 characters long. ©NIIT Implementing Binary Tree/Lesson 5/Slide 13 of 26
14. 14. Implementing Binary Tree Manipulating Tree Structures Insertion in a Binary Tree- It involve the following processes: Allocation of memory for the new node. The left and right pointers of the new node are also initialized to NULL. The comparison of the new INFO with the existing INFO. Change the left or right pointer of the node under which the new node has been inserted. ©NIIT Implementing Binary Tree/Lesson 5/Slide 14 of 26
15. 15. Implementing Binary Tree Manipulating Tree Structures (Contd..) The graphical representation of an insertion is as follows: Before Insertion After Insertion ©NIIT Implementing Binary Tree/Lesson 5/Slide 15 of 26
16. 16. Implementing Binary Tree Searching in a Binary Tree Since the binary tree contains the stored data, the search will be made along one subtree only until a specific INFO is located. The logic for searching is as foliows (assume that SEARCH _INFO is the data to be located, ROOT has the address of the root node and PTR is a pointer of type tree_tag): Set PTR to ROOT. Ask for SEARCH INFO. ©NIIT Implementing Binary Tree/Lesson 5/Slide 16 of 26
17. 17. Implementing Binary Tree Searching in a Binary Tree (Contd..) While PTR != NULL do step 4. Compare SEARCH_INFO with INFO of the node to which PTR is pointing. If SEARCH_INFO = INFO of node, exit from loop and do step 5. If SEARCH-INFO INFO of node, set PTR to the right pointer of the node. If SEARCH_INFO INFO of node, set PTR to the left pointer of the node. ©NIIT Implementing Binary Tree/Lesson 5/Slide 17 of 26
18. 18. Implementing Binary Tree Searching in a Binary Tree(Contd..) Report that the SEARCH_WFO exists in case PTR !=NULL (in which case the loop was exited because the SEARCH _INFO was located) or that the SEARCH_INFO does not exist in case PTR = NULL. ©NIIT Implementing Binary Tree/Lesson 5/Slide 18 of 26
19. 19. Implementing Binary Tree Traversing a Binary Tree Inorder method can be used to traverse the binary tree. The procedure to traverse is as follows: Traverse the left subtree of ROOT in inorder until a NULL pointer is encountered. Display the contents Traverse the right subtree of ROOT in inorder until a NULL pointer is encountered. ©NIIT Implementing Binary Tree/Lesson 5/Slide 19 of 26
20. 20. Implementing Binary Tree Deletion of a Node from a Binary Tree It involve changing of pointers. When a node is to be deleted, its left and /or right subtrees have to be linked to the rest of the tree otherwise all this data will be lost. The situations that may arise during deletion are as follows: The node to be deleted has no subtrees. The node to be deleted has only one subtree. ©NIIT Implementing Binary Tree/Lesson 5/Slide 20 of 26
21. 21. Implementing Binary Tree Deletion of a Node from a Binary Tree(Contd..) The node to be deleted has both subtrees. The root node is being deleted ©NIIT Implementing Binary Tree/Lesson 5/Slide 21 of 26
22. 22. Implementing Binary Tree Problem Statement 5.D.1 Create an invoice data entry program, which performs following operations: Accept Data and store it in a binary tree Display Information from a tree Modify Rate and Qty for a specific item Delete information from the tree ©NIIT Implementing Binary Tree/Lesson 5/Slide 22 of 26
23. 23. Implementing Binary TreeSummaryIn this lesson, you learned that: A tree is a data structure used to represent data containing a hierarchical relation between its elements. For example, the UNIX file system, which has a hierarchical structure, can be represented using a tree Data in a tree is stored in nodes. Each node contains INFO (data) and addresses of the nodes to which it points A tree begin from a root node The height of a tree is the number of layers in the tree ©NIIT Implementing Binary Tree/Lesson 5/Slide 23 of 26
24. 24. Implementing Binary TreeSummary (Contd..) A node with subtrees is referred to as a parent node. A node with no subtrees is referred to as a terminal node Trees are defined using structures A binary tree is a type of a tree in which each node may have a maximum of two nodes under it, which are referred to as subtrees. Thus a node may have a left subtree and/or a right subtree. Binary sorted trees store the data in a manner such that all right subtrees (i.e. subtrees to the right of a parent node) have INFO larger in value than their parent nodes and the left subtrees have INFO smaller than their parent nodes ©NIIT Implementing Binary Tree/Lesson 5/Slide 24 of 26
25. 25. Implementing Binary TreeSummary (Contd..) Insertion in a binary sorted tree involves comparing the new INFO with the existing INFO in the tree, in order to insert the new data in the appropriate place. Searching in a binary sorted tree involves comparing the search INFO with the INFO in the node pointed at by a pointer PTR, which is initially made to point to the root node. Traversal in a binary sorted tree is done recursively using the inorder method. ©NIIT Implementing Binary Tree/Lesson 5/Slide 25 of 26
26. 26. Implementing Binary TreeSummary (Contd..) There are four cases to be considered while deleting a node from a binary tree: The node has no subtrees The node has a subtree to its left only The node has a subtree to its right only The node has two subtrees ©NIIT Implementing Binary Tree/Lesson 5/Slide 26 of 26