-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.html
More file actions
36 lines (34 loc) · 827 Bytes
/
binarytree.html
File metadata and controls
36 lines (34 loc) · 827 Bytes
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<script type="text/javascript">
function addTree(word, vertex) {
var v = vertex;
if (v === null)
v = {word: word, counter: 1, left: null, right: null};
else if (word === v.word)
v.counter += 1;
else if (word < v.word)
v.left = addTree(word, v.left);
else
v.right = addTree(word, v.right);
return v;
}
function printTree(vertex) {
if (vertex === null)
return;
printTree(vertex.left);
document.write(vertex.word + ',' + vertex.counter + '<br>');
printTree(vertex.right);
}
var pattern = /[a-zA-Z]+/g;
var text = 'now is the time for all good men to come to the aid of their party';
var result;
var root = null;
while ((result = pattern.exec(text)) !== null)
root = addTree(result[0], root);
printTree(root);
</script>
</body>
</html>