UNPKG

2.69 kBMarkdownView Raw
1# 左偏树 LeftistTree\<T>
2
3左偏树是一种可并堆的实现。左偏树是一棵二叉树,它的节点除了和二叉树的节点一样具有左右子树指针(left, right)外,还有两个属性: 键值和距离(英文文献中称为s-value)。键值用于比较节点的大小。距离的定义如下:
4
5当且仅当节点 i 的左子树且右子树为空时,节点被称作外节点(实际上保存在二叉树中的节点都是内节点,外节点是逻辑上存在而无需保存。把一颗二叉树补上全部的外节点,则称为extended binary tree)。节点i的距离是节点 i 到它的后代中的最近的外节点所经过的边数。特别的,如果节点 i 本身是外节点,则它的距离为0;而空节点的距离规定为 -1。
6
7![LeftistTree](https://gss3.bdstatic.com/7Po3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=9c3171f5c98065386fe7ac41f6b4ca21/8694a4c27d1ed21b01d6da5eae6eddc451da3f69.jpg)
8
9
10## 基本操作的API及示例
11### 属性
12
13#### 节点个数 Count
14##### Count:number
15``` text
16实例:
17const leftistTree = new LeftistTree();
18const count = LeftistTree.Count;
19console.log(count);
20//0
21```
22
23#### 根节点 Root
24##### Root:LeftistTreeNode\<T>
25``` text
26实例:
27const leftistTree = new LeftistTree();
28const root = LeftistTree.Root;
29```
30
31### 方法
32
33#### 插入 insert
34##### insert(value: T):LeftistTreeNode\<T>
35``` text
36实例:
37const leftistTree = new LeftistTree();
38const node = leftistTree.insert(1);
39console.log(node.Value);
40// 1
41```
42
43#### 删除最小节点 deleteExtremum
44##### deleteExtremum(): T
45``` text
46实例:
47const leftistTree = new LeftistTree();
48leftistTree.insert(3);
49leftistTree.insert(1);
50leftistTree.insert(2);
51const value = leftistTree.deleteExtremum();
52console.log(value);
53// 1
54```
55
56#### 查找最小节点 findExtremum
57##### findExtremum(): T
58``` text
59实例:
60const leftistTree = new LeftistTree();
61leftistTree.insert(3);
62leftistTree.insert(1);
63leftistTree.insert(2);
64const value = leftistTree.findExtremum();
65console.log(value);
66// 1
67描述:
68此方法不删除元素
69```
70
71#### 合并堆 union
72##### union(tree: LeftistTree\<T>): LeftistTree\<T>
73``` text
74实例:
75const leftistTree = new LeftistTree();
76leftistTree.insert(5);
77leftistTree.insert(4);
78leftistTree.insert(6);
79const leftistTree2 = new LeftistTree();
80leftistTree2.insert(3);
81leftistTree2.insert(1);
82leftistTree2.insert(2);
83leftistTree.union(leftistTree2);
84console.log(leftistTree.Count);
85// 6
86console.log(leftistTree.findExtremum());
87// 1
88```
89
90#### 是否为空 isEmpty
91##### isEmpty(): boolean
92``` text
93实例:
94const leftistTree = new LeftistTree();
95const isEmpty = leftistTree.isEmpty();
96console.log(isEmpty);
97// true
98```
\No newline at end of file