restoring heap property after insertion several elements











up vote
2
down vote

favorite
2












Let we have a binary heap, realized by an array of length n. We write at the end of this array k elements. After that we want to restrore the heap property on this array of length n+k.



The complexity should be O(k + (logn)*log(log(n))).



My attemts. Of course we can restore the heap property of the full array with complexity O(n+k) using standart procedure. But it doesn't satisfy this complexity.



Also I have heard about the following method. Let we make a heap on the k last elements by O(k) and after that we create a "new heap" with first element greater (if we work with max-heaps) then the tops of first heap of size n and second heap of size k and with first heap as left subheap of new heap and second heap as right subheap. After that we delete top element by O(log(n+k)). But I really don't understand how to realize this approach, when we use an array representation.










share|improve this question







New contributor




Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote

    favorite
    2












    Let we have a binary heap, realized by an array of length n. We write at the end of this array k elements. After that we want to restrore the heap property on this array of length n+k.



    The complexity should be O(k + (logn)*log(log(n))).



    My attemts. Of course we can restore the heap property of the full array with complexity O(n+k) using standart procedure. But it doesn't satisfy this complexity.



    Also I have heard about the following method. Let we make a heap on the k last elements by O(k) and after that we create a "new heap" with first element greater (if we work with max-heaps) then the tops of first heap of size n and second heap of size k and with first heap as left subheap of new heap and second heap as right subheap. After that we delete top element by O(log(n+k)). But I really don't understand how to realize this approach, when we use an array representation.










    share|improve this question







    New contributor




    Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      2
      down vote

      favorite
      2









      up vote
      2
      down vote

      favorite
      2






      2





      Let we have a binary heap, realized by an array of length n. We write at the end of this array k elements. After that we want to restrore the heap property on this array of length n+k.



      The complexity should be O(k + (logn)*log(log(n))).



      My attemts. Of course we can restore the heap property of the full array with complexity O(n+k) using standart procedure. But it doesn't satisfy this complexity.



      Also I have heard about the following method. Let we make a heap on the k last elements by O(k) and after that we create a "new heap" with first element greater (if we work with max-heaps) then the tops of first heap of size n and second heap of size k and with first heap as left subheap of new heap and second heap as right subheap. After that we delete top element by O(log(n+k)). But I really don't understand how to realize this approach, when we use an array representation.










      share|improve this question







      New contributor




      Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Let we have a binary heap, realized by an array of length n. We write at the end of this array k elements. After that we want to restrore the heap property on this array of length n+k.



      The complexity should be O(k + (logn)*log(log(n))).



      My attemts. Of course we can restore the heap property of the full array with complexity O(n+k) using standart procedure. But it doesn't satisfy this complexity.



      Also I have heard about the following method. Let we make a heap on the k last elements by O(k) and after that we create a "new heap" with first element greater (if we work with max-heaps) then the tops of first heap of size n and second heap of size k and with first heap as left subheap of new heap and second heap as right subheap. After that we delete top element by O(log(n+k)). But I really don't understand how to realize this approach, when we use an array representation.







      arrays algorithm data-structures heap complexity-theory






      share|improve this question







      New contributor




      Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 4 at 9:51









      Maxim Kuznetsov

      111




      111




      New contributor




      Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Maxim Kuznetsov is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          The simplest thing would be to apply the "heap insert" operation on each of the k new elements, starting with the first. This has complexity O(k log(n)), which if k is much smaller than n is better than O(k + log(n) log(log(n)))






          share|improve this answer

















          • 1




            Thank you. Of course I understand it. For this k should be not greater than log(log(n)). Also if k = O(n) than the approach with creating heap on all elements will be good in the complexity sense. But the most interesting situation is for an arbitrary k.
            – Maxim Kuznetsov
            Nov 4 at 10:23











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Maxim Kuznetsov is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139543%2frestoring-heap-property-after-insertion-several-elements%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          The simplest thing would be to apply the "heap insert" operation on each of the k new elements, starting with the first. This has complexity O(k log(n)), which if k is much smaller than n is better than O(k + log(n) log(log(n)))






          share|improve this answer

















          • 1




            Thank you. Of course I understand it. For this k should be not greater than log(log(n)). Also if k = O(n) than the approach with creating heap on all elements will be good in the complexity sense. But the most interesting situation is for an arbitrary k.
            – Maxim Kuznetsov
            Nov 4 at 10:23















          up vote
          1
          down vote













          The simplest thing would be to apply the "heap insert" operation on each of the k new elements, starting with the first. This has complexity O(k log(n)), which if k is much smaller than n is better than O(k + log(n) log(log(n)))






          share|improve this answer

















          • 1




            Thank you. Of course I understand it. For this k should be not greater than log(log(n)). Also if k = O(n) than the approach with creating heap on all elements will be good in the complexity sense. But the most interesting situation is for an arbitrary k.
            – Maxim Kuznetsov
            Nov 4 at 10:23













          up vote
          1
          down vote










          up vote
          1
          down vote









          The simplest thing would be to apply the "heap insert" operation on each of the k new elements, starting with the first. This has complexity O(k log(n)), which if k is much smaller than n is better than O(k + log(n) log(log(n)))






          share|improve this answer












          The simplest thing would be to apply the "heap insert" operation on each of the k new elements, starting with the first. This has complexity O(k log(n)), which if k is much smaller than n is better than O(k + log(n) log(log(n)))







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 4 at 9:58









          John Zwinck

          147k16173285




          147k16173285








          • 1




            Thank you. Of course I understand it. For this k should be not greater than log(log(n)). Also if k = O(n) than the approach with creating heap on all elements will be good in the complexity sense. But the most interesting situation is for an arbitrary k.
            – Maxim Kuznetsov
            Nov 4 at 10:23














          • 1




            Thank you. Of course I understand it. For this k should be not greater than log(log(n)). Also if k = O(n) than the approach with creating heap on all elements will be good in the complexity sense. But the most interesting situation is for an arbitrary k.
            – Maxim Kuznetsov
            Nov 4 at 10:23








          1




          1




          Thank you. Of course I understand it. For this k should be not greater than log(log(n)). Also if k = O(n) than the approach with creating heap on all elements will be good in the complexity sense. But the most interesting situation is for an arbitrary k.
          – Maxim Kuznetsov
          Nov 4 at 10:23




          Thank you. Of course I understand it. For this k should be not greater than log(log(n)). Also if k = O(n) than the approach with creating heap on all elements will be good in the complexity sense. But the most interesting situation is for an arbitrary k.
          – Maxim Kuznetsov
          Nov 4 at 10:23










          Maxim Kuznetsov is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Maxim Kuznetsov is a new contributor. Be nice, and check out our Code of Conduct.













          Maxim Kuznetsov is a new contributor. Be nice, and check out our Code of Conduct.












          Maxim Kuznetsov is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53139543%2frestoring-heap-property-after-insertion-several-elements%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()