How is C++'s std::set class able to implement a binary tree for ANY type of data structure?

user3566398

I understand how binary trees are implemented for most native elements such as ints or strings. So I can understand an implementation of std::set that would have a constructor like

switch(typeof(T)) // T being the typename/class in the implementation 
{
  case int: 
  {
      /* create a binary tree structure that uses the bitshift operator to 
         add elements, e.g. 13=1101 is created as
                                      /
                                     /
                                    /
                                   /
                                  1
                                 /
                                /
                               /
                              1
                               \
                                \
                                 0
                                /
                               1
      */
  }
  case string: 
  {
      /* Do something where the string is added to a tree by going letter-by-letter 
         and looking whether the letter is in the second half of the alphabet (?)
      */
  }
  // etcetera for every imaginable type
}

but obviously this is not how std::set is actually implemented, because it is able to create a tree even when I use a homemade data structure like

struct myStruct
{
      char c; 
      bool b;
};

std::set<myStruct> mySet; 

Could it be possible to create a generic binary tree class that looks at all the bits of a data structure and does something like the int case I mentioned above?

For instance, in the case of myStruct, the size of the structure is 2 bytes of 16 bits, so a myStruct element S with S.c = '!' and S.b = true could look like

00010101 00000001
(c part) (b part)

= 

                             \
                              \
                               0
                                \
                                 \
                                  0
                                   \
                                    \
                                     0
                                    /
                                   /
                                  1
                                   \
                                 [etcetera]

since the ASCII value for '!' is 21 and a bool = true as an int is 1. Then again, this could be inefficient to do generically because a very large data structure would correspond to a gigantic tree that might take more time to traverse then just doing a basic linear search on the elements.

Does that make sense? I'm truly confused an would love if some people here could set me straight.

Deduplicator

What you want is a good book on templates and template meta-programming.

In short, the std::set class only defines a prototype for a class, which is then instantiated at compile-type using the provided arguments (some Key-type Key, some value-type T, which deduces std::less<Key> and std::allocator<std::pair<Key, T>> if not given, or whatever else).

A big part of the flexibility comes from being able to create partial specialisations and using other templates and default arguments.

Now, std::less is defined for many standard-library types and all basic types, but not for custom types.

There are 3 ways to provide the comparison std::map needs:

  • Override the default template argument and provide it to the template (if the override has state, it might make sense to provide an object to the constructor).
  • Specialise std::less.
  • Add a comparison operator (operator<).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Binary Tree Data Structure

From Dev

How to implement a Set Data Structure in Java?

From Dev

c++ data structures implement binary tree with a vector

From Dev

c++ data structures implement binary tree with a vector

From Dev

get succesor Binary Search Tree c++ Data structure

From Dev

How to implement a binary tree in matlab

From Dev

How to implement a binary tree in matlab

From Dev

not able to create Binary search tree in c++

From Dev

c tree structure expression must have a class type

From Dev

c tree structure expression must have a class type

From Dev

How to set needle type in std::binary_search

From Dev

How to set needle type in std::binary_search

From Dev

How to implement Java graph data-structure and class with restricted visibility?

From Dev

Why is Binary Tree Data Structure better that Linear?

From Dev

How to implement a Binary Search Tree in Julia?

From Dev

How to implement keySet method in Binary Search Tree

From Dev

How to allow a std::unique_ptr to access a class's private destructor or implement a C++ factory class with a private destructor?

From Dev

How to convert a Ternary expression to a Binary tree structure?

From Dev

Java child class not able to implement parent class's abstract method

From Dev

How to implement a copy constructor for a class with type-erased data member?

From Dev

How to convert data structure to another tree structure

From Dev

Data Structure Set, List, Map or Tree?

From Dev

Binary Tree Structure

From Dev

Structure allocation with binary tree

From Dev

C++ Defining Tree Node Data Structure with std::pair Template Specialization

From Dev

How to define a `std::set` sorting on another class data member?

From Dev

how to create a class that would accept vector holding any type of data?

From Dev

How to create a binary tree of value type String

From Dev

C++: Which Data Type in STXXL is suitable to create External Memory Binary Search Tree?

Related Related

  1. 1

    Binary Tree Data Structure

  2. 2

    How to implement a Set Data Structure in Java?

  3. 3

    c++ data structures implement binary tree with a vector

  4. 4

    c++ data structures implement binary tree with a vector

  5. 5

    get succesor Binary Search Tree c++ Data structure

  6. 6

    How to implement a binary tree in matlab

  7. 7

    How to implement a binary tree in matlab

  8. 8

    not able to create Binary search tree in c++

  9. 9

    c tree structure expression must have a class type

  10. 10

    c tree structure expression must have a class type

  11. 11

    How to set needle type in std::binary_search

  12. 12

    How to set needle type in std::binary_search

  13. 13

    How to implement Java graph data-structure and class with restricted visibility?

  14. 14

    Why is Binary Tree Data Structure better that Linear?

  15. 15

    How to implement a Binary Search Tree in Julia?

  16. 16

    How to implement keySet method in Binary Search Tree

  17. 17

    How to allow a std::unique_ptr to access a class's private destructor or implement a C++ factory class with a private destructor?

  18. 18

    How to convert a Ternary expression to a Binary tree structure?

  19. 19

    Java child class not able to implement parent class's abstract method

  20. 20

    How to implement a copy constructor for a class with type-erased data member?

  21. 21

    How to convert data structure to another tree structure

  22. 22

    Data Structure Set, List, Map or Tree?

  23. 23

    Binary Tree Structure

  24. 24

    Structure allocation with binary tree

  25. 25

    C++ Defining Tree Node Data Structure with std::pair Template Specialization

  26. 26

    How to define a `std::set` sorting on another class data member?

  27. 27

    how to create a class that would accept vector holding any type of data?

  28. 28

    How to create a binary tree of value type String

  29. 29

    C++: Which Data Type in STXXL is suitable to create External Memory Binary Search Tree?

HotTag

Archive