数据结构与算法-树2-二叉树(javascript描述)

二叉树定义

二叉树(BinaryTree)是n(n≥0)个结点的有限集,它或者是空集(n=0),或者由一个根结点及两棵互不相交的、分别称作这个根的左子树和右子树的二叉树组成。

二叉树有五种形态存在:

空集 - 有左子树无右子树 - 有右子树无左子树 - 有左右子树 - 无左右子树


满二叉树(FullBinaryTree) 
     一棵深度为k且有2的k次方-1个结点的二又树称为满二叉树。
     满二叉树的特点:
  (1) 每一层上的结点数都达到最大值。即对给定的高度,它是具有最多结点数的二叉树。
  (2) 满二叉树中不存在度数为1的结点,每个分支结点均有两棵高度相同的子树,且树叶都在最下一层上。

完全二叉树(Complete BinaryTree) 

 若一棵二叉树至多只有最下面的两层上结点的度数可以小于2,并且最下一层上的结点都集中在该层最左边的若干位置上,则此二叉树称为完全二叉树。
  特点:
  (1) 满二叉树是完全二叉树,完全二叉树不一定是满二叉树。
  (2) 在满二叉树的最下一层上,从最右边开始连续删去若干结点后得到的二叉树仍然是一棵完全二叉树。
  (3) 在完全二叉树中,若某个结点没有左孩子,则它一定没有右孩子,即该结点必是叶结点。

完全二叉树可以采用数组储存,原因是可以通过数组下标推出其双亲和子节点。

用javascript表示

bintree.js

/**
 * ------------------------------------------------------------------
 * define the structure of bintree
 * ------------------------------------------------------------------
 */

 /**
 * Node class
 *
 * @param    {Node}  left    leftchild
 * @param    {Node}  right  rightchild
 * @param    {value}  value  value of the node
 *
 * @date     2014-11-4
 * @author   simonwoo
 */
function Node(left,right,value){
	this.left = left;
	this.right = right;
	this.value = value;
}
Node.prototype.setLeft = function(left){
	this.left = left;
}
Node.prototype.setRight = function(right){
	this.right = right;
}
Node.prototype.getvalue = function(){
	return this.value;
}

/**
 * BinTree class
 *
 * @param    {Node}  root    root node of the tree
 *
 * @date     2014-11-4
 * @author   simonwoo
 */
function BinTree(root){
	this.root = root;
}
//前序遍历
BinTree.prototype.preorder = function(node){
	if(node==null)return;
	
	console.log(' ' + node.value + ' ');
	this.preorder(node.left);
	this.preorder(node.right);
}
//中序遍历
BinTree.prototype.inorder = function(node){
	if(node==null)return;

	this.inorder(node.left);
	console.log(' ' + node.value + ' ');
	this.inorder(node.right);
}
//后序遍历
BinTree.prototype.postorder = function(node){
	if(node==null)return;
	
	this.postorder(node.left);
	this.postorder(node.right);
	console.log(' ' + node.value + ' ');
}

index.html

表示下面这棵二叉树


<!doctype html>  
<html>  
   <!--head-->  
   <head>  
       <meta charset="utf-8">  
       <title>bintree</title>  
   </head>  
   <!--body-->  
   <body>  
   </body>  
   <script src = 'bintree.js'></script>
   <script type="text/javascript">
          var node1 = new Node(null,null,1);
          var node2 = new Node(null,null,2);
          var node3 = new Node(null,null,3);
          var node4 = new Node(null,null,4);
          var node5 = new Node(null,null,5);
          var node6 = new Node(null,null,6);
          var node7 = new Node(null,null,7);
          //set the relations
          node1.setLeft(node2);
          node1.setRight(node3);

          node2.setLeft(node4);
          node2.setRight(node5);

          node3.setLeft(node6);
          node3.setRight(node7);

          var bt = new BinTree(node1);
          bt.preorder(bt.root);
          bt.inorder(bt.root);
          bt.postorder(bt.root);
   </script>
</html>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值