-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg_Tree.cpp
More file actions
82 lines (62 loc) · 1.64 KB
/
org_Tree.cpp
File metadata and controls
82 lines (62 loc) · 1.64 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
76
77
78
79
80
81
82
#include<iostream>
#include<queue>
struct node{
std::string position;
node * frist;
node * second;
};
class org_tree{
node* root;
public:
static org_tree create_org_structure(const std::string& pos)
{
org_tree tree;
tree.root=new node{pos,NULL,NULL};
return tree;
}
static node* find(node* root,const std::string& value)
{
if(root==NULL)
return NULL;
if(root->position==value)
return root;
auto firstNode=org_tree::find(root->frist,value);
if(firstNode!=NULL)
return firstNode;
return org_tree::find(root->second,value);
}
bool addSubordinate(const std::string& manager,const std::string& subordinate)
{
auto managerNode=org_tree::find(root,manager);
if(!managerNode)
{
std::cout<<manager<<" is not found"<<std::endl;
return false;
}
if(managerNode->frist&&managerNode->second)
{
std::cout<<"Unable to add "<<subordinate<<" under "<<manager<<std::endl;
return false;
}
if(!managerNode->frist)
{
managerNode->frist=new node{subordinate,NULL,NULL};
}
else
managerNode->second=new node{subordinate,NULL,NULL};
std::cout<<"Add "<<subordinate<<" Under "<<manager<<std::endl;
return true;
}
};
int main()
{
auto tree=org_tree::create_org_structure("CEO");
tree.addSubordinate("CEO","COO");
tree.addSubordinate("COO","IT");
tree.addSubordinate("COO","POWER");
tree.addSubordinate("IT","SUB");
tree.addSubordinate("IT","FIND");
tree.addSubordinate("POWER","LESS");
tree.addSubordinate("POWER","CURRENT");
tree.addSubordinate("COO","MOTH");
}