본문 바로가기

코드스테이츠(Immersive)/스프린트

Datatstructures - Tree

const Tree = function(value) {
	const newTree = {};
    newTree.value = value;
    
    newTree.children = [];
    
    extend(newTree, treeMethods);
    return newTree;
}

var extend = function(to, from){
	for(var key in from){
    	to[key] = from[key]
    }
};

const treeMethods = {};

treeMethods.addChild = function(val) {
	var nTree = Tree(val);
    this.children.push(nTree);
}

treeMethods.contains = function(target) {
	if(this.value === target){
    	return true;
      } else {
      for(let i of this.children) {
      	if(i.contains(target)){
        	return true;
        }
    }
    return false;
    }
};

 

const Tree = function(value){
    const newTree = Object.create(treeMethods);
    newTree.value = value;
    newTree.children = [];
    
    return newTree;
 }
 const treeMethods = {};
 
 treeMethods.addChild = function(value) {
 	this.chidlren.push(Tree(value));
    }
    
 treeMethods.contains = function(target) {
 	let result = false;
        let search = function(obj){
    	if(obj.value === target){
        	result = true;
        } else {
       		obj.chidlren.forEach(value => {
            	search(value);
            });
        }
    }
 	search(this);
 	return result;
 }

 

const Tree = function(value) {
    const newTree = {};
    newTree.value = value;
    newTree.chidlren = [];
    
    extend(newTree, treeMethods)
    
    return newTree;
}

const treeMethods = {};

treeMethods.addChild = function(value){
    var tree = new Tree(value);
    tree.value = value;
    this.children.push(tree);
};

treeMethods.contains = function(target){
    var isContain = false;
    const checkAgain = (children, target) => {
    	for(let i=0; i< children.length; i++){
        	if(target === chidlren[i].value) {
            	isContain = true
            } else {
            	checkAgain(children[i].children, target)
            }
         }
      }
 checkAgain(this.children, target)
 return isContain
 };

 

const Tree = function(value) {
    const newTree = Object.create(treeMethods);
    newTree.value = value;
    
    newTree.children = [];
    
   	return newTree;
}

const treeMethods = {};

treeMethods.addChild = function(value){
	this.children.push(new Tree(value));
};

treeMethods.contains = function(target) {
	let answer = false;
    function findTarget(node, target){
    	for(let i in node.children){
        	if(node.children[i].value === target){
            	answer = true;
            }
         	findTarget(node.children[i], target);
            }
        }
    findTarget(this, target);
    return answer;
}

 

const Tree = function(value) {
	const newTree = {};
    newTree.value = value;
    
    newTree.children = [];
    extend(newTree, treeMethods);
    extend(newTree.children, treeMethods);
    return newTree;
}

treeMethods.contains = function(target) {
	let result = false;
    function inner(tree) {
    	if(tree.value === target) {
        	result = true;
        }
        if(tree.children) {
        	for(let i = 0; i<tree.children.length; i++){
            	inner(tree.children[i]);
            }
       }
   }
   inner(this);
   return result;
}

 

'코드스테이츠(Immersive) > 스프린트' 카테고리의 다른 글

Server Side Techniques(Promise)  (0) 2019.08.18
Event loop  (0) 2019.08.12
리액트  (0) 2019.08.05
Web Architecture  (0) 2019.08.02
n-queens 스프린트 진행  (0) 2019.08.02