@@ -33,4 +33,58 @@ describe('PriorityQueue', () => {
3333 expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
3434 expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
3535 } ) ;
36+
37+ it ( 'should be possible to change priority of internal nodes' , ( ) => {
38+ const priorityQueue = new PriorityQueue ( ) ;
39+
40+ priorityQueue . add ( 10 , 1 ) ;
41+ priorityQueue . add ( 5 , 2 ) ;
42+ priorityQueue . add ( 100 , 0 ) ;
43+ priorityQueue . add ( 200 , 0 ) ;
44+
45+ priorityQueue . changePriority ( 100 , 10 ) ;
46+ priorityQueue . changePriority ( 10 , 20 ) ;
47+
48+ expect ( priorityQueue . poll ( ) ) . toBe ( 200 ) ;
49+ expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
50+ expect ( priorityQueue . poll ( ) ) . toBe ( 100 ) ;
51+ expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
52+ } ) ;
53+
54+ it ( 'should be possible to change priority of head node' , ( ) => {
55+ const priorityQueue = new PriorityQueue ( ) ;
56+
57+ priorityQueue . add ( 10 , 1 ) ;
58+ priorityQueue . add ( 5 , 2 ) ;
59+ priorityQueue . add ( 100 , 0 ) ;
60+ priorityQueue . add ( 200 , 0 ) ;
61+
62+ priorityQueue . changePriority ( 200 , 10 ) ;
63+ priorityQueue . changePriority ( 10 , 20 ) ;
64+
65+ expect ( priorityQueue . poll ( ) ) . toBe ( 100 ) ;
66+ expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
67+ expect ( priorityQueue . poll ( ) ) . toBe ( 200 ) ;
68+ expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
69+ } ) ;
70+
71+ it ( 'should be possible to change priority along with node addition' , ( ) => {
72+ const priorityQueue = new PriorityQueue ( ) ;
73+
74+ priorityQueue . add ( 10 , 1 ) ;
75+ priorityQueue . add ( 5 , 2 ) ;
76+ priorityQueue . add ( 100 , 0 ) ;
77+ priorityQueue . add ( 200 , 0 ) ;
78+
79+ priorityQueue . changePriority ( 200 , 10 ) ;
80+ priorityQueue . changePriority ( 10 , 20 ) ;
81+
82+ priorityQueue . add ( 15 , 15 ) ;
83+
84+ expect ( priorityQueue . poll ( ) ) . toBe ( 100 ) ;
85+ expect ( priorityQueue . poll ( ) ) . toBe ( 5 ) ;
86+ expect ( priorityQueue . poll ( ) ) . toBe ( 200 ) ;
87+ expect ( priorityQueue . poll ( ) ) . toBe ( 15 ) ;
88+ expect ( priorityQueue . poll ( ) ) . toBe ( 10 ) ;
89+ } ) ;
3690} ) ;
0 commit comments