forked from ucsb-cs24-mirza-s21/lab04_data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintbst.h
59 lines (46 loc) · 2.27 KB
/
intbst.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
// intbst.h
// Binary search tree header file for CS 24 lab
#ifndef INTBST_H
#define INTBST_H
#include <iostream>
using namespace std;
class IntBST {
public:
// ctor, dtor, insert and one print method already done in intbst.cpp:
IntBST(); // constructor
~IntBST(); // destructor
bool insert(int value); // insert value; return false if duplicate
void printPreOrder() const; // prints tree data pre-order to cout
// 8 METHODS YOU MUST IMPLEMENT in intbst.cpp:
void printInOrder() const; // print tree data in-order to cout
void printPostOrder() const; // print tree data post-order to cout
int sum() const; // sum of all values
int count() const; // count of values
bool contains(int value) const; // true if value is in tree
// THESE ARE HARDER! DO THESE LAST
int getPredecessor(int value) const; // returns the predecessor value of the given value or 0 if there is none
int getSuccessor(int value) const; // returns the successor value of the given value or 0 if there is none
bool remove(int value); // deletes the Node containing the given value from the tree
private:
struct Node {
int info;
Node *left, *right, * parent;
// useful constructor:
Node(int v=0) : info(v), left(0), right(0), parent(0) { }
};
// just one instance variable (pointer to root node):
Node *root;
// recursive utility functions for use by public methods above
Node* getNodeFor(int value, Node* n) const; // IMPLEMENT THIS FIRST: returns the node for a given value or NULL if none exists
void clear(Node *n); // for destructor
bool insert(int value, Node *n); // note overloading names for simplicity
void printPreOrder(Node *n) const;
void printInOrder(Node *n) const;
void printPostOrder(Node *n) const;
int sum(Node *n) const;
int count(Node *n) const;
// these should be used by getPredecessor and getSuccessor, and ONE of them should be used by remove
Node* getSuccessorNode(int value) const; // returns the Node containing the successor of the given value
Node* getPredecessorNode(int value) const; // returns the Node containing the predecessor of the given value
};
#endif