-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreesnodespath.cpp
More file actions
63 lines (53 loc) · 1.09 KB
/
Copy pathtreesnodespath.cpp
File metadata and controls
63 lines (53 loc) · 1.09 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
#include <iostream>
using namespace std;
struct node {
int data;
struct node* lchild;
struct node* rchild;
struct node* parent;
};
typedef struct node Node;
class Tree {
private:
Node* _root;
Node* search(const int data, Node* node);
void insert(Node* node, Node* node);
public:
Tree():_root(nullptr){};
void insert(const int data);
int distance(const int d1, const int d2);
};
Node* Tree::search(const int data, Node* node) {
if(node->data == data)
return node;
search(data, node->lchild);
search(data, node->rchild);
}
void Tree::insert(const int data) {
Node* node = new Node;
node->data = data;
node->lchild = nullptr;
node->rchild = nullptr;
node->parent = nullptr;
if(!_root) {
_root = node;
node->parent = nullptr;
}
else
insert(_root, node);
}
bool Tree::insert(Node* tree, Node* node) {
if(!tree->lchild) {
tree->lchild = node;
node->parent = tree;
return true;
}
if(!tree->rchild) {
tree->rchild = node;
node->parent = tree;
return true;
}
if(insert(tree->lchild, node)) return true;
if(insert(tree->rchild, node)) return true;
return false;
}