Javascript學習筆記之:實現排序二叉樹
標簽:
JavaScript
随着web页面应用体验的提升,对于前端代码的性能要求也变得越来越高。
这里简单实现了用javascript进行排序二叉树算法的实现。
<script>
function binarySort() {
var Node = function(key){
this.key = key;
this.left = null;
this.right = null;
}
var root = null;
var insertNode = function(node, newNode){
if(newNode.key < node.key){
if(node.left === null)
node.left = newNode;
else
insertNode(node.left, newNode);
}else{
if(node.right === null)
node.right = newNode;
else
insertNode(node.right, newNode);
}
}
this.insert = function(key){
var newNode = new Node(key);
if(root === null)
root = newNode;
else
insertNode(root, newNode);
}
};
window.onload = function() {
alert("hello!this is binarysortTree");
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new binarySort();
nodes.forEach(function(key){
binaryTree.insert(key);
})
}
</script>
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