Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Huffman</title>
  7. <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
  8.  
  9. <style>
  10. * {
  11. background: rgb(200, 200, 200);
  12. }
  13. #drop_zone {
  14. margin: 50px;
  15. border: 3px solid rgb(75, 75, 75);
  16. width: 80%;
  17. margin-left: 10%;
  18. margin-right: 10%;
  19. text-align: center;
  20. font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
  21. font-size: 10vmin;
  22. }
  23. #wynik {
  24. width: 30%;
  25. font-size: 2em;
  26. word-spacing: 25px;
  27. }
  28. </style>
  29.  
  30.  
  31. <script>
  32.  
  33. // !!!!! TUTAJ ZNAJDUJE SIE KOD ALGORYTMU !!!!!
  34.  
  35. class Wezel {
  36. constructor(znak, ilosc) {
  37. this.znak = znak;
  38. this.ilosc = ilosc;
  39. this.lewy = null;
  40. this.prawy = null;
  41. }
  42. }
  43.  
  44. //funkcja sortujaca wezly po ilosci powtorzen danego znaku
  45. function sortuj(wezly) {
  46. return _.sortBy(wezly, x => x.ilosc)
  47. }
  48.  
  49. function wypisz(wezel, poziom) {
  50. //przemieszczanie sie po drzewie
  51. if(wezel.lewy !== null)
  52. wypisz(wezel.lewy, poziom.concat('0'))
  53. if(wezel.prawy !== null)
  54. wypisz(wezel.prawy, poziom.concat('1'))
  55.  
  56. //wypisywanie
  57. if(wezel.znak !== null) {
  58. const para = document.createElement("tr");
  59. const node = document.createTextNode(wezel.znak + ' ' + wezel.ilosc + ' ' + poziom);
  60. para.appendChild(node);
  61.  
  62. const element = document.getElementById("wynik");
  63. element.appendChild(para);
  64. }
  65. }
  66.  
  67. //przykladowy tekst
  68. let tekst = "aaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddeeeeeeeeeeeeeeeeeeeeffffffffffffffffffffgggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
  69.  
  70. function huffman() {
  71. document.getElementById("wynik").innerHTML = ""
  72.  
  73. //rozbijam tekst na tablice, np "aabbc", daje ['a', 'a', 'b', 'b', 'c']
  74. const tablica = tekst.split('')
  75.  
  76. // grupuje po literach, np "aabcccc" daje {
  77. // a: [a, a],
  78. // b: [b],
  79. // c: [c, c, c, c]
  80. // }
  81. // a nastepnie zmieniam tablice z powtarzajacymi sie literami na jej rozmiar, czyli
  82. // a: 2,
  83. // b: 1,
  84. // c: 4
  85. const pogrupowane = _.mapValues(_.groupBy(tablica, el => el), el => el.length)
  86.  
  87.  
  88. // sortuje po ilosci powtorzen
  89. const posortowane = _.mapValues(_.invert(_.invert(pogrupowane)), x => parseInt(x))
  90.  
  91. // tworze wezly z posortowanych danych i dodaje je do tablicy
  92. // w tej tablicy przechowuje wezly, ktore nie maja jeszcze rodzica
  93. let wezly = []
  94. _.forIn(posortowane, function (wartosc, klucz) {
  95. wezly.push(new Wezel(klucz, wartosc))
  96. })
  97.  
  98. let korzen = null
  99.  
  100. //budowanie drzewa
  101. while(! _.isEmpty(wezly)) {
  102. const wartosc = wezly[0].ilosc + wezly[1].ilosc
  103. const nowy = new Wezel(null, wartosc)
  104. nowy.lewy = wezly[0]
  105. nowy.prawy = wezly[1]
  106.  
  107. //jezeli zostaly tylko dwa wezly, ustawiam korzen
  108. if(wezly.length === 2) {
  109. korzen = nowy
  110. }
  111. else {
  112. //dodaje nowy wezel o znaku null do tablicy z ktorej wybieram dwa najmniejsze wezly
  113. wezly.push(nowy)
  114. wezly = sortuj(wezly)
  115. }
  116.  
  117. //usuwa dwa pierwsze elementy tablicy z wezlami
  118. wezly.splice(0, 2)
  119. }
  120.  
  121. wypisz(korzen, '')
  122. }
  123.  
  124.  
  125.  
  126. //funkcja wczytujaca plik
  127. function dropHandler(ev) {
  128.  
  129. ev.preventDefault();
  130.  
  131. if (ev.dataTransfer.items) {
  132. for (let i = 0; i < ev.dataTransfer.items.length; i++) {
  133. if (ev.dataTransfer.items[i].kind === 'file') {
  134. const file = ev.dataTransfer.items[i].getAsFile();
  135. const reader = new FileReader()
  136.  
  137. reader.onload = function(e) {
  138. tekst = e.target.result
  139. huffman()
  140. }
  141. reader.readAsText(file)
  142. }
  143. }
  144. } else {
  145. for (let i = 0; i < ev.dataTransfer.files.length; i++) {
  146. console.log('... file[' + i + '].name = ' + ev.dataTransfer.files[i].name);
  147. }
  148. }
  149. }
  150.  
  151. function dragOverHandler(ev) {
  152. ev.preventDefault();
  153. }
  154. </script>
  155.  
  156. </head>
  157. <body>
  158.  
  159. <div id="drop_zone" ondrop="dropHandler(event);" ondragover="dragOverHandler(event);">
  160. <p> Tutaj przeciagnac plik z danymi </p>
  161. </div>
  162. <button onclick="huffman()"> przyklad z wykladu </button>
  163. <div id="wynik"></div>
  164.  
  165. </body>
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement