-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree2bt.cpp
More file actions
75 lines (68 loc) · 1.35 KB
/
tree2bt.cpp
File metadata and controls
75 lines (68 loc) · 1.35 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
64
65
66
67
68
69
70
71
72
73
74
75
#include<bits/stdc++.h>
#define SPACES 10;
using namespace std;
struct node {
int value;
node *son;
node *next;
node(int v) {
value = v;
son = NULL;
next = NULL;
}
};
node *newTree() {
node *tree;
int v;
bool children, siblings;
cout << "Enter node value: ";
cin >> v;
tree = new node(v);
cout << "Does the node" << v << " have children? (1/0): ";
cin >> children;
if(children) {
cout << "Enter Child Data: \n";
tree->son = newTree();
}
cout << "Does the node" << v << " have siblings? (1/0): ";
cin >> siblings;
if(siblings) {
cout << "Enter Sibling Data: \n";
tree->next = newTree();
}
return tree;
}
void displayTree(node *root, int space = 0) {
if(!(root == NULL)) {
displayTree(root->next, space);
cout << endl;
for(int i = 0; i < space; i++) {
cout << " ";
}
cout << root->value;
space += SPACES;
displayTree(root->son, space);
}
}
void displayBinaryTree(node *root, int space = 0) {
if(!(root == NULL)) {
space += SPACES;
displayBinaryTree(root->next, space);
cout << endl;
int i = SPACES;
for (; i < space; i++) {
cout<<" ";
}
cout << root->value;
displayBinaryTree(root->son, space);
}
}
int main() {
node *root = newTree();
cout << "Tree Representation: ";
displayTree(root);
cout << endl;
cout << "Binary Tree Representation: ";
displayBinaryTree(root);
cout << endl;
}