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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Octree structure

C++ Octree structure

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structureshelpquestion
3 Posts 3 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.
  • A Offline
    A Offline
    Alex Chitea
    wrote on last edited by
    #1

    Hi guys, I want to implement in C++ an octree data structure. This should look like a binary tree data structure, however instead of having 2 children, every node should have 8. Does any of you has this thing already implemented and can pass on to me the source code? Or do you know somebody that can help with writing my source code? Thanks a lot,

    D 1 Reply Last reply
    0
    • A Alex Chitea

      Hi guys, I want to implement in C++ an octree data structure. This should look like a binary tree data structure, however instead of having 2 children, every node should have 8. Does any of you has this thing already implemented and can pass on to me the source code? Or do you know somebody that can help with writing my source code? Thanks a lot,

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      I haven't written a binary tree since undergraduate school so here is a high-level overview of what might be required:

      struct Node
      {
      // you'll probably want a data item/pointer here

      // pointers to child nodes
      Node \*p1;
      Node \*p2;
      Node \*p3;
      Node \*p4;
      Node \*p5;
      Node \*p6;
      Node \*p7;
      Node \*p8;
      

      } *pRoot = NULL;

      void _Add( Node *pNode, Datum *pDatum )
      {
      if (NULL == pNode)
      {
      pNode = new Node;
      // assign values accordingly to data items

          // make sure pointers start life as NULL
          pNode->p1 = NULL;
          pNode->p2 = NULL;
          ...
      }
      else if (...)
          \_Add(pNode->p1, pDatum);
      else if (...)
          \_Add(pNode->p2, pDatum);
      else if (...)
          \_Add(pNode->p3, pDatum);
      else if (...)
          \_Add(pNode->p4, pDatum);
      else if (...)
          \_Add(pNode->p5, pDatum);
      else if (...)
          \_Add(pNode->p6, pDatum);
      else if (...)
          \_Add(pNode->p7, pDatum);
      else if (...)
          \_Add(pNode->p8, pDatum);
      else
          assert(FALSE);
      

      }

      void Add( Datum *pDatum )
      {
      _Add(pRoot, pDatum);
      }


      Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

      H 1 Reply Last reply
      0
      • D David Crow

        I haven't written a binary tree since undergraduate school so here is a high-level overview of what might be required:

        struct Node
        {
        // you'll probably want a data item/pointer here

        // pointers to child nodes
        Node \*p1;
        Node \*p2;
        Node \*p3;
        Node \*p4;
        Node \*p5;
        Node \*p6;
        Node \*p7;
        Node \*p8;
        

        } *pRoot = NULL;

        void _Add( Node *pNode, Datum *pDatum )
        {
        if (NULL == pNode)
        {
        pNode = new Node;
        // assign values accordingly to data items

            // make sure pointers start life as NULL
            pNode->p1 = NULL;
            pNode->p2 = NULL;
            ...
        }
        else if (...)
            \_Add(pNode->p1, pDatum);
        else if (...)
            \_Add(pNode->p2, pDatum);
        else if (...)
            \_Add(pNode->p3, pDatum);
        else if (...)
            \_Add(pNode->p4, pDatum);
        else if (...)
            \_Add(pNode->p5, pDatum);
        else if (...)
            \_Add(pNode->p6, pDatum);
        else if (...)
            \_Add(pNode->p7, pDatum);
        else if (...)
            \_Add(pNode->p8, pDatum);
        else
            assert(FALSE);
        

        }

        void Add( Datum *pDatum )
        {
        _Add(pRoot, pDatum);
        }


        Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

        H Offline
        H Offline
        Hosam Aly Mahmoud
        wrote on last edited by
        #3

        Excuse me, but is your code somehow better than the following?

        struct Node { Node * children[8]; } *pRoot = 0;

        void _Add( Node *pNode, Datum* pDatum )
        {
        if ( 0 == pNode )
        {
        pNode = new Node;
        // assign data

          memset( pNode->children, 8\*sizeof(Node\*) );
          return;
        

        }
        // else
        if (...) { _Add(pNode->children[0], pDatum); return; }
        // ...
        }

        Hosam Aly Mahmoud

        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