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 | 实例:
|
17 | const leftistTree = new LeftistTree();
|
18 | const count = LeftistTree.Count;
|
19 | console.log(count);
|
20 | //0
|
21 | ```
|
22 |
|
23 | #### 根节点 Root
|
24 | ##### Root:LeftistTreeNode\<T>
|
25 | ``` text
|
26 | 实例:
|
27 | const leftistTree = new LeftistTree();
|
28 | const root = LeftistTree.Root;
|
29 | ```
|
30 |
|
31 | ### 方法
|
32 |
|
33 | #### 插入 insert
|
34 | ##### insert(value: T):LeftistTreeNode\<T>
|
35 | ``` text
|
36 | 实例:
|
37 | const leftistTree = new LeftistTree();
|
38 | const node = leftistTree.insert(1);
|
39 | console.log(node.Value);
|
40 | // 1
|
41 | ```
|
42 |
|
43 | #### 删除最小节点 deleteExtremum
|
44 | ##### deleteExtremum(): T
|
45 | ``` text
|
46 | 实例:
|
47 | const leftistTree = new LeftistTree();
|
48 | leftistTree.insert(3);
|
49 | leftistTree.insert(1);
|
50 | leftistTree.insert(2);
|
51 | const value = leftistTree.deleteExtremum();
|
52 | console.log(value);
|
53 | // 1
|
54 | ```
|
55 |
|
56 | #### 查找最小节点 findExtremum
|
57 | ##### findExtremum(): T
|
58 | ``` text
|
59 | 实例:
|
60 | const leftistTree = new LeftistTree();
|
61 | leftistTree.insert(3);
|
62 | leftistTree.insert(1);
|
63 | leftistTree.insert(2);
|
64 | const value = leftistTree.findExtremum();
|
65 | console.log(value);
|
66 | // 1
|
67 | 描述:
|
68 | 此方法不删除元素
|
69 | ```
|
70 |
|
71 | #### 合并堆 union
|
72 | ##### union(tree: LeftistTree\<T>): LeftistTree\<T>
|
73 | ``` text
|
74 | 实例:
|
75 | const leftistTree = new LeftistTree();
|
76 | leftistTree.insert(5);
|
77 | leftistTree.insert(4);
|
78 | leftistTree.insert(6);
|
79 | const leftistTree2 = new LeftistTree();
|
80 | leftistTree2.insert(3);
|
81 | leftistTree2.insert(1);
|
82 | leftistTree2.insert(2);
|
83 | leftistTree.union(leftistTree2);
|
84 | console.log(leftistTree.Count);
|
85 | // 6
|
86 | console.log(leftistTree.findExtremum());
|
87 | // 1
|
88 | ```
|
89 |
|
90 | #### 是否为空 isEmpty
|
91 | ##### isEmpty(): boolean
|
92 | ``` text
|
93 | 实例:
|
94 | const leftistTree = new LeftistTree();
|
95 | const isEmpty = leftistTree.isEmpty();
|
96 | console.log(isEmpty);
|
97 | // true
|
98 | ``` |
\ | No newline at end of file |