修改二叉树回指的bug

This commit is contained in:
cjc 2022-03-11 17:07:09 +08:00
parent 4bb64214e7
commit d06a0e15e8
2 changed files with 168 additions and 195 deletions

View File

@ -1,99 +1,101 @@
SV.registerLayout('BinaryTree', { SV.registerLayout('BinaryTree', {
defineOptions() { defineOptions() {
return { return {
node: { node: {
default: { default: {
type: 'binary-tree-node', type: 'binary-tree-node',
size: [60, 30], size: [60, 30],
label: '[id]', label: '[data]',
style: { style: {
fill: '#b83b5e', fill: '#b83b5e',
stroke: '#333', stroke: '#333',
cursor: 'pointer', cursor: 'pointer',
}, },
}, },
}, },
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',
lineAppendWidth: 6, lineAppendWidth: 6,
cursor: 'pointer', cursor: 'pointer',
endArrow: 'default', endArrow: 'default',
startArrow: { startArrow: {
path: G6.Arrow.circle(2, -1), path: G6.Arrow.circle(2, -1),
fill: '#333', fill: '#333',
}, },
}, },
}, },
}, },
marker: { marker: {
external: { external: {
type: 'pointer', type: 'pointer',
anchor: 0, anchor: 0,
offset: 14, offset: 14,
style: { style: {
fill: '#f08a5d', fill: '#f08a5d',
}, },
labelOptions: { labelOptions: {
style: { style: {
fill: '#000099', fill: '#000099',
}, },
}, },
}, },
}, },
layout: { layout: {
xInterval: 40, xInterval: 40,
yInterval: 40, yInterval: 40,
}, },
behavior: { behavior: {
// dragNode: false // dragNode: false
}, },
}; };
}, },
/** /**
* 对子树进行递归布局 * 对子树进行递归布局
*/ */
layoutItem(node, layoutOptions) { layoutItem(node, layoutOptions) {
// 次双亲不进行布局 // 次双亲不进行布局
if (!node) { if (!node) {
return null; return null;
} }
let bound = node.getBound(), let bound = node.getBound(),
width = bound.width, width = bound.width,
height = bound.height, height = bound.height,
group = new Group(node), group = new Group(node),
leftGroup = null, leftGroup = null,
rightGroup = null, rightGroup = null,
leftBound = null, leftBound = null,
rightBound = null; rightBound = null;
if (node.visited) { if (node.visited) {
return null; return null;
} }
if (node.child && node.child[0]) { node.visited = true;
leftGroup = this.layoutItem(node.child[0], layoutOptions);
}
if (node.child && node.child[1]) { if (node.child && node.child[0]) {
rightGroup = this.layoutItem(node.child[1], layoutOptions); leftGroup = this.layoutItem(node.child[0], layoutOptions);
} }
if (node.child && node.child[1]) {
rightGroup = this.layoutItem(node.child[1], layoutOptions);
}
if (leftGroup) { if (leftGroup) {
leftBound = leftGroup.getBound(); leftBound = leftGroup.getBound();
node.set('y', leftBound.y - layoutOptions.yInterval - height); node.set('y', leftBound.y - layoutOptions.yInterval - height);
} }
if(rightGroup) { if (rightGroup) {
rightBound = rightGroup.getBound(); rightBound = rightGroup.getBound();
if(leftGroup) { if (leftGroup) {
rightGroup.translate(0, leftBound.y - rightBound.y) rightGroup.translate(0, leftBound.y - rightBound.y)
} }
@ -102,55 +104,52 @@ SV.registerLayout('BinaryTree', {
} }
// 处理左右子树相交问题 // 处理左右子树相交问题
if (leftGroup && rightGroup) { if (leftGroup && rightGroup) {
let move = Math.abs(rightBound.x - layoutOptions.xInterval - leftBound.x - leftBound.width); let move = Math.abs(rightBound.x - layoutOptions.xInterval - leftBound.x - leftBound.width);
if (move > 0) { if (move > 0) {
leftGroup.translate(-move / 2, 0); leftGroup.translate(-move / 2, 0);
rightGroup.translate(move / 2, 0); rightGroup.translate(move / 2, 0);
} }
} }
if (leftGroup) { if (leftGroup) {
leftBound = leftGroup.getBound(); leftBound = leftGroup.getBound();
node.set('x', leftBound.x + leftBound.width + layoutOptions.xInterval / 2 - width); node.set('x', leftBound.x + leftBound.width + layoutOptions.xInterval / 2 - width);
} }
if(rightGroup) { if (rightGroup) {
rightBound = rightGroup.getBound(); rightBound = rightGroup.getBound();
node.set('x', rightBound.x - layoutOptions.xInterval / 2 - width); node.set('x', rightBound.x - layoutOptions.xInterval / 2 - width);
} }
node.visited = true;
if (leftGroup) { if (leftGroup) {
group.add(leftGroup); group.add(leftGroup);
} }
if (rightGroup) { if (rightGroup) {
group.add(rightGroup); group.add(rightGroup);
} }
return group; return group;
}, },
/** /**
* 布局函数 * 布局函数
* @param {*} elements * @param {*} elements
* @param {*} layoutOptions * @param {*} layoutOptions
*/ */
layout(elements, layoutOptions) { layout(elements, layoutOptions) {
let root = elements[0]; let root = elements[0];
this.layoutItem(root, layoutOptions); this.layoutItem(root, layoutOptions);
}, },
}); });
[ [{
{ id: 6385328,
id: 6385328, data: '',
data: '', external: ['L'],
external: ['L'], root: true,
root: true, after: null,
after: null, next: null,
next: null, }, ];
},
];

