AlwaysStackin
Data Exfiltration Specialist
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:
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!
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.
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:
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
First, we need the following declarations:
- #include <iostream>
- using
namespace
std;
- typedef
struct
x {
- int
value;
- struct
x *
L, *
R;
- }
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!
- void
insert(
leaf *
&
currentLeaf, int
val)
{
- if
(
currentLeaf ==
NULL
)
{
- currentLeaf =
new
leaf;
- currentLeaf-
>
value =
val;
- currentLeaf-
>
L =
NULL
;
- currentLeaf-
>
R =
NULL
;
- }
else
{
- if
(
val <
currentLeaf-
>
value)
{
- insert(
currentLeaf-
>
L, val)
;
- }
else
{
- insert(
currentLeaf-
>
R, val)
;
- }
- }
- }
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.
- void
printPreOrder(
leaf *
currentLeaf)
{
- if
(
currentLeaf ==
NULL
)
{
- return
;
- }
- cout
<<
currentLeaf-
>
value <<
" "
;
- printPreOrder(
currentLeaf-
>
L)
;
- printPreOrder(
currentLeaf-
>
R)
;
- }
- void
printInOrder(
leaf *
currentLeaf)
{
- if
(
currentLeaf ==
NULL
)
{
- return
;
- }
- printInOrder(
currentLeaf-
>
L)
;
- cout
<<
currentLeaf-
>
value <<
" "
;
- printInOrder(
currentLeaf-
>
R)
;
- }
- void
printPostOrder(
leaf *
currentLeaf)
{
- if
(
currentLeaf ==
NULL
)
{
- return
;
- }
- printPostOrder(
currentLeaf-
>
L)
;
- printPostOrder(
currentLeaf-
>
R)
;
- cout
<<
currentLeaf-
>
value <<
" "
;
- }
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:
- #include <iostream>
- using
namespace
std;
- typedef
struct
x {
- int
value;
- struct
x *
L, *
R;
- }
leaf;
- void
insert(
leaf *
&
currentLeaf, int
val)
{
- if
(
currentLeaf ==
NULL
)
{
- currentLeaf =
new
leaf;
- currentLeaf-
>
value =
val;
- currentLeaf-
>
L =
NULL
;
- currentLeaf-
>
R =
NULL
;
- }
else
{
- if
(
val <
currentLeaf-
>
value)
{
- insert(
currentLeaf-
>
L, val)
;
- }
else
{
- insert(
currentLeaf-
>
R, val)
;
- }
- }
- }
- void
printPreOrder(
leaf *
currentLeaf)
{
- if
(
currentLeaf ==
NULL
)
{
- return
;
- }
- cout
<<
currentLeaf-
>
value <<
" "
;
- printPreOrder(
currentLeaf-
>
L)
;
- printPreOrder(
currentLeaf-
>
R)
;
- }
- void
printInOrder(
leaf *
currentLeaf)
{
- if
(
currentLeaf ==
NULL
)
{
- return
;
- }
- printInOrder(
currentLeaf-
>
L)
;
- cout
<<
currentLeaf-
>
value <<
" "
;
- printInOrder(
currentLeaf-
>
R)
;
- }
- void
printPostOrder(
leaf *
currentLeaf)
{
- if
(
currentLeaf ==
NULL
)
{
- return
;
- }
- printPostOrder(
currentLeaf-
>
L)
;
- printPostOrder(
currentLeaf-
>
R)
;
- cout
<<
currentLeaf-
>
value <<
" "
;
- }
- int
main(
int
argc, char
**
argv)
{
- leaf *
treeRoot =
NULL
;
- insert(
treeRoot, 10
)
;
- insert(
treeRoot, 3
)
;
- insert(
treeRoot, 8
)
;
- insert(
treeRoot, 7
)
;
- insert(
treeRoot, 20
)
;
- insert(
treeRoot, 5
)
;
- cout
<<
"Pre-order: "
;
- printPreOrder(
treeRoot)
;
- cout
<<
endl;
- cout
<<
"In-order: "
;
- printInOrder(
treeRoot)
;
- cout
<<
endl;
- cout
<<
"Post-order: "
;
- printPostOrder(
treeRoot)
;
- cout
<<
endl;
- return
0
;
- }
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.