Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link href="https://unpkg.com/[email protected]/mocha.css" rel="stylesheet" />
</head>
<body>
  <div id="mocha"></div>

  <script src="https://unpkg.com/chai/chai.js"></script>
  <script src="https://unpkg.com/[email protected]/mocha.js"></script>

  <div></div>
  

</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                class  BinaryHeap{
  constructor(){
    this.heap = [30, 20, 10, 7, 9, 5]
  }
  
   insert(value){

    this.heap.push(value)
     this.bubbleUp();
  }

  //using iterative approach
     bubbleUp(){
       let index =  this.heap.length-1;

    while( index > 0){
       let element = this.heap[index],
           parentIndex = Math.floor((index-1)/2),
           parent = this.heap[parentIndex];

       if(parent >= element) break
          this.heap[index] = parent;
         this.heap[parentIndex] = element;
          index = parentIndex

    }
  }
  
  extractMax(){
    const max = this.heap[0];
    this.heap[0]= this.heap.pop()
    this.sinkDown(0)
    return max
  }
  
 sinkDown(index){
     
     let   left = 2*index+1,
           right = 2*index+2,
           largest = index;
     const length = this.heap.length
    
 
     
      if(left <= length &&  this.heap[left]>this.heap[largest] ){
         largest = left
       }
      if(right <= length && this.heap[right]>this.heap[largest]) {
        largest = right
      }
     // swap
     if(largest !== index){
        [this.heap[largest],this.heap[index]] = [this.heap[index],this.heap[largest]]
       this.sinkDown(largest)
     }
    } 
 
}



mocha.setup('bdd');


var assert = chai.assert;

describe('Heaps(Binary heap)',()=>{
  
  it("should add insert a new node to the heap",()=>{
    
     const heap = new BinaryHeap();
     heap.insert(90)
     heap.insert(49)
   
     assert.deepEqual([90, 49, 30, 20, 9, 5, 10, 7],heap.heap)
  
  })

  it("should  remove a max node from the heap",()=>{
     const heap = new BinaryHeap();
     assert.equal(30,heap.extractMax())
    console.log(heap)
     assert.deepEqual([20, 9, 10, 7, 5],heap.heap)
  })
  
})



mocha.run()
              
            
!
999px

Console