blobt
4 years ago
1 changed files with 156 additions and 0 deletions
-
156src/ThreeNode.ts
@ -0,0 +1,156 @@ |
|||||
|
export class ThreeNode<T> { |
||||
|
private _parent: ThreeNode<T> | undefined; |
||||
|
private _children: Array<ThreeNode<T>> | undefined; |
||||
|
public name: string | undefined; |
||||
|
public data: T | undefined; |
||||
|
|
||||
|
public constructor(data: T | undefined = undefined, parent: ThreeNode<T> | undefined = undefined, name: string) { |
||||
|
this._parent = parent; |
||||
|
this._children = undefined; |
||||
|
this.name = name; |
||||
|
this.data = data; |
||||
|
if (this._parent !== undefined) { |
||||
|
this._parent.addChild(this); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断某个节点是不是当前节点的祖先节点 |
||||
|
* @param ancestor |
||||
|
* @returns |
||||
|
*/ |
||||
|
public isDescendantOf(ancestor: ThreeNode<T> | undefined): boolean { |
||||
|
if (ancestor === undefined) { |
||||
|
return false; |
||||
|
} |
||||
|
//从当前节点向上遍历
|
||||
|
let node: ThreeNode<T> | undefined = this._parent; //不明白为啥要添加这句
|
||||
|
for (let node: ThreeNode<T> | undefined = this._parent; node != undefined; node = node?._parent) { |
||||
|
if (node === ancestor) { |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
public getChildAt(index: number): ThreeNode<T> | undefined { |
||||
|
if (this._children === undefined) { |
||||
|
return undefined; |
||||
|
} |
||||
|
if (index < 0 || index > this._children.length) { |
||||
|
return undefined; |
||||
|
} |
||||
|
return this._children[index]; |
||||
|
} |
||||
|
|
||||
|
public removeChildAt(index: number): ThreeNode<T> | undefined { |
||||
|
if (this._children === undefined) { |
||||
|
return undefined; |
||||
|
} |
||||
|
let child: ThreeNode<T> | undefined = this.getChildAt(index); |
||||
|
if (child === undefined) { |
||||
|
return undefined; |
||||
|
} |
||||
|
this._children.splice(index, 1); |
||||
|
child._parent = undefined; |
||||
|
return child; |
||||
|
} |
||||
|
|
||||
|
public removeChild(child: ThreeNode<T> | undefined): ThreeNode<T> | undefined { |
||||
|
if (child === undefined) { |
||||
|
return undefined; |
||||
|
} |
||||
|
if (this._children === undefined) { |
||||
|
return undefined; |
||||
|
} |
||||
|
let index: number = -1; |
||||
|
for (let i = 0; i < this._children.length; i++) { |
||||
|
if (this.getChildAt(i) === child) { |
||||
|
index = i; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
if (index === -1) { |
||||
|
return undefined; |
||||
|
} |
||||
|
return this.removeChildAt(index); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 把当前节点在父节点中删除 |
||||
|
*/ |
||||
|
public remove(): ThreeNode<T> | undefined { |
||||
|
if (this._parent !== undefined) { |
||||
|
return this._parent.removeChild(this); |
||||
|
} |
||||
|
return undefined; |
||||
|
} |
||||
|
|
||||
|
public addChildAt(child: ThreeNode<T>, index: number): ThreeNode<T> | undefined { |
||||
|
//第一种情况,如果child是本节点的祖先节点,是不允许的
|
||||
|
if (this.isDescendantOf(child)) { |
||||
|
return undefined; |
||||
|
} |
||||
|
|
||||
|
//延迟初始化的处理
|
||||
|
if (this._children === undefined) { |
||||
|
this._children = []; |
||||
|
} |
||||
|
|
||||
|
if (index >= 0 && index <= this._children.length) { |
||||
|
//第二种情况,child已经是别人的子节点,那要先去除和别人的关系
|
||||
|
if (child._parent !== undefined) { |
||||
|
child._parent.removeChild(child); |
||||
|
} |
||||
|
|
||||
|
//第三种情况 正常情况
|
||||
|
child._parent = this; |
||||
|
this._children.splice(index, 0, child); |
||||
|
return child; |
||||
|
} else { |
||||
|
return undefined; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public addChild(child: ThreeNode<T>): ThreeNode<T> | undefined { |
||||
|
if (this._children === undefined) { |
||||
|
this._children = []; |
||||
|
} |
||||
|
return this.addChildAt(child, this._children.length); |
||||
|
} |
||||
|
|
||||
|
public get parent(): ThreeNode<T> | undefined { |
||||
|
return this._parent; |
||||
|
} |
||||
|
|
||||
|
public get childCount(): number { |
||||
|
if (this._children !== undefined) { |
||||
|
return this._children.length; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
public hasChild(): boolean { |
||||
|
return this._children !== undefined && this._children.length > 0; |
||||
|
} |
||||
|
|
||||
|
public get root(): ThreeNode<T> | undefined { |
||||
|
let curr: ThreeNode<T> | undefined = this; |
||||
|
while (curr !== undefined && curr._parent !== undefined) { |
||||
|
curr = curr._parent; |
||||
|
} |
||||
|
return curr; |
||||
|
} |
||||
|
|
||||
|
public get depth() :number{ |
||||
|
let curr: ThreeNode<T> | undefined = this; |
||||
|
let level: number = 0; |
||||
|
|
||||
|
while(curr !== undefined && curr._parent !== undefined){ |
||||
|
curr = curr._parent; |
||||
|
level++; |
||||
|
} |
||||
|
|
||||
|
return level; |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue