|
2 | 2 |
|
3 | 3 | import { useState } from 'react'; |
4 | 4 | import Navbar from '@/components/navbar'; |
5 | | -import { fromValues, insertActions, deleteActions, searchActions } from '@/lib/algorithms/bst'; |
| 5 | +import * as bst from '@/lib/algorithms/bst'; |
| 6 | +import * as redBlack from '@/lib/algorithms/redBlack'; |
6 | 7 | import { binaryLayout } from '@/components/tree/layout'; |
7 | 8 | import { useTreeEditor } from '@/components/tree/use-tree-editor'; |
8 | 9 | import TreeCanvas from '@/components/tree/tree-canvas'; |
9 | 10 | import TreeMenu from '@/components/tree/tree-menu'; |
10 | 11 |
|
| 12 | +const MODES = ['Binary Search Tree', 'Red-Black Tree']; |
| 13 | +const SEED = [50, 30, 70, 20, 40, 60, 80]; |
| 14 | +const seedFor = (mode) => (mode === 1 ? redBlack.fromValues(SEED) : bst.fromValues(SEED)); |
| 15 | + |
11 | 16 | export default function Bst() { |
12 | | - const [initialTree] = useState(() => fromValues([50, 30, 70, 20, 40, 60, 80])); |
| 17 | + const [mode, setMode] = useState(0); |
| 18 | + const [initialTree] = useState(() => seedFor(0)); |
13 | 19 | const g = useTreeEditor({ initialTree }); |
| 20 | + const algo = mode === 1 ? redBlack : bst; |
| 21 | + |
| 22 | + const onModeChange = (m) => { |
| 23 | + if (g.isRunning) return; |
| 24 | + setMode(m); |
| 25 | + g.reset(seedFor(m)); |
| 26 | + }; |
14 | 27 |
|
15 | 28 | return ( |
16 | 29 | <div className="flex flex-col h-screen"> |
17 | 30 | <Navbar /> |
18 | 31 | <div className="flex flex-1 overflow-hidden"> |
19 | 32 | <TreeMenu |
20 | | - title="Binary Search Tree" |
| 33 | + title={MODES[mode]} |
| 34 | + modes={MODES} |
| 35 | + onModeChange={onModeChange} |
21 | 36 | disabled={g.isRunning} |
22 | | - onInsert={(v) => g.run(insertActions(g.getContext().tree, v))} |
23 | | - onDelete={(v) => g.run(deleteActions(g.getContext().tree, v))} |
24 | | - onSearch={(v) => g.run(searchActions(g.getContext().tree, v))} |
| 37 | + onInsert={(v) => g.run(algo.insertActions(g.getContext().tree, v))} |
| 38 | + onDelete={(v) => g.run(algo.deleteActions(g.getContext().tree, v))} |
| 39 | + onSearch={(v) => g.run(algo.searchActions(g.getContext().tree, v))} |
25 | 40 | onClear={g.clear} |
26 | 41 | onSpeedChange={g.setSpeed} |
27 | 42 | /> |
|
0 commit comments