Skip to content

Commit 903b3c8

Browse files
committed
adding example files by folder
1 parent a079ee8 commit 903b3c8

File tree

154 files changed

+3864
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+3864
-0
lines changed

Chapter1/chapter1.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*var david = {
2+
first_name: "David",
3+
last_name: "Durr"
4+
};
5+
6+
var student1 = {
7+
id: "1234",
8+
courses: ["Programming I", "English II", "Algebra"],
9+
advised: false
10+
};
11+
12+
console.log(david.last_name); // displays "Durr"
13+
console.log(student1["id"]); // displays "1234"
14+
console.log(student1.courses[1]); displays "English II"
15+
student1.advised = true;
16+
david.age = 50;*/
17+
function Person(first, middle, last) {
18+
this.first = first;
19+
this.middle = middle;
20+
this.last = last;
21+
this.initials = initials;
22+
}
23+
24+
function initials() {
25+
return this.first[0] + this.middle[0] + this.last[0];
26+
}
27+
28+
var aPerson = new Person("John","Quincy","Public");
29+
console.log("First name: " + aPerson.first);
30+
console.log("Middle name: " + aPerson.middle);
31+
console.log("Last name: " + aPerson.last);
32+
console.log("Initials: " + aPerson.initials());

Chapter10/Chap10-1.js

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
function Node(data, left, right) {
2+
this.data = data;
3+
this.left = left;
4+
this.right = right;
5+
this.show = show;
6+
}
7+
8+
function show() {
9+
return this.data;
10+
}
11+
12+
function BST() {
13+
this.root = null;
14+
this.insert = insert;
15+
this.inOrder = inOrder;
16+
this.preOrder = preOrder;
17+
this.postOrder = postOrder;
18+
this.getmin = getmin;
19+
this.getmax = getmax;
20+
this.find = find;
21+
this.remove = remove;
22+
this.removeNode = removeNode;
23+
this.getSmallest = getSmallest;
24+
}
25+
26+
function insert(data) {
27+
var n = new Node(data, null, null);
28+
if (this.root == null) {
29+
this.root = n;
30+
}
31+
else {
32+
var current = this.root;
33+
var parent;
34+
while (true) {
35+
parent = current;
36+
if (data < current.data) {
37+
current = current.left;
38+
if (current == null) {
39+
parent.left = n;
40+
break;
41+
}
42+
}
43+
else {
44+
current = current.right;
45+
if (current == null) {
46+
parent.right = n;
47+
break;
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
function inOrder(node) {
55+
if (!(node == null)) {
56+
inOrder(node.left);
57+
putstr(node.show() + " ");
58+
inOrder(node.right);
59+
}
60+
}
61+
62+
function preOrder(node) {
63+
if (!(node == null)) {
64+
putstr(node.show() + " ");
65+
preOrder(node.left);
66+
preOrder(node.right);
67+
}
68+
}
69+
70+
function postOrder(node) {
71+
if (!(node == null)) {
72+
postOrder(node.left);
73+
postOrder(node.right);
74+
putstr(node.show() + " ");
75+
}
76+
}
77+
78+
function getmin() {
79+
var current = this.root;
80+
print("debug: " + current.data);
81+
while (!(current.left == null)) {
82+
current = current.left;
83+
}
84+
return current.data;
85+
}
86+
87+
function getmax() {
88+
var current = this.root;
89+
while (!(current.right == null)) {
90+
current = current.right;
91+
}
92+
return current.data;
93+
}
94+
95+
function find(data) {
96+
var current = this.root;
97+
while (current.data != data) {
98+
if (data < current.data) {
99+
current = current.left;
100+
}
101+
else {
102+
current = current.right;
103+
}
104+
if (current == null) {
105+
return null;
106+
}
107+
}
108+
return current;
109+
}
110+
111+
function getSmallest(node) {
112+
if (node.left == null) {
113+
return node;
114+
}
115+
else {
116+
return getSmallest(node.left);
117+
}
118+
}
119+
120+
function remove(data) {
121+
root = removeNode(this.root, data);
122+
}
123+
124+
function removeNode(node, data) {
125+
if (node == null) {
126+
return null;
127+
}
128+
if (data == node.data) {
129+
// node has no children
130+
if (node.left == null && node.right == null) {
131+
return null;
132+
}
133+
// node has no left child
134+
if (node.left == null) {
135+
return node.right;
136+
}
137+
// node has no right child
138+
if (node.right == null) {
139+
return node.left;
140+
}
141+
// node has two children
142+
var tempNode = getSmallest(node.right);
143+
node.data = tempNode.data;
144+
node.right = removeNode(node.right, tempNode.data);
145+
return node;
146+
}
147+
else if (data < node.data) {
148+
node.left = removeNode(node.left, data);
149+
return node;
150+
}
151+
else {
152+
node.right = removeNode(node.right, data);
153+
return node;
154+
}
155+
}

Chapter10/Chap10-2.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("bst.js");
2+
var nums = new BST();
3+
nums.insert(23);
4+
nums.insert(45);
5+
nums.insert(16);
6+
nums.insert(37);
7+
nums.insert(3);
8+
nums.insert(99);
9+
nums.insert(22);
10+
print("Inorder traversal: ");
11+
inOrder(nums.root);

Chapter10/Chap10-3.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("bst.js");
2+
var nums = new BST();
3+
nums.insert(23);
4+
nums.insert(45);
5+
nums.insert(16);
6+
nums.insert(37);
7+
nums.insert(3);
8+
nums.insert(99);
9+
nums.insert(22);
10+
var min = nums.getmin();
11+
print("The minimum value of the BST is: " + min);
12+
print("\n");
13+
var max = nums.getmax();
14+
print("The maximum value of the BST is: " + max);

Chapter10/Chap10-4.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("bst.js");
2+
var nums = new BST();
3+
nums.insert(23);
4+
nums.insert(45);
5+
nums.insert(16);
6+
nums.insert(37);
7+
nums.insert(3);
8+
nums.insert(99);
9+
nums.insert(22);
10+
inOrder(nums.root);
11+
print("\n");
12+
putstr("Enter a value to search for: ");
13+
var value = parseInt(readline());
14+
var found = nums.find(value);
15+
if (found != null) {
16+
print("Found " + value + " in the BST.");
17+
}
18+
else {
19+
print(value + " was not found in the BST.");
20+
}

Chapter10/Chap10-5.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
load("bst.js");
2+
var grades = genArray(100);
3+
prArray(grades);
4+
var gradedistro = new BST();
5+
for (var i = 0; i < grades.length; ++i) {
6+
var g = grades[i];
7+
var grade = gradedistro.find(g);
8+
if (grade == null) {
9+
gradedistro.insert(g);
10+
}
11+
else {
12+
gradedistro.update(g);
13+
}
14+
}
15+
var cont = "y";
16+
17+
18+
while (cont == "y") {
19+
putstr("\n\nEnter a grade: ");
20+
var g = parseInt(readline());
21+
var aGrade = gradedistro.find(g);
22+
if (aGrade == null) {
23+
print("No occurrences of " + g);
24+
}
25+
else {
26+
print("Occurrences of " + g + ": " + aGrade.count);
27+
}
28+
putstr("Look at another grade (y/n)? ");
29+
cont = readline();
30+
}

Chapter10/Chapter9/Chap9-1.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
load("set.js");
2+
var names = new Set();
3+
names.add("David");
4+
names.add("Jennifer");
5+
names.add("Cynthia");
6+
names.add("Mike");
7+
names.add("Raymond");
8+
if (names.add("Mike")) {
9+
print("Mike added");
10+
}
11+
else {
12+
print("Can't add Mike, must already be in set.");
13+
}
14+
print(names.show());
15+
var removed = "Mike";
16+
if (names.remove("Mike")) {
17+
print(removed + " removed.");
18+
}
19+
else {
20+
print(removed + " not removed.");
21+
}
22+
names.add("Clayton");
23+
print(names.show());
24+
removed = "Alisa";
25+
if (names.remove("Mike")) {
26+
print(removed + " removed.");
27+
}
28+
else {
29+
print(removed + " not removed.");
30+
}

Chapter10/Chapter9/Chap9-2.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("set.js");
2+
var cis = new Set();
3+
cis.add("Mike");
4+
cis.add("Clayton");
5+
cis.add("Jennifer");
6+
cis.add("Raymond");
7+
var dmp = new Set();
8+
dmp.add("Raymond");
9+
dmp.add("Cynthia");
10+
dmp.add("Bryan");
11+
var it = new Set();
12+
it = cis.union(dmp);
13+
print(it.show());

Chapter10/Chapter9/Chap9-3.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
load("set.js");
2+
var cis = new Set();
3+
cis.add("Mike");
4+
cis.add("Clayton");
5+
cis.add("Jennifer");
6+
cis.add("Raymond");
7+
var dmp = new Set();
8+
dmp.add("Raymond");
9+
dmp.add("Cynthia");
10+
dmp.add("Bryan");
11+
var inter = cis.intersect(dmp);
12+
print(inter.show());

Chapter10/Chapter9/Chap9-4.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("set.js");
2+
var it = new Set();
3+
it.add("Cynthia");
4+
it.add("Clayton");
5+
it.add("Jennifer");
6+
it.add("Danny");
7+
it.add("Bryan");
8+
it.add("Terrill");
9+
it.add("Raymond");
10+
it.add("Mike");
11+
var dmp = new Set();
12+
dmp.add("Cynthia");
13+
dmp.add("Raymond");
14+
dmp.add("Bryan");
15+
if (dmp.subset(it)) {
16+
print("DMP is a subset of IT.");
17+
}
18+
else {
19+
print("DMP is not a subset of IT.");
20+
}

Chapter10/Chapter9/Chap9-5.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("set.js");
2+
var cis = new Set();
3+
var it = new Set();
4+
cis.add("Clayton");
5+
cis.add("Jennifer");
6+
cis.add("Danny");
7+
it.add("Bryan");
8+
it.add("Clayton");
9+
it.add("Jennifer");
10+
var diff = new Set();
11+
diff = cis.difference(it);
12+
print("[" + cis.show() + "] difference [" + it.show()
13+
+ "] -> [" + diff.show() + "]");
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("HashTable.js");
2+
var someNames = ["David", "Jennifer", "Donnie", "Raymond",
3+
"Cynthia", "Mike", "Clayton", "Danny", "Bryan"];
4+
var hTable = new HashTable();
5+
for (var i = 0; i < someNames.length; ++i) {
6+
hTable.put(someNames[i]);
7+
}
8+
hTable.showDistro();

0 commit comments

Comments
 (0)