-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
34 lines (28 loc) · 732 Bytes
/
Copy pathtree.cpp
File metadata and controls
34 lines (28 loc) · 732 Bytes
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
#include <iostream>
using namespace std;
struct tree_node{
int data;
tree_node* left_child;
tree_node* right_child;
tree_node(int val) : data(val), left_child(nullptr), right_child(nullptr) {}
};
class binary_tree{
public:
tree_node* root;
binary_tree(): root(nullptr){}
void insert(int val){
root = insert_node(root, val);
}
private:
tree_node* insert_node(tree_node* node, int val){
if(node == nullptr){
return new tree_node(val);
}
if(val<node->data){
node->left_child = insert_node(node->left_child, val);
} else{
node->right_child = insert_node(node->right_child, val);
}
return node;
}
};