-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.h
80 lines (53 loc) · 1.86 KB
/
BST.h
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
#ifndef BST_H_INCLUDED
#define BST_H_INCLUDED
#include <iostream>
#include <vector>
using namespace std;
/*
Basic Class Methods:
Constructor:Constructs empty BinarySearchTree
empty:returns bool to check if tree is empty
search: given an item the search method will search for the existence of such item in the tree
insert: Insert an item in the BinarySearchTree
remove:Removes an item from the tree
InOrder:Traverses the tree with order Left-Root-Right
Private Methods which contains the root node in:
searchAux: has the root f the tree in its arguments and search for item
InOrderAux: has the root f the tree in its arguments traverses the tree with order Left-Root-Right
levelOrderPrint: prints the tree's nodes level by level
*/
typedef int DataType;
class BST {
public:
BST();
~BST();
bool empty() const;
bool search(const DataType &item) const;
void insert(const DataType &item);
void remove(const DataType &item);
void InOrder(vector<DataType> &v);
void clear();
void PreOrder();
void PostOrder();
void levelOrderPrint();
public:
class BSTNode {
public:
DataType data;
BSTNode *left;
BSTNode *right;
BSTNode() : left(nullptr), right(nullptr) {}
BSTNode(DataType item) : data(item), left(nullptr), right(nullptr) {}
};
typedef BSTNode *NodePointer;
NodePointer root;
bool searchAux(BST::NodePointer subtree, const DataType &item) const;
void insertAux(BST::NodePointer &subtree, const DataType &item);
void search2(const DataType &item, bool &found, BST::NodePointer &locptr, BST::NodePointer &parent) const;
void InOrderAux(BST::NodePointer root, vector<DataType> &v);
void PostOrderAux(BST::NodePointer root);
void PreOrderAux(BST::NodePointer root);
void clearAux(NodePointer &root);
friend class screenBST;
};
#endif // BST_H_INCLUDED