-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.h
More file actions
82 lines (68 loc) · 1.52 KB
/
Copy pathbinarySearchTree.h
File metadata and controls
82 lines (68 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef BINARYSEARCHTREE_H
#define BINARYSEARCHTREE_H
#include "node.h"
class BinarySearchTree
{
private:
Node *root;
void insert(int, Node *&);
bool searchElement(int, Node *&);
void inOrderTraversal(Node *&);
public:
BinarySearchTree();
~BinarySearchTree();
void insert(int);
bool searchElement(int);
void inOrderTraversal();
};
BinarySearchTree::BinarySearchTree()
{
this->root = NULL;
}
BinarySearchTree::~BinarySearchTree()
{
delete this->root;
}
void BinarySearchTree::insert(int value, Node *&node)
{
if (node == NULL)
node = new Node(value);
else if (value <= node->getValue())
insert(value, node->getLeftChild());
else if (value > node->getValue())
insert(value, node->getRightChild());
}
bool BinarySearchTree::searchElement(int value, Node *&node)
{
if (node == NULL)
return false;
else if (value < node->getValue())
return searchElement(value, node->getLeftChild());
else if (value > node->getValue())
return searchElement(value, node->getRightChild());
else
return true;
}
void BinarySearchTree::inOrderTraversal(Node *&node)
{
if (node == NULL)
{
return;
}
inOrderTraversal(node->getLeftChild());
std::cout << node->getValue() << " ";
inOrderTraversal(node->getRightChild());
}
void BinarySearchTree::insert(int value)
{
insert(value, this->root);
}
bool BinarySearchTree::searchElement(int value)
{
return searchElement(value, this->root);
}
void BinarySearchTree::inOrderTraversal()
{
inOrderTraversal(this->root);
}
#endif // BINARYSEARCHTREE_H