In addition to the answers you have already received.
Use the right headers
Prefer the C++ headers for C functions when you write C++ code.
I.e:
#include <cstdio>
Weird use of typedef
I'm not really a fan of how you use typedef here, the type int
is the default backing type and it doesn't really matter which type you have in your case. As you're using C++11 you might as well use enum class you might as well use enum class.
Your enum definition should just be:
enum class TraversalType
{
InOrder,
PreOrder,
PostOrder
};
A classic case of KISS.
Use an object oriented design
You currently use Node
as a plain data structure. But if you delegate some of the work to the node class, you'll see that your code will simplify. For example search
is a good candidate. While we're at it, your code requires that you have both operator <
and operator ==
for the key type. You can rewrite your search logic like this to avoid this requirement:
Node* search(const T &a_key)
{
if (a_key < key) {
return left ? left->search(a_key) : nullptr;
}
else if (key < a_key){
return right? right->search(a_key) : nullptr;
}
else{
return node;
}
}
as long as operator <
defines a partial ordering.
An added benefit of the above implementation is that instead of nullptr
you can return left
or right
, you can use then use this to implement your insert
function. How is left as an exercise for the reader.
Any method where you find yourself using node->
frequently is a good candidate to move into the Node
class.
In addition to the answers you have already received.
Use the right headers
Prefer the C++ headers for C functions when you write C++ code.
I.e:
#include <cstdio>
Weird use of typedef
I'm not really a fan of how you use typedef here, the type int
is the default backing type and it doesn't really matter which type you have in your case. As you're using C++11 you might as well use enum class.
Your enum definition should just be:
enum class TraversalType
{
InOrder,
PreOrder,
PostOrder
};
A classic case of KISS.
Use an object oriented design
You currently use Node
as a plain data structure. But if you delegate some of the work to the node class, you'll see that your code will simplify. For example search
is a good candidate. While we're at it, your code requires that you have both operator <
and operator ==
for the key type. You can rewrite your search logic like this to avoid this requirement:
Node* search(const T &a_key)
{
if (a_key < key) {
return left ? left->search(a_key) : nullptr;
}
else if (key < a_key){
return right? right->search(a_key) : nullptr;
}
else{
return node;
}
}
as long as operator <
defines a partial ordering.
An added benefit of the above implementation is that instead of nullptr
you can return left
or right
, you can use then use this to implement your insert
function. How is left as an exercise for the reader.
Any method where you find yourself using node->
frequently is a good candidate to move into the Node
class.
In addition to the answers you have already received.
Use the right headers
Prefer the C++ headers for C functions when you write C++ code.
I.e:
#include <cstdio>
Weird use of typedef
I'm not really a fan of how you use typedef here, the type int
is the default backing type and it doesn't really matter which type you have in your case. As you're using C++11 you might as well use enum class.
Your enum definition should just be:
enum class TraversalType
{
InOrder,
PreOrder,
PostOrder
};
A classic case of KISS.
Use an object oriented design
You currently use Node
as a plain data structure. But if you delegate some of the work to the node class, you'll see that your code will simplify. For example search
is a good candidate. While we're at it, your code requires that you have both operator <
and operator ==
for the key type. You can rewrite your search logic like this to avoid this requirement:
Node* search(const T &a_key)
{
if (a_key < key) {
return left ? left->search(a_key) : nullptr;
}
else if (key < a_key){
return right? right->search(a_key) : nullptr;
}
else{
return node;
}
}
as long as operator <
defines a partial ordering.
An added benefit of the above implementation is that instead of nullptr
you can return left
or right
, you can use then use this to implement your insert
function. How is left as an exercise for the reader.
Any method where you find yourself using node->
frequently is a good candidate to move into the Node
class.
In addition to the answers you have already received.
Use the right headers
Prefer the C++ headers for C functions when you write C++ code.
I.e:
#include <cstdio>
Weird use of typedef
I'm not really a fan of how you use typedef here, the type int
is the default backing type and it doesn't really matter which type you have in your case. As you're using C++11 you might as well use enum class.
Your enum definition should just be:
enum class TraversalType
{
TraversalTypeInOrderInOrder,
TraversalTypePreOrderPreOrder,
TraversalTypePostOrderPostOrder
};
A classic case of KISS.
Use an object oriented design
You currently use Node
as a plain data structure. But if you delegate some of the work to the node class, you'll see that your code will simplify. For example search
is a good candidate. While we're at it, your code requires that you have both operator <
and operator ==
for the key type. You can rewrite your search logic like this to avoid this requirement:
Node* search(const T &a_key)
{
if (a_key < key) {
return left ? left->search(a_key) : nullptr;
}
else if (key < a_key){
return right? right->search(a_key) : nullptr;
}
else{
return node;
}
}
as long as operator <
defines a partial ordering.
An added benefit of the above implementation is that instead of nullptr
you can return left
or right
, you can use then use this to implement your insert
function. How is left as an exercise for the reader.
Any method where you find yourself using node->
frequently is a good candidate to move into the Node
class.
In addition to the answers you have already received.
Use the right headers
Prefer the C++ headers for C functions when you write C++ code.
I.e:
#include <cstdio>
Weird use of typedef
I'm not really a fan of how you use typedef here, the type int
is the default backing type and it doesn't really matter which type you have in your case. As you're using C++11 you might as well use enum class.
Your enum definition should just be:
enum class TraversalType
{
TraversalTypeInOrder,
TraversalTypePreOrder,
TraversalTypePostOrder
};
A classic case of KISS.
Use an object oriented design
You currently use Node
as a plain data structure. But if you delegate some of the work to the node class, you'll see that your code will simplify. For example search
is a good candidate. While we're at it, your code requires that you have both operator <
and operator ==
for the key type. You can rewrite your search logic like this to avoid this requirement:
Node* search(const T &a_key)
{
if (a_key < key) {
return left ? left->search(a_key) : nullptr;
}
else if (key < a_key){
return right? right->search(a_key) : nullptr;
}
else{
return node;
}
}
as long as operator <
defines a partial ordering.
An added benefit of the above implementation is that instead of nullptr
you can return left
or right
, you can use then use this to implement your insert
function. How is left as an exercise for the reader.
Any method where you find yourself using node->
frequently is a good candidate to move into the Node
class.
In addition to the answers you have already received.
Use the right headers
Prefer the C++ headers for C functions when you write C++ code.
I.e:
#include <cstdio>
Weird use of typedef
I'm not really a fan of how you use typedef here, the type int
is the default backing type and it doesn't really matter which type you have in your case. As you're using C++11 you might as well use enum class.
Your enum definition should just be:
enum class TraversalType
{
InOrder,
PreOrder,
PostOrder
};
A classic case of KISS.
Use an object oriented design
You currently use Node
as a plain data structure. But if you delegate some of the work to the node class, you'll see that your code will simplify. For example search
is a good candidate. While we're at it, your code requires that you have both operator <
and operator ==
for the key type. You can rewrite your search logic like this to avoid this requirement:
Node* search(const T &a_key)
{
if (a_key < key) {
return left ? left->search(a_key) : nullptr;
}
else if (key < a_key){
return right? right->search(a_key) : nullptr;
}
else{
return node;
}
}
as long as operator <
defines a partial ordering.
An added benefit of the above implementation is that instead of nullptr
you can return left
or right
, you can use then use this to implement your insert
function. How is left as an exercise for the reader.
Any method where you find yourself using node->
frequently is a good candidate to move into the Node
class.
In addition to the answers you have already received.
Use the right headers
Prefer the C++ headers for C functions when you write C++ code.
I.e:
#include <cstdio>
Weird use of typedef
I'm not really a fan of how you use typedef here, the type int
is the default backing type and it doesn't really matter which type you have in your case. As you're using C++11 you might as well use enum class.
Your enum definition should just be:
enum class TraversalType
{
TraversalTypeInOrder,
TraversalTypePreOrder,
TraversalTypePostOrder
};
A classic case of KISS.
Use an object oriented design
You currently use Node
as a plain data structure. But if you delegate some of the work to the node class, you'll see that your code will simplify. For example search
is a good candidate. While we're at it, your code requires that you have both operator <
and operator ==
for the key type. You can rewrite your search logic like this to avoid this requirement:
Node* search(const T &a_key)
{
if (a_key < key) {
return left ? left->search(a_key) : nullptr;
}
else if (key < a_key){
return right? right->search(a_key) : nullptr;
}
else{
return node;
}
}
as long as operator <
defines a partial ordering.
An added benefit of the above implementation is that instead of nullptr
you can return left
or right
, you can use then use this to implement your insert
function. How is left as an exercise for the reader.
Any method where you find yourself using node->
frequently is a good candidate to move into the Node
class.