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,8 +138,7 @@ SV.registerLayout('BinaryTree', {
[ [{
{
"id": 6385328, "id": 6385328,
"data": "", "data": "",
"external": [ "external": [
@ -142,6 +147,4 @@ SV.registerLayout('BinaryTree', {
"root": true, "root": true,
"after": null, "after": null,
"next": null "next": null
} }]
]

View File

@ -1,6 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DEMO</title> <title>DEMO</title>
@ -42,13 +43,13 @@
transition: opacity 0.75s ease-in-out; transition: opacity 0.75s ease-in-out;
} }
#leak > span { #leak>span {
color: #000; color: #000;
} }
</style> </style>
</head> </head>
<body> <body>
<div class="container" id="container"> <div class="container" id="container">
<div id="leak"> <div id="leak">
<span>泄漏区</span> <span>泄漏区</span>
@ -93,47 +94,145 @@
}, },
}); });
let data = [ let data = [{
{ "BinaryTree0": {
LinkStack0: { "data": [{
data: [ "external": [
{ "T1"
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', "child": [
}, "0x617ee0",
LinkStack1: { "0x617f10"
data: [
{
id: '0x616f10',
data: '',
next: 'LinkStack0#0x616ef0',
headExternal: ['ls1'],
type: 'default',
},
], ],
layouter: 'LinkStack', "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"
}, },
"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"
}
}
]; ];
let dataIndex = 0, let dataIndex = 0,
@ -189,5 +288,6 @@
pos.innerHTML = `${x},${y}`; pos.innerHTML = `${x},${y}`;
}); });
</script> </script>
</body> </body>
</html> </html>