-
Notifications
You must be signed in to change notification settings - Fork 1
/
rootedtree.cpp
86 lines (49 loc) · 1.26 KB
/
rootedtree.cpp
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
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
struct node
{
char data;
struct node* left;
struct node* right;
};
int search(std::string arr, int strt, int end, char value);
struct node* newNode(char data);
struct node* buildTree(std::string in, std::string pre, int inStrt, int inEnd)
{
static int preIndex = 0;
if (inStrt > inEnd)
return NULL;
struct node* tNode = newNode(pre[preIndex++]);
if (inStrt == inEnd)
return tNode;
int inIndex = search(in, inStrt, inEnd, tNode->data);
tNode->left = buildTree(in, pre, inStrt, inIndex - 1);
tNode->right = buildTree(in, pre, inIndex + 1, inEnd);
return tNode;
}
int search(std::string arr, int strt, int end, char value)
{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
return i;
}
}
struct node* newNode(char data)
{
struct node* node = new struct node;
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int main()
{
std::string in, pre;
std::cin>> in;
std::cin>> pre;
char* arr=NULL;
int len = in.length();
struct node* root = buildTree(in, pre, 0, len - 1);
return 0;
}