-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeJs.js
More file actions
171 lines (165 loc) · 4.39 KB
/
Copy pathTreeJs.js
File metadata and controls
171 lines (165 loc) · 4.39 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var Tree = function (value) {
this.value = value;
this.children = [];
}
var CountleafE = 0 ;
Tree.prototype.countHLeafE = function () {
var node = new Tree();
function recurse (node) {
if (node.children.length == 0 ) {
if(node.value== "E"){CountleafE++;}
}
for (var i = 0; i < node.children.length; i++) {
recurse(node.children[i]);
}
}
recurse(this);
return CountleafE;
}
Tree.prototype.countHLeafG = function () {
var node = new Tree();
var countHLeafG = 0 ;
function recurse (node) {
if (node.children.length === 0) {
if(node.value== "G"){countHLeafG++;}
}
for (var i = 0; i < node.children.length; i++) {
recurse(node.children[i]);
}
}
recurse(this);
return countHLeafG;
}
Tree.prototype.countHLeafI = function () {
var node = new Tree();
var countHLeafI= 0;
function recurse (node) {
if (node.children.length === 0) {
if(node.value== "I"){countHLeafI++;}
}
for (var i = 0; i < node.children.length; i++) {
recurse(node.children[i]);
}
}
recurse(this);
return countHLeafI;
}
Tree.prototype.countHLeafH = function () {
var node = new Tree();
var countHLeafH= 0;
function recurse (node) {
if (node.children.length === 0) {
if(node.value== "H"){countHLeafH++;}
}
for (var i = 0; i < node.children.length; i++) {
recurse(node.children[i]);
}
}
recurse(this);
return countHLeafH;
}
Tree.prototype.addChild = function(child){
if (!child || !(child instanceof Tree)){
child = new Tree(child);
}
if(!this.isDescendant(child)){
this.children.push(child);
}else {
}
// return the new child node for convenience
return child;
};
6
Tree.prototype.isDescendant = function(child){
if(this.children.indexOf(child) !== -1){
// `child` is an immediate child of this tree
return true;
}else{
for(var i = 0; i < this.children.length; i++){
if(this.children[i].isDescendant(child)){
// `child` is descendant of this tree
return true;
}
}
return false;
}
};
var root = new Tree("A");
var nodeB = new Tree("B");
var nodeC = new Tree("C");
root.addChild(nodeB);
root.addChild(nodeC);
var nodeD = new Tree("D");
var nodeE = new Tree("E");
nodeB.addChild(nodeE);
nodeB.addChild(nodeD);
var nodeF = new Tree("F");
var nodeG = new Tree("G");
nodeC.addChild(nodeF);
nodeC.addChild(nodeG);
var nodeI = new Tree("I");
var nodeH = new Tree("H");
nodeF.addChild(nodeI);
nodeF.addChild(nodeH);
nodeD.addChild(nodeF);
nodeD.addChild(nodeC);
var CountOfB = 0 ;
var CountOfC = 0 ;
var CountOfD = 0 ;
var CountOfE = 0 ;
var CountOfF = 0 ;
var CountOfG = 0 ;
var CountOfH = 0 ;
var CountOfI = 0 ;
Tree.prototype.CountwittraversDF=function(callback) {
function recurse(data) {
for (var i = 0; i < data.children.length; i++) {
recurse(data.children[i]);
if (data.children[i].value =="B") {
CountOfB++;
}
if (data.children[i].value =="C") {
CountOfC++;
}
if (data.children[i].value =="D") {
CountOfD++;
}
if (data.children[i].value =="E") {
CountOfE++;
}
if (data.children[i].value =="F") {
CountOfF++;
}
if (data.children[i].value =="G") {
CountOfG++;
}
if (data.children[i].value =="H") {
CountOfH++;
}
if (data.children[i].value =="I") {
CountOfI++;
}
}
callback(data);
}
recurse(this);
};
/////////////////////////////////////////////////////////////////
var str =root;
root.CountwittraversDF(function(data){
// console.log(data);
});
//var countt = 1;
var countt = prompt("Enter count of tree");
console.log("Count of B node with Value and Coefficient :"+CountOfB*3*countt);
console.log("Count of C node with value and Coefficient :"+CountOfC*2*countt);
console.log("Count of D node with value and Coefficient :"+CountOfD*7*countt);
console.log("Count of E node with value and Coefficient :"+CountOfE*5*countt);
console.log("Count of F node with value and Coefficient :"+CountOfF*2*countt);
console.log("Count of G node with value and Coefficient :"+CountOfG*5*countt);
console.log("Count of H node with value and Coefficient :"+CountOfH*3*countt);
console.log("Count of I node with value and Coefficient :"+CountOfI*5*countt);
console.log("Count of leaf E "+root.countHLeafE());
console.log("Count of leaf G "+root.countHLeafG());
console.log("Count of leaf I "+root.countHLeafI());
console.log("Count of leaf H "+root.countHLeafH());