最近学习了二叉树,这真是一颗神奇的树
什么是二叉树:包含根节点,中间节点,叶子节点,节点层次为二叉树的高,左子节点值小于父节点值,右子节点值大于父节点.
<script type="text/javascript">
function BinaryTree(){
//定义节点
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);
}
}
}
//构造节点
var nodes = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
nodes.forEach(function(key){
binaryTree.insert(key);
});
</script>
网友评论