二叉树是指数的度为2的有序数。它是一种非常重要的数结构,在计算机领域中有着广泛的应用。二叉树的递归定义为:二叉树或者是一颗空数,或者是一颗由一个根结点和两颗互不相交的分别称为根的左子树和右子树所组成的非空树,左子树和右子树又同样都是一颗二叉树。
二叉树的存储结构
同单链表相同,二叉链表既可由独立分配的结点链接而成,也可由数组中的元素结点链接而成。
独立结点
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class BTreeNode { Object element; BTreeNode left,rigth; public BTreeNode(Object element){ this.element=element; left=rigth=null; } public BTreeNode(Object element,BTreeNode left,BTreeNode rigth){ this.element=element; this.left=left; this.rigth=rigth; } }
|
元素结点
1 2 3 4 5 6 7 8 9 10 11 12
| public class ABTreeNode { Object element; int left,rigth; public ABTreeNode(Object element){ this.element=element; } public ABTreeNode(Object element,int left,int rigth){ this.element=element; this.left=left; this.rigth=rigth; } }
|
二叉树的方法
创建二叉树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public void creat(){ System.out.println("请按照树的先序遍历顺序组织数据"); System.out.println("一个结点的二叉树11,输入:11 0 0"); root=creat0(); }
private BTreeNode creat0(){ BTreeNode p; int date; System.out.print("输入数据:"); date=in.nextInt(); if(date==0){ p=null; }else{ p=new BTreeNode(date); p.left=creat0(); p.rigth=creat0(); } return p; }
|
二叉树遍历
先序遍历
1 2 3 4 5 6 7
| public void preOrder(BTreeNode bt){ if(bt!=null){ System.out.print(bt.element+" "); preOrder(bt.left); preOrder(bt.rigth); } }
|
中序遍历
1 2 3 4 5 6 7
| public void inOrder(BTreeNode bt){ if(bt!=null){ preOrder(bt.left); System.out.print(bt.element+" "); preOrder(bt.rigth); } }
|
后序遍历
1 2 3 4 5 6 7
| public void postOrder(BTreeNode bt){ if(bt!=null){ preOrder(bt.left); preOrder(bt.rigth); System.out.print(bt.element+" "); } }
|
层次遍历
1 2 3 4 5 6 7 8 9 10 11
| public void levelOrder(BTreeNode bt){ Queue que=new LinkedList(); BTreeNode p=null; que.add(bt); while(!que.isEmpty()){ p=(BTreeNode)que.poll(); System.out.print(p.element+" "); if(p.left!=null) que.add(p.left); if(p.rigth!=null) que.add(p.rigth); } }
|
其他方法
判断二叉树是否为空
1 2 3
| public boolean isEmpty(){ return root==null; }
|
求二叉树的深度
1 2 3 4 5 6 7 8 9 10 11 12
| public int depthBTree(BTreeNode bt){ if(bt==null) return 0; else{ int left=depthBTree(bt.left); int rigth=depthBTree(bt.rigth); if(left>rigth) return left+1; else return rigth+1; } }
|
求二叉树中的结点数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public int countBTree(BTreeNode bt){ if(bt==null) return 0; else return countBTree(bt.left)+countBTree(bt.rigth)+1; }
public int findBTree(BTreeNode bt,int x){ if(bt==null) return -1; else{ if(bt.element==x) return bt.element; else{ int y; y=findBTree(bt.left, x); if(y!=-1) return y; y=findBTree(bt.rigth, x); if(y!=-1) return y; return -1; } } }
|
清除二叉树
1 2 3
| public void cleatBTree(){ root=null; }
|
实现代码
结点
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class BTreeNode { int element; BTreeNode left,rigth; public BTreeNode(int element){ this.element=element; left=rigth=null; } public BTreeNode(int element,BTreeNode left,BTreeNode rigth){ this.element=element; this.left=left; this.rigth=rigth; } }
|
二叉树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import java.util.LinkedList; import java.util.Queue; import java.util.Scanner;
public class linkBinaryTree { protected BTreeNode root; private Scanner in =new Scanner(System.in); public linkBinaryTree(){ root=null; } public BTreeNode getRoot(){ return root; } public void creat(){ System.out.println("请按照树的先序遍历顺序组织数据"); System.out.println("一个结点的二叉树11,输入:11 0 0"); root=creat0(); } private BTreeNode creat0(){ BTreeNode p; int date; System.out.print("输入数据:"); date=in.nextInt(); if(date==0){ p=null; }else{ p=new BTreeNode(date); p.left=creat0(); p.rigth=creat0(); } return p; } public void preOrder(BTreeNode bt){ if(bt!=null){ System.out.print(bt.element+" "); preOrder(bt.left); preOrder(bt.rigth); } } public void inOrder(BTreeNode bt){ if(bt!=null){ preOrder(bt.left); System.out.print(bt.element+" "); preOrder(bt.rigth); } } public void postOrder(BTreeNode bt){ if(bt!=null){ preOrder(bt.left); preOrder(bt.rigth); System.out.print(bt.element+" "); } } public void levelOrder(BTreeNode bt){ Queue que=new LinkedList(); BTreeNode p=null; que.add(bt); while(!que.isEmpty()){ p=(BTreeNode)que.poll(); System.out.print(p.element+" "); if(p.left!=null) que.add(p.left); if(p.rigth!=null) que.add(p.rigth); } } public boolean isEmpty(){ return root==null; } public int depthBTree(BTreeNode bt){ if(bt==null) return 0; else{ int left=depthBTree(bt.left); int rigth=depthBTree(bt.rigth); if(left>rigth) return left+1; else return rigth+1; } } public int countBTree(BTreeNode bt){ if(bt==null) return 0; else return countBTree(bt.left)+countBTree(bt.rigth)+1; } public int findBTree(BTreeNode bt,int x){ if(bt==null) return -1; else{ if(bt.element==x) return bt.element; else{ int y; y=findBTree(bt.left, x); if(y!=-1) return y; y=findBTree(bt.rigth, x); if(y!=-1) return y; return -1; } } } public void cleatBTree(){ root=null; } }
|