二叉树的基本信息

1. 什么是二叉树

  每个结点的度均不超过2的有序树称为二叉树
  度:结点拥有的孩子数
  有序树:树中结点的各子树从左到右是有次序的,不能随意变换,则为有序树,
  否则为无序树 

2. 二叉树的状态

    二叉树有五种状态:即 空二叉树、只有根节点的二叉树、只有左子树的二叉树、只有
右子树的二叉树、左子树和右子树都存在的二叉树

3. 二叉树的性质

 性质1:在二叉树的第i层上最多有2的i-1次方个结点(根是第一层)
 性质2:高度为h的二叉树最多由2的h次方-1个结点
 性质3:对任意以可二叉树T,若其叶子结点(终端节点)为x,度为2 的节点数为y,那么y = x-1
 性质4:有n个节点的完全二叉树的高度为[log n]+1,其中[log n]是向下取整
 性质5:含有n>=1个节点的二叉树的高度至多为n-1,至少为[log n]+1,其中[log n]是向下取整
 性质6:如果对一颗有n个节点的完全二叉树的节点进行编号,则对任一节点i(1<=i<=n), 有
     (1) 如果i=1,则结点i时二叉树的根,无双亲
     (2) 如果2i>n,则结点i没有左孩子,否则其左孩子为2i
     (3) 如果2i+1>n,则结点无右孩子,否则其右孩子是结点2i+1

5. 满二叉树与完全二叉树的关系

     满二叉树:每层的节点数都要达到最大
     完全二叉树:在满二叉树的基础上,在最后一层从最右端删除相邻的结点
     关系:满二叉树从最后一层的最右端删除相邻的结点转变为完全二叉树,完全二叉树
     的最后一层加上结点,使得节点数达到该层的最大节点数转换为完全二叉树

6. 二叉链表与三叉链表

 二叉链表里面包含的信息为:结点本身的数据、左子树的地址、右子树的地址

 三叉链表里面包含的信息为:结点本身的数据、左子树地址、右子树地址、父结点地址

7. java写二叉树
(1) 首先定义一个Node类,它用来定义每个节点所包含的信息,数据、左子树、右子树

public class Node {
    Object value;
    Node leftchild;
    Node rightchild;
    public Node(Object value) {
    	super();
	    this.value = value;
    }
    public Node(Object value, Node leftchild, Node rightchild) {
	    super();
    	this.value = value;
	    this.leftchild = leftchild;
    	this.rightchild = rightchild;
    }
    @Override
    public String toString() {
	    return "Node [value=" + value + ", leftchild=" + leftchild + ", 
	            rightchild=" + rightchild + "]";
    }
}

(2)实现二叉树的一些方法

public class BinaryTree   {
    public Node root;  // 根结点
    public BinaryTree() {
    }
    public BinaryTree(Node root) {
    	super();
    	this.root = root;
	}
    public int getHeight(Node root) { // 树的深度
    	if(root == null) {
	    	return 0;
    	}else {
	    	// 得到左子树的深度
	    	BinaryTree bt1 = new BinaryTree(root.leftchild);
	    	int nl = bt1.getHeight(root.leftchild);
		    //得到右子树的深度
	    	BinaryTree bt2 = new BinaryTree(root.rightchild);
	    	int nr = bt2.getHeight(root.rightchild);
	    	//这两个深度去较大的并加1作为树的深度
	    	return nl > nr ? nl+1 : nr+1;
    	}
	}
    public boolean isEmpty() { // 树是否为空
    	if (root == null) {
    		return true;
	    }
    	return false;
	}
	public void pre() { // 先序遍历
    	if (root != null) {
            // 输出根
	    	System.out.print(root.value+"  ");
	    	// 创建左子树
	    	BinaryTree bt1 = new BinaryTree(root.leftchild);
	    	bt1.pre();
		    // 创建右子树
	    	BinaryTree bt2 = new BinaryTree(root.rightchild);
	    	bt2.pre();
	    	//System.out.println();
	    }
	}
	public void mid() { // 中序遍历
	    if(root != null) {
    	//第一步: 创建左子树
    	BinaryTree bt1 = new BinaryTree(root.leftchild);
    	bt1.mid();
	    //第二步:输出根节点
    	System.out.print(root.value+"  ");
    	//第三步:创建右子树
    	BinaryTree bt2 = new BinaryTree(root.rightchild);
		bt2.mid();
    	}
	}
    public void fin() {  // 后序遍历  5 4 3 7 6 2 1
    	if(root != null) {
    		//第一步: 创建左子树
    		BinaryTree bt1 = new BinaryTree(root.leftchild);
		    bt1.fin();
	    	//第二步:创建右子树
	    	BinaryTree bt2 = new BinaryTree(root.rightchild);
	    	bt2.fin();
	    	//第三步:输出根节点
    		System.out.print(root.value+"  ");
    		}
	}
}

(3) 测试类

public class BTTest {
public static void main(String[] args) {
	// 创建二叉树
	Node n7 = new Node(7,null,null);
	Node n6 = new Node(6,null,n7);
	Node n5 = new Node(5,null,null);
	Node n4 = new Node(4,null,n5);
	Node n3 = new Node(3,null,null);
	Node n2 = new Node(2,n3,n6);
	Node n1 = new Node(1,n4,n2);
	BinaryTree bt = new BinaryTree(n1);
	//先序遍历
	System.out.print("先序遍历:    ");
	bt.pre();
	System.out.println();
	//中序遍历
	System.out.print("中序遍历:    ");
	bt.mid();
	System.out.println();
	//后序遍历
	System.out.print("后序遍历:    ");
	bt.fin();
	System.out.println();
	//该二叉树是否为空
	System.out.println("该二叉树是否为空:  "+bt.isEmpty());
	//深度
	System.out.print("二叉树的深度:");
	System.out.println(bt.getHeight(n1));
	System.out.println( );
}
}

(4)结果如下图所示:

先序遍历:    1  4  5  2  3  6  7  
中序遍历:    4  5  1  3  2  6  7  
后序遍历:    5  4  3  7  6  2  1  
该二叉树是否为空:  false
二叉树的深度:4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值