-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (78 loc) · 2.96 KB
/
script.js
File metadata and controls
94 lines (78 loc) · 2.96 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
import Tree from "./tree.js";
import Node from "./node.js";
const prettyPrint = (node, prefix = "", isLeft = true) => {
if (node === null) {
return;
}
if (node.rightChild !== null) {
prettyPrint(node.rightChild, `${prefix}${isLeft ? "│ " : " "}`, false);
}
console.log(`${prefix}${isLeft ? "└── " : "┌── "}${node.data}`);
if (node.leftChild !== null) {
prettyPrint(node.leftChild, `${prefix}${isLeft ? " " : "│ "}`, true);
}
};
const createRandomArray = (size = 10) => {
const array = [];
for (let i = 0; i < size; i++) {
const randomNumber = Math.floor(Math.random() * 100); // Random numbers 0-99
array.push(randomNumber);
}
return array;
};
//Step 1: Create a binary search tree from an array of random numbers
const randomArray = createRandomArray();
const tree = new Tree();
tree.buildTree(randomArray);
prettyPrint(tree.root);
//Step 2: Confirm that the tree is balanced
console.log("\nIs Tree balanced: " + tree.isBalanced());
//Step 3: Print out all elements in level, pre, post, and in order
console.log("\nTesting iterative level order traversal:");
const iterativeResult = [];
tree.levelOrder((node) => iterativeResult.push(node.data));
console.log(iterativeResult);
console.log("Testing Preorder traversal:");
const preOrderResult = [];
tree.preOrder((node) => preOrderResult.push(node.data));
console.log(preOrderResult);
console.log("Testing Inorder traversal:");
const inOrderResult = [];
tree.inOrder((node) => inOrderResult.push(node.data));
console.log(inOrderResult);
console.log("Testing Postorder traversal:");
const postOrderResult = [];
tree.postOrder((node) => postOrderResult.push(node.data));
console.log(postOrderResult);
//Step 4: Unbalance the tree by adding several numbers > 100
console.log("\nUnbalancing the tree with numbers > 100...");
tree.insert(101);
tree.insert(102);
tree.insert(103);
tree.insert(104);
prettyPrint(tree.root);
//Step 5: Confirm that the tree is unbalanced by calling isBalanced
console.log("\nIs Tree balanced: " + tree.isBalanced());
//Step 6: Balance the tree by calling rebalance
tree.rebalance();
console.log("");
prettyPrint(tree.root);
//Step 7: Confirm that the tree is balanced by calling isBalanced
console.log("\nIs Tree balanced: " + tree.isBalanced());
//Step 8: Print out all elements in level, pre, post, and in order
console.log("\nTesting iterative level order traversal:");
const iterativeResult2 = [];
tree.levelOrder((node) => iterativeResult2.push(node.data));
console.log(iterativeResult2);
console.log("Testing Preorder traversal:");
const preOrderResult2 = [];
tree.preOrder((node) => preOrderResult2.push(node.data));
console.log(preOrderResult2);
console.log("Testing Inorder traversal:");
const inOrderResult2 = [];
tree.inOrder((node) => inOrderResult2.push(node.data));
console.log(inOrderResult2);
console.log("Testing Postorder traversal:");
const postOrderResult2 = [];
tree.postOrder((node) => postOrderResult2.push(node.data));
console.log(postOrderResult2);