View File

@ -1,103 +1,77 @@
const SOURCES_DATA = [{ const SOURCES_DATA = [{
"LinkList0": { "BinaryTree0": {
"data": [{ "data": [{
"id": "0x616eb0", "external": [
"data": "Z", "T1"
"next": "0x616ef0",
"loopNext": null,
"rootExternal": [
"L"
], ],
"child": [
"0x617ee0",
"0x617f10"
],
"id": "0x617eb0",
"name": "T1",
"data": "Z",
"root": true,
"type": "default" "type": "default"
}, },
{ {
"freed": true, "child": [
"id": "0x616ef0", "0x0",
"data": "", "0x0"
"next": "0x605010", ],
"loopNext": null, "id": "0x617ee0",
"name": "T1->lchild",
"data": "D",
"type": "default" "type": "default"
}, },
{ {
"id": "0x605010", "child": [
"data": "1", "0x617f70",
"external": [ "0x617f40"
"t"
], ],
"next": null, "id": "0x617f10",
"loopNext": null, "name": "T1->rchild",
"data": "C",
"type": "default" "type": "default"
} },
], {
"layouter": "LinkList" "child": [
}, "0x0",
"handleUpdate": { "0x0"
"isEnterFunction": false,
"isFirstDebug": false
}
}, {
"LinkList0": {
"data": [{
"id": "0x616eb0",
"data": "Z",
"external": [
"L"
], ],
"next": "0x616ef0", "id": "0x617f70",
"loopNext": null "name": "(T1->rchild)->lchild",
"data": "A",
"type": "default"
}, },
{ {
"freed": true, "child": [
"id": "0x616ef0", "0x0",
"data": "", "0x617fa0"
"next": "0x605010", ],
"loopNext": null "id": "0x617f40",
}, "name": "(T1->rchild)->rchild",
{ "data": "B",
"id": "0x605010", "type": "default",
"data": "1",
"external": [ "external": [
"t" "t"
], ]
"next": null,
"loopNext": null
}
],
"layouter": "LinkList"
},
"handleUpdate": {
"isEnterFunction": false,
"isFirstDebug": false
}
}, {
"LinkList0": {
"data": [{
"id": "0x616eb0",
"data": "Z",
"external": [
"L"
],
"next": "0x616ef0",
"loopNext": null
}, },
{ {
"freed": true, "child": [
"id": "0x616ef0", "0x0",
"data": "", "0x617f40"
"next": "0x605010",
"loopNext": null
},
{
"id": "0x605010",
"data": "1",
"external": [
"t"
], ],
"next": null, "id": "0x617fa0",
"loopNext": null "name": "((T1->rchild)->rchild)->rchild",
"data": "E",
"type": "default",
"external": [
"r"
]
} }
], ],
"layouter": "LinkList" "layouter": "BinaryTree"
}, },
"handleUpdate": { "handleUpdate": {
"isEnterFunction": false, "isEnterFunction": false,