Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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/mocha@5.2.0/mocha.css" rel="stylesheet" />
</head>
<body>
  <div id="mocha"></div>

  <script src="https://unpkg.com/chai/chai.js"></script>
  <script src="https://unpkg.com/mocha@5.2.0/mocha.js"></script>

  <div></div>
  

</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                class Node{
  constructor(data){
    this.right = null;
    this.left = null;
    this.data = data
  }
}


class Bst{
  
  constructor(){
    this.root = null
  }
  
  insert(data){
    
    var node = new Node(data);
    
    if(!this.root){
       this.root = node;  
      return this
    }
   
    let current = this.root
    
    while(current){

        if (data === current.data) {
            return;
        }     
      if(data < current.data){
        
        if(!current.left){
           current.left = node
         break
        }
        current = current.left
      }
      
      if(data > current.data){
        
        if(!current.right){
           current.right = node
         break
        }
        current = current.right
      }
      
      
     
 }
    return this
}
  
  find(data){
    
    
    if(!this.root) return null
    
    let current = this.root;
   
    while(current){
      if(data == current.data) return current.data;
      
      if(current.right && data > current.data){
        current = current.right
      }else{
        current = current.left
      }
    }
    
    return false
  }
  
    contains(data){
    
      const found = this.find(data)
      
      if(found){
        return true
      }
      
      return false
       
      
    }
  

   bfs(){
    
    let node = this.root;
    const queue = [node];
    const finalData = [ ]
    
    while(queue.length){
      
      node= queue.shift()
      if(node.left) queue.push(node.left)
      if(node.right) queue.push(node.right)
      finalData.push(node.data)
      
    }
    
    return finalData
 
    
  }
  
  preOrder(){
    
    const finalData = [];
    
    function traverse(node){
      
      finalData.push(node.data)
      
      if(node.left) traverse(node.left)
      if(node.right) traverse(node.right)
      
      
    }
    
    traverse(this.root)
    return finalData
    
  }
  
  postOrder(){
     const finalData = [];
    
    function traverse(node){
      if(node.left) traverse(node.left)
      if(node.right) traverse(node.right)
      
        finalData.push(node.data)
    }
    
    traverse(this.root)
    return finalData
    
  }
    inOrder(){
     const finalData = [];
    
    function traverse(node){
      if(node.left) traverse(node.left)
      
        finalData.push(node.data)
      if(node.right) traverse(node.right)
      
    }
    
    traverse(this.root)
    return finalData
    
  }
  
  maxNode(){
    if(!this.root) return null;
    
    let current = this.root;
    
    while(current.right){
    
       current = current.right
    }
    
    return current.data
  }
  
    minNode(){
    
     if(!this.root) return null 
    let current = this.root
    
    while(current.left){
    
       current = current.left
    }
    
    return current.data
  }

  
} 



mocha.setup('bdd');


var assert = chai.assert;

describe('Binary search tree',()=>{
  
  it("should add  new node to the tree",()=>{
    
      const tree = new Bst()
  
    tree.insert(5)
    tree.insert(3)
    tree.insert(2)
    tree.insert(4)
    
    assert.equal(3,tree.root.left.data)

  })

     it("find a node  ",()=>{
   
   
    const tree = new Bst()
  
    tree.insert(5)
    tree.insert(3)
    tree.insert(2)
    tree.insert(4)
    
    assert.equal(2,tree.find(2))
    assert.equal(false,tree.find(22))   
  })
      it("Max node in the tree  ",()=>{
   
   
    const tree = new Bst()


tree.insert(10);
tree.insert(4);
tree.insert(12);
tree.insert(1);
tree.insert(5);
tree.insert(11);
    
    assert.equal(12,tree.maxNode())
   
  })
    it("Min node in the tree  ",()=>{
   
   
    const tree = new Bst()


tree.insert(10);
tree.insert(4);
tree.insert(12);
tree.insert(1);
tree.insert(5);
tree.insert(11);
    
    assert.equal(1,tree.minNode())
   
  })
  
    it("Breadth-first search",()=>{
   
   
    const tree = new Bst()


tree.insert(10);
tree.insert(4);
tree.insert(12);
tree.insert(1);
tree.insert(5);
tree.insert(11);
    
    assert.deepEqual([10,4,12,1,5,11],tree.bfs())
   
  })
 
  
  
    it("Depth-first search",()=>{
   
   
    const tree = new Bst()


tree.insert(10);
tree.insert(4);
tree.insert(12);
tree.insert(1);
tree.insert(5);
tree.insert(11);
    
    assert.deepEqual([10,4,1,5,12,11],tree.preOrder())
    assert.deepEqual([1,5,4,11,12,10],tree.postOrder())
    assert.deepEqual([1,4,5,10,11,12],tree.inOrder())
   
  })
  
  
  
  
  
})



mocha.run()
              
            
!
999px

Console