SlideShare una empresa de Scribd logo
1 de 26
Implementing Binary Tree

Objectives
In 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Implementing Binary Tree

Summary
In 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
Implementing Binary Tree

Summary (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
Implementing Binary Tree

Summary (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
Implementing Binary Tree
Summary (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

Más contenido relacionado

La actualidad más candente

Data structure-questions
Data structure-questionsData structure-questions
Data structure-questionsShekhar Chander
 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesHoang Nguyen
 
An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...
An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...
An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...Editor IJCATR
 
Lecture4a dynamic data_structure
Lecture4a dynamic data_structureLecture4a dynamic data_structure
Lecture4a dynamic data_structurembadhi barnabas
 
Comparision Of Various Lossless Image Compression Techniques
Comparision Of Various Lossless Image Compression TechniquesComparision Of Various Lossless Image Compression Techniques
Comparision Of Various Lossless Image Compression TechniquesIJERA Editor
 
DATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESDATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESsuthi
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
C programming session 07
C programming session 07C programming session 07
C programming session 07Dushmanta Nath
 
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...TELKOMNIKA JOURNAL
 
Data Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn HubData Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn HubLearn Hub
 
Computing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
Computing 9691 Test Paper Paper 3 for May / June 2007 CambridgeComputing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
Computing 9691 Test Paper Paper 3 for May / June 2007 CambridgeAlpro
 

La actualidad más candente (19)

Data structure-questions
Data structure-questionsData structure-questions
Data structure-questions
 
Ds 1
Ds 1Ds 1
Ds 1
 
Tree
TreeTree
Tree
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Ds 2
Ds 2Ds 2
Ds 2
 
Data strucer
Data strucerData strucer
Data strucer
 
Lesson 2.2 abstraction
Lesson 2.2   abstractionLesson 2.2   abstraction
Lesson 2.2 abstraction
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...
An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...
An Efficient Approach for Enhancing the Security of Amazigh Text using Binary...
 
Lecture4a dynamic data_structure
Lecture4a dynamic data_structureLecture4a dynamic data_structure
Lecture4a dynamic data_structure
 
Lesson 2.1 array
Lesson 2.1   arrayLesson 2.1   array
Lesson 2.1 array
 
Comparision Of Various Lossless Image Compression Techniques
Comparision Of Various Lossless Image Compression TechniquesComparision Of Various Lossless Image Compression Techniques
Comparision Of Various Lossless Image Compression Techniques
 
DATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTESDATA STRUCTURES - SHORT NOTES
DATA STRUCTURES - SHORT NOTES
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
 
Data Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn HubData Types | CS8251- Programming in c | Learn Hub
Data Types | CS8251- Programming in c | Learn Hub
 
Computing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
Computing 9691 Test Paper Paper 3 for May / June 2007 CambridgeComputing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
Computing 9691 Test Paper Paper 3 for May / June 2007 Cambridge
 

Similar a Implement Binary Tree Operations

Binary trees
Binary treesBinary trees
Binary treesAmit Vats
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structureMahmoud Alfarra
 
Vb net xp_05
Vb net xp_05Vb net xp_05
Vb net xp_05Niit Care
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptxrani marri
 
Vb net xp_03
Vb net xp_03Vb net xp_03
Vb net xp_03Niit Care
 
Furnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree StructuresFurnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree Structuresijceronline
 
mitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.pptmitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.pptAMMAD45
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn osalisha230390
 
Data and File Structure Lecture Notes
Data and File Structure Lecture NotesData and File Structure Lecture Notes
Data and File Structure Lecture NotesFellowBuddy.com
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfMaryJacob24
 
Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfezzi552
 

Similar a Implement Binary Tree Operations (20)

Binary trees
Binary treesBinary trees
Binary trees
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
Binary trees
Binary treesBinary trees
Binary trees
 
Vb net xp_05
Vb net xp_05Vb net xp_05
Vb net xp_05
 
data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
 
A41001011
A41001011A41001011
A41001011
 
bst.ppt
bst.pptbst.ppt
bst.ppt
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
 
Vb net xp_03
Vb net xp_03Vb net xp_03
Vb net xp_03
 
Furnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree StructuresFurnish an Index Using the Works of Tree Structures
Furnish an Index Using the Works of Tree Structures
 
mitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.pptmitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.ppt
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
Binary Trees
Binary TreesBinary Trees
Binary Trees
 
for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
 
Binary tree
Binary treeBinary tree
Binary tree
 
Data and File Structure Lecture Notes
Data and File Structure Lecture NotesData and File Structure Lecture Notes
Data and File Structure Lecture Notes
 
Data Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdfData Structures & Recursion-Introduction.pdf
Data Structures & Recursion-Introduction.pdf
 
Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdf
 
Ds 4
Ds 4Ds 4
Ds 4
 
data mining.pptx
data mining.pptxdata mining.pptx
data mining.pptx
 

Más de Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Último

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Último (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Implement Binary Tree Operations

  • 1. Implementing Binary Tree Objectives In 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Implementing Binary Tree Summary In 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. Implementing Binary Tree Summary (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. Implementing Binary Tree Summary (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. Implementing Binary Tree Summary (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

Notas del editor

  1. Lower Bound and Upper Bound to denote the first element number and the last element number respectively
  2. Lower Bound and Upper Bound to denote the first element number and the last element number respectively