Merge branch 'hotfix/cjc' into 'main'

修复二叉树指针回指上层结点问题

See merge request phenomLi/StructV2!7
This commit is contained in:
chen jiancheng 2022-02-23 12:02:55 +00:00
commit fd4b0f6054
2 changed files with 301 additions and 198 deletions

View File

@ -1,5 +1,3 @@
SV.registerLayout('BinaryTree', { SV.registerLayout('BinaryTree', {
defineOptions() { defineOptions() {
return { return {
@ -18,7 +16,7 @@ SV.registerLayout('BinaryTree', {
link: { link: {
child: { child: {
type: 'line', type: 'line',
sourceAnchor: index => index === 0? 3: 1, sourceAnchor: index => index === 0 ? 3 : 1,
targetAnchor: 0, targetAnchor: 0,
style: { style: {
stroke: '#333', stroke: '#333',
@ -62,7 +60,7 @@ SV.registerLayout('BinaryTree', {
*/ */
layoutItem(node, parent, index, layoutOptions) { layoutItem(node, parent, index, layoutOptions) {
// 次双亲不进行布局 // 次双亲不进行布局
if(!node) { if (!node) {
return null; return null;
} }
@ -71,21 +69,28 @@ SV.registerLayout('BinaryTree', {
height = bound.height, height = bound.height,
group = new Group(node); group = new Group(node);
if(parent) { if (node.visited) {
return;
}
if (parent) {
node.set('y', parent.get('y') + layoutOptions.yInterval + height); node.set('y', parent.get('y') + layoutOptions.yInterval + height);
// 左节点 // 左节点
if(index === 0) { if (index === 0) {
node.set('x', parent.get('x') - layoutOptions.xInterval / 2 - width / 2); node.set('x', parent.get('x') - layoutOptions.xInterval / 2 - width / 2);
} }
// 右结点 // 右结点
if(index === 1) { if (index === 1) {
node.set('x', parent.get('x') + layoutOptions.xInterval / 2 + width / 2); node.set('x', parent.get('x') + layoutOptions.xInterval / 2 + width / 2);
} }
} }
if(node.child && (node.child[0] || node.child[1])) { node.visited = true;
if (node.child && (node.child[0] || node.child[1])) {
let leftChild = node.child[0], let leftChild = node.child[0],
rightChild = node.child[1], rightChild = node.child[1],
leftGroup = this.layoutItem(leftChild, node, 0, layoutOptions), leftGroup = this.layoutItem(leftChild, node, 0, layoutOptions),
@ -94,22 +99,22 @@ SV.registerLayout('BinaryTree', {
move = 0; move = 0;
// 处理左右子树相交问题 // 处理左右子树相交问题
if(leftGroup && rightGroup) { if (leftGroup && rightGroup) {
intersection = Bound.intersect(leftGroup.getBound(), rightGroup.getBound()); intersection = Bound.intersect(leftGroup.getBound(), rightGroup.getBound());
move = 0; move = 0;
if(intersection && intersection.width > 0) { if (intersection && intersection.width > 0) {
move = (intersection.width + layoutOptions.xInterval) / 2; move = (intersection.width + layoutOptions.xInterval) / 2;
leftGroup.translate(-move, 0); leftGroup.translate(-move, 0);
rightGroup.translate(move, 0); rightGroup.translate(move, 0);
} }
} }
if(leftGroup) { if (leftGroup) {
group.add(leftGroup); group.add(leftGroup);
} }
if(rightGroup) { if (rightGroup) {
group.add(rightGroup) group.add(rightGroup)
} }
} }
@ -124,6 +129,7 @@ SV.registerLayout('BinaryTree', {
*/ */
layout(elements, layoutOptions) { layout(elements, layoutOptions) {
let root = elements[0]; let root = elements[0];
let visited = new Group();
this.layoutItem(root, null, -1, layoutOptions); this.layoutItem(root, null, -1, layoutOptions);
} }
}); });
@ -132,16 +138,13 @@ SV.registerLayout('BinaryTree', {
[ [{
{ "id": 6385328,
"id": 6385328, "data": "",
"data": "", "external": [
"external": [ "L"
"L" ],
], "root": true,
"root": true, "after": null,
"after": null, "next": null
"next": null }]
}
]

View File

@ -1,193 +1,293 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DEMO</title>
<style>
* {
padding: 0;
margin: 0;
user-select: none;
}
.container { <head>
background-color: #fafafa; <meta charset="UTF-8" />
border: 1px solid #ccc; <meta name="viewport" content="width=device-width, initial-scale=1.0" />
position: relative; <title>DEMO</title>
} <style>
* {
padding: 0;
margin: 0;
user-select: none;
}
.down { .container {
display: flex; background-color: #fafafa;
margin-top: 20px; border: 1px solid #ccc;
} position: relative;
}
#container { .down {
width: 100%; display: flex;
height: 500px; margin-top: 20px;
position: relative; }
overflow: hidden;
}
#leak { #container {
position: absolute; width: 100%;
left: 0; height: 500px;
opacity: 0; position: relative;
top: 100px; overflow: hidden;
width: 100%; }
box-sizing: border-box;
padding: 4px;
border-top: 1px dashed #000;
pointer-events: none;
transition: opacity 0.75s ease-in-out;
}
#leak > span { #leak {
color: #000; position: absolute;
} left: 0;
</style> opacity: 0;
</head> top: 100px;
width: 100%;
box-sizing: border-box;
padding: 4px;
border-top: 1px dashed #000;
pointer-events: none;
transition: opacity 0.75s ease-in-out;
}
<body> #leak>span {
<div class="container" id="container"> color: #000;
<div id="leak"> }
<span>泄漏区</span> </style>
</div> </head>
</div>
<button id="btn-prev">prev</button> <body>
<button id="btn-next">next</button> <div class="container" id="container">
<button id="resize">resize</button> <div id="leak">
<button id="relayout">relayout</button> <span>泄漏区</span>
<button id="switch-mode">switch mode</button> </div>
<button id="brush-select">brush-select</button> </div>
<span id="pos"></span>
<script src="./../dist/sv.js"></script> <button id="btn-prev">prev</button>
<script> <button id="btn-next">next</button>
const Group = SV.Group, <button id="resize">resize</button>
Bound = SV.Bound, <button id="relayout">relayout</button>
G6 = SV.G6, <button id="switch-mode">switch mode</button>
Vector = SV.Vector; <button id="brush-select">brush-select</button>
</script> <span id="pos"></span>
<script src="./Layouter/LinkList.js"></script>
<script src="./Layouter/BinaryTree.js"></script>
<script src="./Layouter/Stack.js"></script>
<script src="./Layouter/LinkQueue.js"></script>
<script src="./Layouter/GeneralizedList.js"></script>
<script src="./Layouter/ChainHashTable.js"></script>
<script src="./Layouter/Array.js"></script>
<script src="./Layouter/HashTable.js"></script>
<script src="./Layouter/LinkStack.js"></script>
<script src="./Layouter/AdjoinMatrixGraph.js"></script>
<script src="./Layouter/AdjoinTableGraph.js"></script>
<script src="./Layouter/SqQueue.js"></script>
<script src="./Layouter/PTree.js"></script>
<script src="./Layouter/PCTree.js"></script>
<script> <script src="./../dist/sv.js"></script>
let cur = SV(document.getElementById('container'), { <script>
view: { const Group = SV.Group,
leakAreaHeight: 130, Bound = SV.Bound,
groupPadding: 40, G6 = SV.G6,
}, Vector = SV.Vector;
}); </script>
<script src="./Layouter/LinkList.js"></script>
<script src="./Layouter/BinaryTree.js"></script>
<script src="./Layouter/Stack.js"></script>
<script src="./Layouter/LinkQueue.js"></script>
<script src="./Layouter/GeneralizedList.js"></script>
<script src="./Layouter/ChainHashTable.js"></script>
<script src="./Layouter/Array.js"></script>
<script src="./Layouter/HashTable.js"></script>
<script src="./Layouter/LinkStack.js"></script>
<script src="./Layouter/AdjoinMatrixGraph.js"></script>
<script src="./Layouter/AdjoinTableGraph.js"></script>
<script src="./Layouter/SqQueue.js"></script>
<script src="./Layouter/PTree.js"></script>
<script src="./Layouter/PCTree.js"></script>
let data = [ <script>
{ let cur = SV(document.getElementById('container'), {
LinkStack0: { view: {
data: [ leakAreaHeight: 130,
{ groupPadding: 40,
id: '0x616eb0', },
data: '', });
next: '0x616ed0',
headExternal: ['ls'],
type: 'default',
},
{
id: '0x616ed0',
data: 'f',
external: ['p'],
next: '0x616ef0',
type: 'default',
},
{
id: '0x616ef0',
data: 'g',
external: ['p1'],
next: null,
type: 'default',
},
],
layouter: 'LinkStack',
},
LinkStack1: {
data: [
{
id: '0x616f10',
data: '',
next: 'LinkStack0#0x616ef0',
headExternal: ['ls1'],
type: 'default',
},
],
layouter: 'LinkStack',
},
},
];
let dataIndex = 0, let data = [{
curData = data[dataIndex]; "BinaryTree0": {
"data": [{
"external": [
"T1"
],
"child": [
"0x617ee0",
"0x617f10"
],
"id": "0x617eb0",
"name": "T1",
"data": "Z",
"root": true,
"type": "default"
}, {
"child": [
"0x0",
"0x0"
],
"id": "0x617ee0",
"name": "T1->lchild",
"data": "A",
"type": "default"
}, {
"child": [
"0x0",
"0x0"
],
"id": "0x617f10",
"name": "T1->rchild",
"data": "B",
"type": "default",
"external": [
"r",
"t"
]
}],
"layouter": "BinaryTree"
},
"BinaryTree3": {
"data": [{
"external": [
"T2",
"r2"
],
"child": [
"0x617f70",
"0x0"
],
"id": "0x617f40",
"name": "T2",
"data": "Z",
"root": true,
"type": "default"
}, {
"child": [
"0x0",
"0x0"
],
"id": "0x617f70",
"name": "T2->lchild",
"data": "D",
"type": "default",
"external": [
"t2"
]
}],
"layouter": "BinaryTree"
}
}, {
"BinaryTree0": {
"data": [{
"external": [
"T1"
],
"child": [
"0x617ee0",
"0x617f10"
],
"id": "0x617eb0",
"name": "T1",
"data": "Z",
"root": true,
"type": "default"
}, {
"child": [
"0x0",
"0x0"
],
"id": "0x617ee0",
"name": "T1->lchild",
"data": "A",
"type": "default"
}, {
"child": [
"0x0",
"0x617eb0"
],
"id": "0x617f10",
"name": "T1->rchild",
"data": "B",
"type": "default"
}],
"layouter": "BinaryTree"
},
let enableBrushSelect = false; "BinaryTree3": {
"data": [{
"external": [
"T2",
"r2"
],
"child": [
"0x617f70",
"0x0"
],
"id": "0x617f40",
"name": "T2",
"data": "Z",
"root": true,
"type": "default"
}, {
"child": [
"0x0",
"0x0"
],
"id": "0x617f70",
"name": "T2->lchild",
"data": "D",
"type": "default",
"external": [
"t2"
]
}],
"layouter": "BinaryTree"
}
}
const container = document.getElementById('container'), ];
pos = document.getElementById('pos');
const leak = document.getElementById('leak'); let dataIndex = 0,
curData = data[dataIndex];
cur.render(curData); let enableBrushSelect = false;
document.getElementById('btn-next').addEventListener('click', e => { const container = document.getElementById('container'),
curData = data[++dataIndex]; pos = document.getElementById('pos');
cur.render(curData);
});
document.getElementById('btn-prev').addEventListener('click', e => { const leak = document.getElementById('leak');
curData = data[--dataIndex];
cur.render(curData);
});
document.getElementById('resize').addEventListener('click', e => { cur.render(curData);
container.style.height = 800 + 'px';
cur.resize(container.offsetWidth, container.offsetHeight);
});
document.getElementById('relayout').addEventListener('click', e => { document.getElementById('btn-next').addEventListener('click', e => {
cur.reLayout(); curData = data[++dataIndex];
}); cur.render(curData);
});
document.getElementById('switch-mode').addEventListener('click', e => { document.getElementById('btn-prev').addEventListener('click', e => {
cur.updateStyle('Array', newArrayOption); curData = data[--dataIndex];
}); cur.render(curData);
});
document.getElementById('brush-select').addEventListener('click', e => { document.getElementById('resize').addEventListener('click', e => {
enableBrushSelect = !enableBrushSelect; container.style.height = 800 + 'px';
cur.switchBrushSelect(enableBrushSelect); cur.resize(container.offsetWidth, container.offsetHeight);
}); });
cur.on('onLeakAreaUpdate', payload => { document.getElementById('relayout').addEventListener('click', e => {
leak.style.opacity = payload.hasLeak ? 1 : 0; cur.reLayout();
leak.style.top = payload.leakAreaY - 40 + 'px'; });
});
// ------------------------------------------------------------------------------------------------------- document.getElementById('switch-mode').addEventListener('click', e => {
cur.updateStyle('Array', newArrayOption);
});
document.getElementById('brush-select').addEventListener('click', e => {
enableBrushSelect = !enableBrushSelect;
cur.switchBrushSelect(enableBrushSelect);
});
cur.on('onLeakAreaUpdate', payload => {
leak.style.opacity = payload.hasLeak ? 1 : 0;
leak.style.top = payload.leakAreaY - 40 + 'px';
});
// -------------------------------------------------------------------------------------------------------
container.addEventListener('mousemove', e => {
let x = e.offsetX,
y = e.offsetY;
pos.innerHTML = `${x},${y}`;
});
</script>
</body>
container.addEventListener('mousemove', e => {
let x = e.offsetX,
y = e.offsetY;
pos.innerHTML = `${x},${y}`;
});
</script>
</body>
</html> </html>