• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Simple Binary Tree in C++ - Part 1

AlwaysStackin

Data Exfiltration Specialist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
35
Likes
190
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 900 XP
Today we are about to see how simple and fast it is to build a binary tree in C++. Binary trees are generally useful when you need a quick search in a given data set. The tree we are creating is with an int value, but you can change to any other type!

First, we need the following declarations:
  1. #include <iostream>

  2. using

    namespace

    std;

  3. typedef

    struct

    x {
  4. int

    value;
  5. struct

    x *

    L, *

    R;
  6. }

    leaf;

As you can see, we have defined the leaf structure. It holds a value, and 2 pointers (L and R) to the left and right leaf, respectively.

Now, we will define the insert method. In the next tutorial, we are going to define the other operations too!

  1. void

    insert(

    leaf *

    &

    currentLeaf, int

    val)

    {
  2. if

    (

    currentLeaf ==

    NULL

    )

    {
  3. currentLeaf =

    new

    leaf;
  4. currentLeaf-

    >

    value =

    val;
  5. currentLeaf-

    >

    L =

    NULL

    ;
  6. currentLeaf-

    >

    R =

    NULL

    ;
  7. }

    else

    {
  8. if

    (

    val <

    currentLeaf-

    >

    value)

    {
  9. insert(

    currentLeaf-

    >

    L, val)

    ;
  10. }

    else

    {
  11. insert(

    currentLeaf-

    >

    R, val)

    ;
  12. }
  13. }
  14. }

As you can see, right from the beginning (insert) the data is sorted based on a simple rule: if the value is smaller, it's the left leaf, if it's larger then it's the right leaf. It is a recursive function, but very simple to comprehend.

We also define 3 routines for outputting leafs in pre-, in-, and post-order.

  1. void

    printPreOrder(

    leaf *

    currentLeaf)

    {
  2. if

    (

    currentLeaf ==

    NULL

    )

    {
  3. return

    ;
  4. }
  5. cout

    <<

    currentLeaf-

    >

    value <<

    " "

    ;
  6. printPreOrder(

    currentLeaf-

    >

    L)

    ;
  7. printPreOrder(

    currentLeaf-

    >

    R)

    ;
  8. }

  9. void

    printInOrder(

    leaf *

    currentLeaf)

    {
  10. if

    (

    currentLeaf ==

    NULL

    )

    {
  11. return

    ;
  12. }
  13. printInOrder(

    currentLeaf-

    >

    L)

    ;
  14. cout

    <<

    currentLeaf-

    >

    value <<

    " "

    ;
  15. printInOrder(

    currentLeaf-

    >

    R)

    ;
  16. }

  17. void

    printPostOrder(

    leaf *

    currentLeaf)

    {
  18. if

    (

    currentLeaf ==

    NULL

    )

    {
  19. return

    ;
  20. }
  21. printPostOrder(

    currentLeaf-

    >

    L)

    ;
  22. printPostOrder(

    currentLeaf-

    >

    R)

    ;
  23. cout

    <<

    currentLeaf-

    >

    value <<

    " "

    ;
  24. }

The 3 routines are almost identical - the only difference is when the leaf value is printed!

We have created a test program to insert some numbers and then output the tree calling all 3 output methods (orders).

Here is the full source code for main.cpp:

  1. #include <iostream>

  2. using

    namespace

    std;

  3. typedef

    struct

    x {
  4. int

    value;
  5. struct

    x *

    L, *

    R;
  6. }

    leaf;

  7. void

    insert(

    leaf *

    &

    currentLeaf, int

    val)

    {
  8. if

    (

    currentLeaf ==

    NULL

    )

    {
  9. currentLeaf =

    new

    leaf;
  10. currentLeaf-

    >

    value =

    val;
  11. currentLeaf-

    >

    L =

    NULL

    ;
  12. currentLeaf-

    >

    R =

    NULL

    ;
  13. }

    else

    {
  14. if

    (

    val <

    currentLeaf-

    >

    value)

    {
  15. insert(

    currentLeaf-

    >

    L, val)

    ;
  16. }

    else

    {
  17. insert(

    currentLeaf-

    >

    R, val)

    ;
  18. }
  19. }
  20. }

  21. void

    printPreOrder(

    leaf *

    currentLeaf)

    {
  22. if

    (

    currentLeaf ==

    NULL

    )

    {
  23. return

    ;
  24. }
  25. cout

    <<

    currentLeaf-

    >

    value <<

    " "

    ;
  26. printPreOrder(

    currentLeaf-

    >

    L)

    ;
  27. printPreOrder(

    currentLeaf-

    >

    R)

    ;
  28. }

  29. void

    printInOrder(

    leaf *

    currentLeaf)

    {
  30. if

    (

    currentLeaf ==

    NULL

    )

    {
  31. return

    ;
  32. }
  33. printInOrder(

    currentLeaf-

    >

    L)

    ;
  34. cout

    <<

    currentLeaf-

    >

    value <<

    " "

    ;
  35. printInOrder(

    currentLeaf-

    >

    R)

    ;
  36. }

  37. void

    printPostOrder(

    leaf *

    currentLeaf)

    {
  38. if

    (

    currentLeaf ==

    NULL

    )

    {
  39. return

    ;
  40. }
  41. printPostOrder(

    currentLeaf-

    >

    L)

    ;
  42. printPostOrder(

    currentLeaf-

    >

    R)

    ;
  43. cout

    <<

    currentLeaf-

    >

    value <<

    " "

    ;
  44. }

  45. int

    main(

    int

    argc, char

    **

    argv)

    {
  46. leaf *

    treeRoot =

    NULL

    ;

  47. insert(

    treeRoot, 10

    )

    ;
  48. insert(

    treeRoot, 3

    )

    ;
  49. insert(

    treeRoot, 8

    )

    ;
  50. insert(

    treeRoot, 7

    )

    ;
  51. insert(

    treeRoot, 20

    )

    ;
  52. insert(

    treeRoot, 5

    )

    ;

  53. cout

    <<

    "Pre-order: "

    ;
  54. printPreOrder(

    treeRoot)

    ;
  55. cout

    <<

    endl;
  56. cout

    <<

    "In-order: "

    ;
  57. printInOrder(

    treeRoot)

    ;
  58. cout

    <<

    endl;
  59. cout

    <<

    "Post-order: "

    ;
  60. printPostOrder(

    treeRoot)

    ;
  61. cout

    <<

    endl;

  62. return

    0

    ;
  63. }

The output is the following:

Pre-order: 10 3 8 7 5 20
In-order: 3 5 7 8 10 20
Post-order: 5 7 8 3 20 10

Enjoy!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,672

349,682

Top