TypeScript数据结构与算法(12)基于二分搜索树实现的Set
源码如下:
import { DataStruct_BinarySearchTree } from "../06-Binary-Search-Tree/DataStruct_BinarySearchTree";
import { Interface_Set } from "../Interface_Set";
/**
* Autor: Created by 李清风 on 2021-01-02.
* Desc: 集合(基于二分搜索树实现),关键词:去重,运用:网站统计不重复用户;学习词汇量统计
*/
export class DataStruct_Set<T> implements Interface_Set<T>{
//由于之前实现二分搜索树时,不能存放重复元素,所以时一种很好的设计集合的底层数据结构
public constructor() {
this.bst = new DataStruct_BinarySearchTree<T>();
}
private bst: DataStruct_BinarySearchTree<T>;
//添加元素
add(e: T): void {
this.bst.add(e);
}
//移处元素
remove(e: T): void {
this.bst.remove(e);
}
//是否包含
contains(e: T): boolean {
return this.bst.contains(e);
}
getSize(): number {
return this.bst.getSize();
}
isEmpty(): boolean {
return this.bst.isEmpty();
}
}

浙公网安备 33010602011771号