Skip to content

Commit ad7e47a

Browse files
committed
Merge branch '2024_sem2_dev' of https://github.com/JarrettChen217/ITP_team025_staticsound into 2024_sem2_dev
2 parents d5e411d + 5733efc commit ad7e47a

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/algorithms/controllers/AVLTreeSearch.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/**
2+
* This file contains the AVL Tree Search algorithm,
3+
* alongside the visualisation code.
4+
*
5+
* The AVL Tree Search algorithm is used to find a node.
6+
*
7+
* The search algorithm is based on the tree created by the insertion algorithm.
8+
* By accessing the visualized AVL tree, it retrieves the complete tree structure.
9+
* The input is a node to be searched for, and the algorithm starts from the root,
10+
* traversing down to the child nodes to find the target node.
11+
*
12+
* @author StaticSound Team
13+
* - Hao Chen (1314613) <[email protected]>
14+
* - Jiayi Sun (1305340) <[email protected]>
15+
* - Junhao Zhu (1261757) <[email protected]>
16+
* - Ziyu Wang (1243302) <[email protected]>
17+
* - Gaoyongle Zhang (1346309) <[email protected]>
18+
*
19+
* @since 08/10/2024
20+
*/
21+
122
export default {
223
/**
324
* For the search algorithm, we use the tree that is created in
@@ -26,31 +47,26 @@ export default {
2647
const tree = visualiser.graph.instance.getTree();
2748
let root = visualiser.graph.instance.getRoot();
2849

29-
console.log('tree:', Object.keys(tree));
30-
3150
let current = root;
3251
let parent = null;
3352

3453
chunker.add('AVL_Search(t, k)', (vis) => {
3554
vis.graph.setFunctionInsertText(" (" + target + ") ");
3655
vis.graph.setFunctionName("AVL_Search");
3756
});
38-
// chunker.add('while t not Empty', (vis, c, p) => vis.graph.visit(c, p), [current, parent]);
3957
chunker.add('while t not Empty');
4058

4159
let ptr = tree;
4260
parent = current;
4361

4462
while (ptr) {
45-
// chunker.add('n = root(t)');
63+
4664
chunker.add('n = root(t)', (vis, c, p) => vis.graph.visit(c, p), [current, parent]);
4765
let node = current;
4866
chunker.add('if n.key = k');
4967
if (node === target) {
5068
chunker.add('if n.key = k', (vis, c, p) => vis.graph.leave(c, p), [node, parent]);
5169
chunker.add('return t', (vis, c, p) => vis.graph.select(c, p), [node, parent]);
52-
// for test
53-
console.log('success! found the target!');
5470
return 'success';
5571
}
5672

@@ -61,7 +77,7 @@ export default {
6177
parent = node;
6278
current = tree[node].left;
6379
ptr = tree[node];
64-
// chunker.add('t <- n.left', (vis, c, p) => vis.graph.visit(c, p), [node, parent]);
80+
6581
chunker.add('t <- n.left');
6682
} else {
6783
break;
@@ -71,26 +87,14 @@ export default {
7187
parent = node;
7288
current = tree[node].right;
7389
ptr = tree[node];
74-
// chunker.add('t <- n.right', (vis, c, p) => vis.graph.visit(c, p), [node, parent]);
90+
7591
chunker.add('t <- n.right');
7692
} else {
7793
break;
7894
}
7995
}
80-
// chunker.add('return NotFound');
81-
// for test
82-
// chunker.add('return NotFound', (vis, final) => {
83-
// console.log('chunker.add called');
84-
// const ResultStr = 'NotFound';
85-
// console.log('ResultStr:', ResultStr);
86-
// console.log('vis:', vis);
87-
// console.log('final:', final);
88-
// vis.graph.addResult(ResultStr, final);
89-
// console.log('vis.graph.addResult called');
90-
// }, [final]);
91-
console.log('root:', root);
96+
9297
chunker.add('return NotFound', (vis) => vis.graph.setText('RESULT NOT FOUND'));
93-
console.log('fail! target not found!');
9498
return 'fail';
9599
},
96100
};

0 commit comments

Comments
 (0)