-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.js
More file actions
117 lines (104 loc) · 1.79 KB
/
Copy pathbinaryTree.js
File metadata and controls
117 lines (104 loc) · 1.79 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
function node(data)
{
this.data=data;
this.left=null;
this.right=null;
this.spaces=0;
}
function height(node)
{
if(node == null)
{
return 1;
}
else
{
return max(height(root.left,root.right))+1;
}
}
function getMaxDistance(node)
{
if(node==null)
{
return 0;
}
else
{
return max(height(node.left)+height(node.right)+1,getMaxDistance(node.right),getMaxDistance(node.left));
}
}
function max(a,b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
function driverFunction()
{
var nodeArray=[];
for(var i=0;i<10;i++)
{
nodeArray.push(new node(i));
}
nodeArray[0].left=nodeArray[1];
nodeArray[0].right=nodeArray[2];
nodeArray[1].left=nodeArray[3];
nodeArray[1].right=nodeArray[4];
nodeArray[2].left=nodeArray[5];
nodeArray[2].right=nodeArray[6];
nodeArray[3].left=nodeArray[7];
nodeArray[3].right=nodeArray[8];
nodeArray[4].left=nodeArray[9];
addSpaces(nodeArray[0]);
normalizeSpaces(nodeArray[0]);
console.log(nodeArray);
}
function addSpaces(node)
{
console.log(node.data);
if(node.left)
{
console.log('left');
node.left.spaces= node.spaces-1;
}
if(node.right)
{
console.log('right');
node.right.spaces+=node.spaces+1;
}
if(node.left)
{
addSpaces(node.left);
}
if(node.right)
{
addSpaces(node.right);
}
//console.log(node.spaces);
return node.spaces;
}
function getMinimum(root)
{
var min=root.spaces;
var node=root;
while(node.left)
{
min=node.spaces;
node=node.left;
}
return min;
}
function normalizeSpaces(root)
{
root.spaces-=getMinimum(root);
if(root.left)
normalizeSpaces(root.left);
if(root.right)
normalizeSpaces(root.right);
}
driverFunction();