<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>
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()

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.