patterncppMinor
C++ binary search tree
Viewed 0 times
binarytreesearch
Problem
I am learning C++ and today I've implemented a version of the binary search tree. I would like your thoughts about it.
node.h
bTree.h
bTree.cpp
```
#include
#include "bTree.h"
BTree::BTree()
{
root = nullptr;
}
void BTree::addValue(int n, Node *node) {
if (n val)
{
if (node->left != nullptr)
{
addValue(n, node->left);
}
else
{
node->left = new Node;
node->left->val = n;
node->left->left = nullptr;
node->left->right = nullptr;
}
}
else if (n >= node->val)
{
if (node->right != nullptr)
{
addValue(n, node->right);
}
else
{
node->right = new Node;
node->right->val = n;
node->right->left = nullptr;
node->right->right = nullptr;
}
}
}
void BTree::traverse(Node *node) {
if (node->left != nullptr)
traverse(node->left);
std::cout val right != nullptr)
traverse(node->right);
}
Node BTree::search(int n, Node node) {
if (node != nullptr)
{
if (node->val == n)
return node;
else if (n val)
return search(n, node->left);
else if (n > node->val)
return search(n, node->right);
}
else
return nullptr;
}
void BTree::addValue(int n) {
if (root == nullptr)
{
root = new Node;
root->val = n;
root->left = nullptr;
root->right = nullptr;
}
else
{
addValue(n, root);
}
}
void BTree::traverse() {
if (root ==
node.h
#pragma once
struct Node
{
int val;
Node *left;
Node *right;
};bTree.h
#include "node.h"
#pragma once
class BTree
{
private:
Node *root;
void addValue(int n, Node *node);
void traverse(Node *node);
Node* search(int n, Node *node);
public:
BTree();
void addValue(int n);
void traverse();
void search(int n);
};bTree.cpp
```
#include
#include "bTree.h"
BTree::BTree()
{
root = nullptr;
}
void BTree::addValue(int n, Node *node) {
if (n val)
{
if (node->left != nullptr)
{
addValue(n, node->left);
}
else
{
node->left = new Node;
node->left->val = n;
node->left->left = nullptr;
node->left->right = nullptr;
}
}
else if (n >= node->val)
{
if (node->right != nullptr)
{
addValue(n, node->right);
}
else
{
node->right = new Node;
node->right->val = n;
node->right->left = nullptr;
node->right->right = nullptr;
}
}
}
void BTree::traverse(Node *node) {
if (node->left != nullptr)
traverse(node->left);
std::cout val right != nullptr)
traverse(node->right);
}
Node BTree::search(int n, Node node) {
if (node != nullptr)
{
if (node->val == n)
return node;
else if (n val)
return search(n, node->left);
else if (n > node->val)
return search(n, node->right);
}
else
return nullptr;
}
void BTree::addValue(int n) {
if (root == nullptr)
{
root = new Node;
root->val = n;
root->left = nullptr;
root->right = nullptr;
}
else
{
addValue(n, root);
}
}
void BTree::traverse() {
if (root ==
Solution
Use constructors to initialize your objects.
This should be replaced by:
Design
I think you will find that your code becomes a lot simpler if you move your test for
For Example:
I would write addValue like this:
root = new Node;
root->val = n;
root->left = nullptr;
root->right = nullptr;This should be replaced by:
root = new Node(n);Design
I think you will find that your code becomes a lot simpler if you move your test for
null out of the public members. Then make the test for null the first thing you do in the private member (Don't test before you call the private member test as the first action in the call). This will simplify all the situations were you currently have to test for null.For Example:
I would write addValue like this:
void BTree::addValue(int n) {
root = addValue(n, root);
}
Node* BTree::addValue(int n, Node* current) {
if (current == nullptr) {
return new Node(n);
}
if (n val) {
current->left = addValue(n, current->left);
else if (n > current->val) {
current->right = addValue(n, current->right);
}
return current;
}Code Snippets
root = new Node;
root->val = n;
root->left = nullptr;
root->right = nullptr;root = new Node(n);void BTree::addValue(int n) {
root = addValue(n, root);
}
Node* BTree::addValue(int n, Node* current) {
if (current == nullptr) {
return new Node(n);
}
if (n < current->val) {
current->left = addValue(n, current->left);
else if (n > current->val) {
current->right = addValue(n, current->right);
}
return current;
}Context
StackExchange Code Review Q#160198, answer score: 6
Revisions (0)
No revisions yet.