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
+
1
22
export default {
2
23
/**
3
24
* For the search algorithm, we use the tree that is created in
@@ -26,31 +47,26 @@ export default {
26
47
const tree = visualiser . graph . instance . getTree ( ) ;
27
48
let root = visualiser . graph . instance . getRoot ( ) ;
28
49
29
- console . log ( 'tree:' , Object . keys ( tree ) ) ;
30
-
31
50
let current = root ;
32
51
let parent = null ;
33
52
34
53
chunker . add ( 'AVL_Search(t, k)' , ( vis ) => {
35
54
vis . graph . setFunctionInsertText ( " (" + target + ") " ) ;
36
55
vis . graph . setFunctionName ( "AVL_Search" ) ;
37
56
} ) ;
38
- // chunker.add('while t not Empty', (vis, c, p) => vis.graph.visit(c, p), [current, parent]);
39
57
chunker . add ( 'while t not Empty' ) ;
40
58
41
59
let ptr = tree ;
42
60
parent = current ;
43
61
44
62
while ( ptr ) {
45
- // chunker.add('n = root(t)');
63
+
46
64
chunker . add ( 'n = root(t)' , ( vis , c , p ) => vis . graph . visit ( c , p ) , [ current , parent ] ) ;
47
65
let node = current ;
48
66
chunker . add ( 'if n.key = k' ) ;
49
67
if ( node === target ) {
50
68
chunker . add ( 'if n.key = k' , ( vis , c , p ) => vis . graph . leave ( c , p ) , [ node , parent ] ) ;
51
69
chunker . add ( 'return t' , ( vis , c , p ) => vis . graph . select ( c , p ) , [ node , parent ] ) ;
52
- // for test
53
- console . log ( 'success! found the target!' ) ;
54
70
return 'success' ;
55
71
}
56
72
@@ -61,7 +77,7 @@ export default {
61
77
parent = node ;
62
78
current = tree [ node ] . left ;
63
79
ptr = tree [ node ] ;
64
- // chunker.add('t <- n.left', (vis, c, p) => vis.graph.visit(c, p), [node, parent]);
80
+
65
81
chunker . add ( 't <- n.left' ) ;
66
82
} else {
67
83
break ;
@@ -71,26 +87,14 @@ export default {
71
87
parent = node ;
72
88
current = tree [ node ] . right ;
73
89
ptr = tree [ node ] ;
74
- // chunker.add('t <- n.right', (vis, c, p) => vis.graph.visit(c, p), [node, parent]);
90
+
75
91
chunker . add ( 't <- n.right' ) ;
76
92
} else {
77
93
break ;
78
94
}
79
95
}
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
+
92
97
chunker . add ( 'return NotFound' , ( vis ) => vis . graph . setText ( 'RESULT NOT FOUND' ) ) ;
93
- console . log ( 'fail! target not found!' ) ;
94
98
return 'fail' ;
95
99
} ,
96
100
} ;
0 commit comments