Skip to content

Commit 9e87e42

Browse files
author
lucifer
committed
WIP: 树的可视化
1 parent 4389d6b commit 9e87e42

File tree

7 files changed

+251
-23
lines changed

7 files changed

+251
-23
lines changed

package-lock.json

Lines changed: 29 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import ProblemDetail from "./Detail";
99
import TagOrLink from "./TagOrLink";
1010
import tempaltes from "./codeTemplates/index";
1111
import Codes from "./codes";
12-
12+
import { bfs } from "./utils";
13+
import drawTree from "canvas-binary-tree";
1314
import "antd/dist/antd.css";
1415
import "./App.css";
16+
import { data as a } from "./db/binary-tree";
1517

1618
const { problems } = db;
1719
const { TabPane } = Tabs;
@@ -64,8 +66,6 @@ const columns = [
6466
},
6567
];
6668

67-
const pages = ["allSolutions", "templates", "detail"];
68-
6969
function App() {
7070
// eslint-disable-next-line
7171
chrome.tabs &&
@@ -89,8 +89,33 @@ function App() {
8989

9090
if (!inLeetCode) return window.open(LEETCODE_CN_URL + "/problemset/all/");
9191

92+
// setTimeout(() => {
93+
// const canvas = document.querySelector("#canvas");
94+
95+
// drawTree(canvas, bfs([1, 2, 2, 3, 1, 2, 2, 323213213232329]));
96+
// }, 1000);
97+
console.log(a);
98+
9299
return (
93100
<div className="container">
101+
<div
102+
className="tree-vis"
103+
style={{
104+
position: "fixed",
105+
zIndex: 99,
106+
top: 0,
107+
bottom: 0,
108+
left: 0,
109+
right: 0,
110+
backgroundColor: "rgba(0,0,0,.4)",
111+
}}
112+
>
113+
<div>
114+
<pre>{a}</pre>
115+
</div>
116+
<canvas width="1000" height="1000" id="canvas"></canvas>
117+
</div>
118+
94119
<div>
95120
<div className="guide">
96121
{page !== "" ? (

src/Detail.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React, { Fragment } from "react";
2-
import { Button, Tabs, Collapse, message } from "antd";
3-
import marked from "marked";
4-
import hljs from "highlight.js";
2+
import { Button, Tabs, Collapse } from "antd";
3+
54
import "highlight.js/styles/github.css";
65

76
import {
@@ -10,15 +9,13 @@ import {
109
CONTRIBUTE_COMPANY_URL,
1110
} from "./constant/index";
1211
import db from "./db/db";
13-
import { copy } from "./utils";
12+
1413
import Codes from "./codes";
1514
import TagOrLink from "./TagOrLink";
1615

1716
const { TabPane } = Tabs;
1817
const { Panel } = Collapse;
1918

20-
const formatCodeToMarkDown = (code, lang) => `\`\`\`${lang}\n${code}\`\`\`\n`;
21-
2219
const { problems, company } = db;
2320

2421
export default function Detail({ problemId }) {

src/codeTemplates/heap.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const minHeapCode = `
2+
class MinHeap {
3+
4+
constructor () {
5+
/* Initialing the array heap and adding a dummy element at index 0 */
6+
this.heap = [null]
7+
}
8+
9+
getMin () {
10+
/* Accessing the min element at index 1 in the heap array */
11+
return this.heap[1]
12+
}
13+
14+
insert (node) {
15+
16+
/* Inserting the new node at the end of the heap array */
17+
this.heap.push(node)
18+
19+
/* Finding the correct position for the new node */
20+
21+
if (this.heap.length > 1) {
22+
let current = this.heap.length - 1
23+
24+
/* Traversing up the parent node until the current node (current) is greater than the parent (current/2)*/
25+
while (current > 1 && this.heap[Math.floor(current/2)] > this.heap[current]) {
26+
27+
/* Swapping the two nodes by using the ES6 destructuring syntax*/
28+
[this.heap[Math.floor(current/2)], this.heap[current]] = [this.heap[current], this.heap[Math.floor(current/2)]]
29+
current = Math.floor(current/2)
30+
}
31+
}
32+
}
33+
34+
remove() {
35+
/* Smallest element is at the index 1 in the heap array */
36+
let smallest = this.heap[1]
37+
38+
/* When there are more than two elements in the array, we put the right most element at the first position
39+
and start comparing nodes with the child nodes
40+
*/
41+
if (this.heap.length > 2) {
42+
this.heap[1] = this.heap[this.heap.length-1]
43+
this.heap.splice(this.heap.length - 1)
44+
45+
if (this.heap.length === 3) {
46+
if (this.heap[1] > this.heap[2]) {
47+
[this.heap[1], this.heap[2]] = [this.heap[2], this.heap[1]]
48+
}
49+
return smallest
50+
}
51+
52+
let current = 1
53+
let leftChildIndex = current * 2
54+
let rightChildIndex = current * 2 + 1
55+
56+
while (this.heap[leftChildIndex] &&
57+
this.heap[rightChildIndex] &&
58+
(this.heap[current] > this.heap[leftChildIndex] ||
59+
this.heap[current] > this.heap[rightChildIndex])) {
60+
if (this.heap[leftChildIndex] < this.heap[rightChildIndex]) {
61+
[this.heap[current], this.heap[leftChildIndex]] = [this.heap[leftChildIndex], this.heap[current]]
62+
current = leftChildIndex
63+
} else {
64+
[this.heap[current], this.heap[rightChildIndex]] = [this.heap[rightChildIndex], this.heap[current]]
65+
current = rightChildIndex
66+
}
67+
68+
leftChildIndex = current * 2
69+
rightChildIndex = current * 2 + 1
70+
}
71+
}
72+
73+
/* If there are only two elements in the array, we directly splice out the first element */
74+
75+
else if (this.heap.length === 2) {
76+
this.heap.splice(1, 1)
77+
} else {
78+
return null
79+
}
80+
81+
return smallest
82+
}
83+
/**
84+
* Your MinHeap object will be instantiated and called as such:
85+
* var obj = new MinHeap()
86+
* obj.insert(1)
87+
* obj.insert(2)
88+
* obj.getMin() // will return 1
89+
* obj.remove() // remove 1
90+
* obj.getMin() // will return 2
91+
*/
92+
}
93+
`;
94+
95+
module.exports = {
96+
title: "堆",
97+
list: [
98+
{
99+
text: "标准堆",
100+
problems: [],
101+
codes: [
102+
{
103+
language: "js",
104+
text: minHeapCode,
105+
},
106+
],
107+
},
108+
],
109+
link: "",
110+
};

src/codeTemplates/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ import uf from "./uf";
44
import bfs from "./bfs";
55
import trie from "./trie";
66
import slidingWindow from "./sliding-window";
7+
// import heap from "./heap";
78
// bfs , sliding window, trie, uf
89
export default [binarySearch, bfs, slidingWindow, backtrack, trie, uf];

src/db/binary-tree.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module.exports = {
2+
data: String.raw`
3+
3
4+
/ \
5+
9 20
6+
/ \
7+
/ \
8+
/ \
9+
/ \
10+
/ \
11+
/ \
12+
/ \
13+
/ \
14+
/ \
15+
/ \
16+
15 7
17+
/ \ /
18+
/ \ 20
19+
/ \ / \
20+
3 9 / \
21+
\ / \ 9 20
22+
15 / \ / \
23+
/ \ 20 15
24+
/ \ / \ /
25+
/ \ 3 9 20
26+
/ \
27+
/ \
28+
7 3
29+
/ \ / \
30+
/ \ 3 9
31+
/ \ / \
32+
15 7 / \
33+
/ \ / \ / \
34+
/ \ 9 20 / \
35+
/ \ / \
36+
7 3 153232312323123213 7
37+
\ /
38+
15 7
39+
40+
`,
41+
};

src/utils.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
function TreeNode(value) {
2+
return {
3+
left: null,
4+
right: null,
5+
value,
6+
};
7+
}
8+
9+
export function bfs(nodes) {
10+
if (nodes.length === 0 || nodes[0] === "null") return null;
11+
const root = TreeNode(nodes[0]);
12+
const q = [root];
13+
let i = 0;
14+
15+
while (q.length > 0 && i < nodes.length - 1) {
16+
const cur = q.shift();
17+
18+
const l = TreeNode(nodes[i + 1]);
19+
if (l.value !== "null") {
20+
q.push(l);
21+
cur.left = l;
22+
}
23+
24+
if (i < nodes.length - 2) {
25+
const r = TreeNode(nodes[i + 2]);
26+
27+
if (r.value !== "null") {
28+
q.push(r);
29+
cur.right = r;
30+
}
31+
}
32+
33+
i += 2;
34+
}
35+
console.log(root);
36+
return {
37+
root,
38+
};
39+
}
140
export function copy(text, cb) {
241
//Create a textbox field where we can insert text to.
342
var copyFrom = document.createElement("textarea");

0 commit comments

Comments
 (0)