Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Linked List With Two Three Tree

Linked List With Two Three Tree

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurescssalgorithmslearning
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hamza Bin Amin
    wrote on last edited by
    #1

    I've to do the searching process through the 2-3 tree by storing the roll number s of the students as keys in the tree. The tree node will contain a pointer to the doubly node in the doubly linked list. I included the doubly node pointer as a private member in the tree node class Then tried to merge the 2-3 tree nodes with the student objects

    // Students Objects

    Node \*s1,\*s2,\*s3,\*s4;
    s1 = new Node;
    s1->data.setData("Hamza",12105092);
    
    s2 = new Node;
    s2->data.setData("Sherlock",12105102);
    
    s3 = new Node;
    s3->data.setData("Watson",12105022);
    
    s4 = new Node;
    s4->data.setData("Spidey",12105042);
    

    // Courses Objects

    Course c1("OOP",3.52,"A");
    Course c2("DM",4,"A+");
    Course c3("DLD",3.2,"A-");
    

    // Tree Creation

    CTree \*tree1;
    

    // Tree Insertion

    tree1 = new CTree();
    tree1->insert(new Student("",12105092));
    tree1->insert(new Student("",12105042));
    tree1->insert(new Student("",12105102));
    tree1->insert(new Student("",12105022));
    

    // Merging Doubly Nodes With 2-3 Tree

    tree1->findOWN(new Student("",12105092))->merge(s1);	
    tree1->findOWN(new Student("",12105102))->merge(s2);	
    tree1->findOWN(new Student("",12105022))->merge(s3);
    tree1->findOWN(new Student("",12105042))->merge(s4);
    

    /************************************

    1. Creating An Empty List Of Students
      ************************************* */

      doublyLinkedList d1;

    /*****************************
    2. Adding Students To The List
    ****************************** */

    d1.insert(s1);
    d1.insert(s2);
    d1.insert(s3);
    d1.insert(s4);
    

    Relevant 2-3 tree methods

    template
    CNode* CTree::findOWN(T*pKey)
    {
    CNode *pNodeFound= 0;
    bool bKeyFound = false;

    search(pKey, &pNodeFound, &bKeyFound);
    if(bKeyFound == true &&
       TCompare(pKey, pNodeFound->getSmallKey()) == EQUAL)
    {
        return pNodeFound;  
    }
    else if(bKeyFound == true &&
            pNodeFound->getSize() == threeNode && 
            TCompare(pKey, pNodeFound->getBigKey()) == EQUAL)
    {
        return pNodeFound;
    }
    else
    {
        return 0;
    } 
    

    }

    template
    int CTree::TCompare(const T* const pT1, const T* const pT2) const
    {
    int iReturnCode = FAILURE;

    if(\*pT1 < \*pT2)
    {
        iReturnCode = LESS;
    }
    else if(\*pT2 < \*pT1)
    {
        iReturnCode = GREATER;
    }
    else
    {
        iR
    
    D CPalliniC S 3 Replies Last reply
    0
    • H Hamza Bin Amin

      I've to do the searching process through the 2-3 tree by storing the roll number s of the students as keys in the tree. The tree node will contain a pointer to the doubly node in the doubly linked list. I included the doubly node pointer as a private member in the tree node class Then tried to merge the 2-3 tree nodes with the student objects

      // Students Objects

      Node \*s1,\*s2,\*s3,\*s4;
      s1 = new Node;
      s1->data.setData("Hamza",12105092);
      
      s2 = new Node;
      s2->data.setData("Sherlock",12105102);
      
      s3 = new Node;
      s3->data.setData("Watson",12105022);
      
      s4 = new Node;
      s4->data.setData("Spidey",12105042);
      

      // Courses Objects

      Course c1("OOP",3.52,"A");
      Course c2("DM",4,"A+");
      Course c3("DLD",3.2,"A-");
      

      // Tree Creation

      CTree \*tree1;
      

      // Tree Insertion

      tree1 = new CTree();
      tree1->insert(new Student("",12105092));
      tree1->insert(new Student("",12105042));
      tree1->insert(new Student("",12105102));
      tree1->insert(new Student("",12105022));
      

      // Merging Doubly Nodes With 2-3 Tree

      tree1->findOWN(new Student("",12105092))->merge(s1);	
      tree1->findOWN(new Student("",12105102))->merge(s2);	
      tree1->findOWN(new Student("",12105022))->merge(s3);
      tree1->findOWN(new Student("",12105042))->merge(s4);
      

      /************************************

      1. Creating An Empty List Of Students
        ************************************* */

        doublyLinkedList d1;

      /*****************************
      2. Adding Students To The List
      ****************************** */

      d1.insert(s1);
      d1.insert(s2);
      d1.insert(s3);
      d1.insert(s4);
      

      Relevant 2-3 tree methods

      template
      CNode* CTree::findOWN(T*pKey)
      {
      CNode *pNodeFound= 0;
      bool bKeyFound = false;

      search(pKey, &pNodeFound, &bKeyFound);
      if(bKeyFound == true &&
         TCompare(pKey, pNodeFound->getSmallKey()) == EQUAL)
      {
          return pNodeFound;  
      }
      else if(bKeyFound == true &&
              pNodeFound->getSize() == threeNode && 
              TCompare(pKey, pNodeFound->getBigKey()) == EQUAL)
      {
          return pNodeFound;
      }
      else
      {
          return 0;
      } 
      

      }

      template
      int CTree::TCompare(const T* const pT1, const T* const pT2) const
      {
      int iReturnCode = FAILURE;

      if(\*pT1 < \*pT2)
      {
          iReturnCode = LESS;
      }
      else if(\*pT2 < \*pT1)
      {
          iReturnCode = GREATER;
      }
      else
      {
          iR
      
      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Hamza Bin Amin wrote:

      ...it mixes up the merging when I try to search 12105022 I should be getting "Watson" but I get "Spidey" instead, how to fix that?

      So have you single-stepped through the code using the debugger?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      • H Hamza Bin Amin

        I've to do the searching process through the 2-3 tree by storing the roll number s of the students as keys in the tree. The tree node will contain a pointer to the doubly node in the doubly linked list. I included the doubly node pointer as a private member in the tree node class Then tried to merge the 2-3 tree nodes with the student objects

        // Students Objects

        Node \*s1,\*s2,\*s3,\*s4;
        s1 = new Node;
        s1->data.setData("Hamza",12105092);
        
        s2 = new Node;
        s2->data.setData("Sherlock",12105102);
        
        s3 = new Node;
        s3->data.setData("Watson",12105022);
        
        s4 = new Node;
        s4->data.setData("Spidey",12105042);
        

        // Courses Objects

        Course c1("OOP",3.52,"A");
        Course c2("DM",4,"A+");
        Course c3("DLD",3.2,"A-");
        

        // Tree Creation

        CTree \*tree1;
        

        // Tree Insertion

        tree1 = new CTree();
        tree1->insert(new Student("",12105092));
        tree1->insert(new Student("",12105042));
        tree1->insert(new Student("",12105102));
        tree1->insert(new Student("",12105022));
        

        // Merging Doubly Nodes With 2-3 Tree

        tree1->findOWN(new Student("",12105092))->merge(s1);	
        tree1->findOWN(new Student("",12105102))->merge(s2);	
        tree1->findOWN(new Student("",12105022))->merge(s3);
        tree1->findOWN(new Student("",12105042))->merge(s4);
        

        /************************************

        1. Creating An Empty List Of Students
          ************************************* */

          doublyLinkedList d1;

        /*****************************
        2. Adding Students To The List
        ****************************** */

        d1.insert(s1);
        d1.insert(s2);
        d1.insert(s3);
        d1.insert(s4);
        

        Relevant 2-3 tree methods

        template
        CNode* CTree::findOWN(T*pKey)
        {
        CNode *pNodeFound= 0;
        bool bKeyFound = false;

        search(pKey, &pNodeFound, &bKeyFound);
        if(bKeyFound == true &&
           TCompare(pKey, pNodeFound->getSmallKey()) == EQUAL)
        {
            return pNodeFound;  
        }
        else if(bKeyFound == true &&
                pNodeFound->getSize() == threeNode && 
                TCompare(pKey, pNodeFound->getBigKey()) == EQUAL)
        {
            return pNodeFound;
        }
        else
        {
            return 0;
        } 
        

        }

        template
        int CTree::TCompare(const T* const pT1, const T* const pT2) const
        {
        int iReturnCode = FAILURE;

        if(\*pT1 < \*pT2)
        {
            iReturnCode = LESS;
        }
        else if(\*pT2 < \*pT1)
        {
            iReturnCode = GREATER;
        }
        else
        {
            iR
        
        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        I suppose you should show the complete code in order to get help.

        THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • H Hamza Bin Amin

          I've to do the searching process through the 2-3 tree by storing the roll number s of the students as keys in the tree. The tree node will contain a pointer to the doubly node in the doubly linked list. I included the doubly node pointer as a private member in the tree node class Then tried to merge the 2-3 tree nodes with the student objects

          // Students Objects

          Node \*s1,\*s2,\*s3,\*s4;
          s1 = new Node;
          s1->data.setData("Hamza",12105092);
          
          s2 = new Node;
          s2->data.setData("Sherlock",12105102);
          
          s3 = new Node;
          s3->data.setData("Watson",12105022);
          
          s4 = new Node;
          s4->data.setData("Spidey",12105042);
          

          // Courses Objects

          Course c1("OOP",3.52,"A");
          Course c2("DM",4,"A+");
          Course c3("DLD",3.2,"A-");
          

          // Tree Creation

          CTree \*tree1;
          

          // Tree Insertion

          tree1 = new CTree();
          tree1->insert(new Student("",12105092));
          tree1->insert(new Student("",12105042));
          tree1->insert(new Student("",12105102));
          tree1->insert(new Student("",12105022));
          

          // Merging Doubly Nodes With 2-3 Tree

          tree1->findOWN(new Student("",12105092))->merge(s1);	
          tree1->findOWN(new Student("",12105102))->merge(s2);	
          tree1->findOWN(new Student("",12105022))->merge(s3);
          tree1->findOWN(new Student("",12105042))->merge(s4);
          

          /************************************

          1. Creating An Empty List Of Students
            ************************************* */

            doublyLinkedList d1;

          /*****************************
          2. Adding Students To The List
          ****************************** */

          d1.insert(s1);
          d1.insert(s2);
          d1.insert(s3);
          d1.insert(s4);
          

          Relevant 2-3 tree methods

          template
          CNode* CTree::findOWN(T*pKey)
          {
          CNode *pNodeFound= 0;
          bool bKeyFound = false;

          search(pKey, &pNodeFound, &bKeyFound);
          if(bKeyFound == true &&
             TCompare(pKey, pNodeFound->getSmallKey()) == EQUAL)
          {
              return pNodeFound;  
          }
          else if(bKeyFound == true &&
                  pNodeFound->getSize() == threeNode && 
                  TCompare(pKey, pNodeFound->getBigKey()) == EQUAL)
          {
              return pNodeFound;
          }
          else
          {
              return 0;
          } 
          

          }

          template
          int CTree::TCompare(const T* const pT1, const T* const pT2) const
          {
          int iReturnCode = FAILURE;

          if(\*pT1 < \*pT2)
          {
              iReturnCode = LESS;
          }
          else if(\*pT2 < \*pT1)
          {
              iReturnCode = GREATER;
          }
          else
          {
              iR
          
          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          1. the line

          tree1->findOWN(new Student("",12105092))->merge(s1);

          creates a memory leak, as the function argument is created on the heap, but never deleted. 2. your function CTree::TCompare compares objects of type Student using

          operator<(const Student&, const Student&)

          Did you define that operator? If not, the result of these comparisons are undefined. If you did, the error may be within the code of that operator definition.

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups