1 | import { ICompare } from "../interface/ICompare";
|
2 | import {BasicBinaryTreeNode} from "../tree/basic-binary-tree/BasicBinaryTreeNode";
|
3 |
|
4 |
|
5 | function compareFn<T>(a: T, b: T) {
|
6 | return a <= b;
|
7 | }
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export class LeftistTreeNode<T = number> extends BasicBinaryTreeNode<T> {
|
13 | constructor(value: T, private rank: number) {
|
14 | super(value);
|
15 | }
|
16 |
|
17 | public set Rank(rank: number){
|
18 | this.rank = rank;
|
19 | }
|
20 |
|
21 | public get Rank(){
|
22 | return this.rank;
|
23 | }
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export class LeftistTree<T>{
|
30 | private root: LeftistTreeNode<T>;
|
31 | private count = 0;
|
32 |
|
33 | constructor(private compare: ICompare<T> = compareFn, value?: T) {
|
34 | if (typeof value !== "undefined") {
|
35 | this.root = new LeftistTreeNode(value , 0);
|
36 | this.count = 1;
|
37 | }
|
38 | }
|
39 |
|
40 | public get Root(){
|
41 | return this.root;
|
42 | }
|
43 |
|
44 | public get Count(){
|
45 | return this.count;
|
46 | }
|
47 |
|
48 | public isEmpty() {
|
49 | return !this.root;
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 | private fixNode(node: LeftistTreeNode<T>) {
|
57 | const left = node.Left as LeftistTreeNode<T>;
|
58 | const right = node.Right as LeftistTreeNode<T>;
|
59 |
|
60 | if (left && right && left.Rank < right.Rank) {
|
61 | const temp = node.Right;
|
62 | node.setRight(node.Left);
|
63 | node.setLeft(temp);
|
64 | }else if (node.Right && !node.Left) {
|
65 |
|
66 | node.setLeft(node.Right);
|
67 | node.setRight(null);
|
68 | }
|
69 |
|
70 |
|
71 | if (node.Right) {
|
72 | node.Rank = (node.Right as LeftistTreeNode<T>).Rank + 1;
|
73 | }else {
|
74 | node.Rank = 0;
|
75 | }
|
76 | }
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 | private _merge(root1: LeftistTreeNode<T>, root2: LeftistTreeNode<T>) {
|
84 | if (!root1) {
|
85 | return root2;
|
86 | }
|
87 | if (!root2) {
|
88 | return root1;
|
89 | }
|
90 |
|
91 | if (!this.compare(root1.Value , root2.Value)) {
|
92 | const temp = root2;
|
93 | root2 = root1;
|
94 | root1 = temp;
|
95 | }
|
96 |
|
97 | root1.setRight(this._merge(root1.Right as LeftistTreeNode<T>, root2));
|
98 | this.fixNode(root1);
|
99 | return root1;
|
100 | }
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 | public merge(tree2: LeftistTree<T>) {
|
107 |
|
108 | if (!tree2 || tree2.isEmpty()) {
|
109 | return this;
|
110 | }
|
111 | if (!this.root) {
|
112 | this.root = tree2.Root;
|
113 | this.count = tree2.Count;
|
114 | return this;
|
115 | }
|
116 |
|
117 | const root1 = this.Root;
|
118 | const root2 = tree2.Root;
|
119 | this.root = this._merge(root1 , root2);
|
120 | this.count += tree2.Count;
|
121 | return this;
|
122 | }
|
123 |
|
124 | |
125 |
|
126 |
|
127 | public findExtremum() {
|
128 | if (!this.root) {
|
129 | return null;
|
130 | }
|
131 | return this.root.Value;
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 | public insert(value: T) {
|
139 | const node = new LeftistTree(this.compare , value);
|
140 | this.merge(node);
|
141 | return node;
|
142 | }
|
143 |
|
144 | |
145 |
|
146 |
|
147 | public deleteExtremum(): T {
|
148 | if (!this.root) {
|
149 | return null;
|
150 | }
|
151 | const value = this.root.Value;
|
152 |
|
153 | this.root = this._merge(this.root.Left as LeftistTreeNode<T> , this.root.Right as LeftistTreeNode<T>);
|
154 | this.count --;
|
155 | return value;
|
156 | }
|
157 | }
|