如题,具体看代码
其实用到的难点就是一个class一个递归
希望对大家有帮助
interface ITwoTree {
leftTree: ITwoTree,
rightTree: ITwoTree,
rootName: string
}
interface ITreeFactory {
setTreeChildrenNode(treeName: string, fatherTree: ITwoTree, childrenName: IChidrenName): ITwoTree,
delTreeChildrenNode(treeName: string, fatherTree: ITwoTree, childrenName: IChidrenName): void,
ergodicTwoTree(rootNode:ITwoTree,ergodicResult:string[]):string[],
}
type IChidrenName = 'leftTree' | 'rightTree'
///用于存储二叉树的内容
class TwoTree implements ITwoTree {
leftTree: ITwoTree;
rightTree: ITwoTree;
rootName: string;
constructor(rootName: string) {
this.rootName = rootName
}
}
//封装的一些操作二叉树的方法
class TreeFactory implements ITreeFactory {
constructor() {
}
/**
* 给二叉树添加节点
* @param treeName要添加的节点名
* @param fatherTree 父节点的class
* @param childrenName 要添加左子树还是右子树