이 진 트 리 - 자바 script 구현
github 코드
사실 여기 서 쓴 것 은 바로 원 코드 입 니 다. 데이터 구 조 를 배 우 는 것 이 어렵 습 니 다. 이 진 트 리 의 각종 작업 의 핵심 입 니 다. 저 는 지금 깨 달 은 것 은 재 귀, 재 귀, 재 귀 입 니 다. 자신 이 어렵 게 생각 하 는 지식 을 배 워 야 발전 할 수 있 습 니 다.응, 이 닭 볶 음탕 은 정말 시시 해!!
/**
*
* @param val
* @constructor
*/
function Node(val, left, right) {
this.val = val;
this.left = left;
this.right = right;
}
Node.prototype.show = function() {
return this.val;
}
/**
*
* @constructor
*/
function BTree() {
this.root = null;
this.depthBT = 0;
}
BTree.prototype = {
constructor : BTree,
add : function(val){
// , ,
// 1. ,
// 2. , ,
// 3. , , , ,
// 4. , ,
// 5. , , ,
var newNode = new Node(val, null, null);
if(!this.root) {
//
this.root = newNode;
}else {
var current = this.root,
parent;// current ,
while(current) {
parent = current;
if(val < current.val) {
//
current = current.left;
if(current == null ) {
parent.left = newNode;
return;
}
}else {
current = current.right;
if(current == null) {
parent.right = newNode;
return;
}
}
}
}
},
depth : function(node) {
// , , ,
// , node.left node.right, return 0
// ldepth rdepth, +1, 1
// , ,
return !node ? 0 : Math.max(this.depth(node.left),this.depth(node.right)) + 1;
// if(!node) {
// return 0;
// }else {
// var ldepth = this.depth(node.left);
// var rdepth = this.depth(node.right);
// return ldepth > rdepth ? ldepth + 1 : rdepth + 1;
// }
},
preorder : function(node) {
// : 23→16→45
// , , node (23)
// , node , (16), .left ,
// .right ,
// , node , (45), .left ,
// .right ,
//
if(node) {
console.log(node.show());
this.preorder(node.left);
this.preorder(node.right);
}
},
inorderTraversal : function(node) {
//
if(node){
this.inorderTraversal(node.left);
console.log(node.show());
this.inorderTraversal(node.right);
}
},
postorderTraversal : function(node) {
//
if(node) {
this.postorderTraversal(node.left);
this.postorderTraversal(node.right);
console.log(node.show());
}
},
BFS : function(node) {
// 1. ( ),
// 2. ,
// 3. ,
//
var nodes = [];
if(!node) {
console.log('empty tree');
}else {
nodes.push(node);
while(nodes.length !== 0) {
node = nodes.shift();//
console.log(node.val);
if(node.left) {
nodes.push(node.left);
}
if(node.right) {
nodes.push(node.right);
}
}
}
},
searchMin : function(node) {
// ,
//
while(node.left) {
node = node.left;
}
return node;
},
searchMax : function(node) {
//
while(node.right) {
node = node.right;
}
return node;
},
find : function(val) {
// ,
//
var node = this.root,
parent = node;//
while(node) {
if(node.val === val) {
//
return {nodeParent : parent,findNode : node};
}else{
parent = node;
if(node.val > val) {
node = node.left;
}else {
node = node.right;
}
}
}
return null;
},
removeNode : function(val) {
// 1. , , null
// 2. ,
// 3. ,(1) (2)
//
var currentobj = this.find(val),
parentNode,//
currentNode;//
if(currentobj){
parentNode = currentobj.nodeParent;//
currentNode = currentobj.findNode;//
if(currentNode.left == null && currentNode.right == null){
choosePos(null);
}else if(currentNode.left && currentNode.right){
var max = this.searchMax(currentNode.right).val,//
maxNode = this.find(max);// ,
currentNode.val = max;//
maxNode.nodeParent.right = null;// , null
}else if(currentNode.right){// ,
choosePos(currentNode.right);
}else if(currentNode.left) {// ,
choosePos(currentNode.left);
}
}else{
return ' ';
}
// , , ,
function choosePos(setValue){
currentNode.val > parentNode.val ? parentNode.right = setValue : parentNode.left = setValue;
}
}
}
var bt = new BTree(),
arrTree = [23,45,16,37,3,4,99,22,44,52,42,10,100,1];
arrTree.forEach(function(i){
//
bt.add(i);
});
console.log(bt);
console.log(' ');
bt.preorder(bt.root);
console.log(' ');
bt.inorderTraversal(bt.root);
console.log(' ');
bt.postorderTraversal(bt.root);
//
console.log(bt.depth(bt.root));
//
console.log(' ' + bt.searchMin(bt.root).val);
//
console.log(' ' + bt.searchMax(bt.root).val);
//
console.log(' :' + bt.find(99).nodeParent.val);
console.log(' :' + bt.find(99).findNode.val);
//
console.log(' :' + bt.find(52).nodeParent.val);
bt.removeNode(52);
console.log(' :' + bt.find(52));
console.log(bt.removeNode(55));
현재 실현
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
STL 학습노트(6) 함수 객체모방 함수는 모두pass-by-value이다 함수 대상은 값에 따라 전달되고 값에 따라 되돌아오기 때문에 함수 대상은 가능한 한 작아야 한다(대상 복사 비용이 크다) 함수 f와 대상 x, x 대상에서 f를 호출하면:...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.