-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trees.h
56 lines (48 loc) · 1.12 KB
/
Trees.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
//
// Created by ehsan on 4/5/17.
//
#ifndef DATASTRUCTURES_TREES_H
#define DATASTRUCTURES_TREES_H
#include <list>
using namespace std;
typedef int Elem;
class LinkedBinaryTree{
protected:
struct Node{
Elem elt;
Node* parent;
Node* left;
Node* right;
Node():elt(),parent(NULL),left(NULL),right(NULL){}
};
public:
// position in a tree
class Position{
private:
Node* _v;
public:
Position(Node* v):_v(v){}
Elem& operator*();
Position left() const;
Position right() const;
Position parent() const;
bool isRoot() const;
bool isExternal() const;
friend class LinkedBinaryTree; // Give tree access
};
public:
LinkedBinaryTree();
int size() const;
bool empty() const;
Position root() const;
list<Position> getAllPositions() const;
void addRoot();
void expandExternal(const Position& p);
Position removeAboveExternal(const Position& p);
public:
void preorder(Node* v, list<Position>& pl) const;
private:
Node* _root;
int n;
};
#endif //DATASTRUCTURES_TREES_H