基本介绍
存储方式
数组存储方式的分析
优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。
缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低。
链表存储方式分析
优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可,删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)。
树存储方式分析
能提高数据存储,读取的效率, 比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。
常用术语
二叉树概念
树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。二叉树的子节点分为左节点和右节点。
如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2n -1 , n 为层数,则我们称为满二叉树。
如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。
遍历结点
结点类中定义的方法
//前序遍历
public void preOrder() {
//输出父结点
System.out.println(this);
//递归向左子树前序遍历
if (this.left != null) {
this.left.preOrder();
}
//递归向右子树前序遍历
if (this.right != null) {
this.right.preOrder();
}
}
//中序遍历
public void infixOrder() {
//递归向左子树中序遍历
if (this.left != null) {
this.left.infixOrder();
}
//输出父节点
System.out.println(this);
//递归右子树中序遍历
if (this.right != null) {
this.right.infixOrder();
}
}
//后序遍历
public void postOrder() {
//递归向左子树后序遍历
if (this.left != null) {
this.left.postOrder();
}
//递归向右子树后序遍历
if (this.right != null) {
this.right.postOrder();
}
//输出父节点
System.out.println(this);
}
二叉树类中引用的方法
//前序遍历
public void preOrder() {
if (this.root != null)
root.preOrder();
else
System.out.println("二叉树为空");
}
//中序遍历
public void infixOrder() {
if (this.root != null)
this.root.infixOrder();
else
System.out.println("二叉树为空");
}
//后序遍历
public void postOrder() {
if (this.root != null)
this.root.postOrder();
else
System.out.println("二叉树为空");
}
查找结点
结点类中定义的方法
//前序遍历查找
public Node preOrderSearch(int no) {
Node resNode = null;
//比较当前结点
if (this.no == no) {
return this;
}
//左子树递归查找
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if (resNode != null) //在左子树找到就返回,上层的递归就不用向右子树找了
return resNode;
//右子树递归查找
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
//中序遍历查找
public Node infixOrderSearch(int no){
Node resNode = null;
//左子树递归查找
if(this.left!=null){
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) //在左子树找到就返回,上层的递归就不用向右子树找了
return resNode;
//比较当前结点
if (this.no == no) {
return this;
}
//右子树递归查找
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
//后序遍历查找
public Node postOrderSearch(int no){
Node resNode = null;
//左子树递归查找
if(this.left!=null){
resNode = this.left.postOrderSearch(no);
}
if (resNode != null) //在左子树找到就返回,上层的递归就不用向右子树找了
return resNode;
//右子树递归查找
if (this.right != null) {
resNode = this.right.postOrderSearch(no);
}
if (resNode != null) //在右子树找到就返回
return resNode;
//比较当前结点
if(this.no == no){
return this;
}
return null;
}
二叉树类中引用的方法
//前序查找
public Node preOrderSearch(int no){
if(root!=null){
return root.preOrderSearch(no);
}else {
return null;
}
}
//中序查找
public Node infixOrderSearch(int no){
if(root!=null){
return root.infixOrderSearch(no);
}else {
return null;
}
}
//后序查找
public Node postOrderSearch(int no){
if(root!=null){
return root.postOrderSearch(no);
}else {
return null;
}
}
删除结点
结点类中定义的方法
//叶子结点直接删除,非叶子结点删除子树
public void delNode(int no){
//左子结点不为空且左子结点就是要删除的结点
if(this.left!=null&&this.left.no == no){
this.left = null;
return;
}
//右子结点不为空且右子结点就是要删除的结点
if(this.right !=null&&this.right.no==no){
this.right=null;
return;
}
//向左子树进行递归删除
if(this.left!=null){
this.left.delNode(no);
}
//向右子树进行递归删除
if(this.right!=null){
this.right.delNode(no);
}
}
二叉树类中引用的方法
//删除结点
public void delNode(int no){
if(root!=null){
//判断root是不是要删除的结点
if(root.getNo() ==no){
root = null;
}else{
//递归删除
root.delNode(no);
}
}else {
System.out.println("无法删除,该树为空");
}
}
完整代码
package tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
Node node1 = new Node(1, "a");
Node node2 = new Node(2, "a");
Node node3 = new Node(3, "a");
Node node4 = new Node(4, "a");
//先手动创建二叉树
binaryTree.setRoot(node1);
node1.setLeft(node2);
node1.setRight(node3);
node3.setRight(node4);
//
// binaryTree.preOrder(); //1234
// binaryTree.infixOrder();//2134
// binaryTree.postOrder(); //2431
System.out.println(binaryTree.preOrderSearch(3));
System.out.println(binaryTree.infixOrderSearch(9));
System.out.println(binaryTree.postOrderSearch(3));
}
}
//定义BinaryTree二叉树
class BinaryTree {
private Node root;
public void setRoot(Node root) {
this.root = root;
}
//前序遍历
public void preOrder() {
if (this.root != null)
root.preOrder();
else
System.out.println("二叉树为空");
}
//中序遍历
public void infixOrder() {
if (this.root != null)
this.root.infixOrder();
else
System.out.println("二叉树为空");
}
//后序遍历
public void postOrder() {
if (this.root != null)
this.root.postOrder();
else
System.out.println("二叉树为空");
}
//前序查找
public Node preOrderSearch(int no){
if(root!=null){
return root.preOrderSearch(no);
}else {
return null;
}
}
//中序查找
public Node infixOrderSearch(int no){
if(root!=null){
return root.infixOrderSearch(no);
}else {
return null;
}
}
//后序查找
public Node postOrderSearch(int no){
if(root!=null){
return root.postOrderSearch(no);
}else {
return null;
}
}
//删除结点
public void delNode(int no){
if(root!=null){
//判断root是不是要删除的结点
if(root.getNo() ==no){
root = null;
}else{
//递归删除
root.delNode(no);
}
}else {
System.out.println("无法删除,该树为空");
}
}
}
//创建Node结点
class Node {
private int no;
private String name;
private Node left; //默认null
private Node right; //默认null
public Node(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "no=" + no + ", name='" + name + '\'';
}
//前序遍历
public void preOrder() {
//输出父结点
System.out.println(this);
//递归向左子树前序遍历
if (this.left != null) {
this.left.preOrder();
}
//递归向右子树前序遍历
if (this.right != null) {
this.right.preOrder();
}
}
//中序遍历
public void infixOrder() {
//递归向左子树中序遍历
if (this.left != null) {
this.left.infixOrder();
}
//输出父节点
System.out.println(this);
//递归右子树中序遍历
if (this.right != null) {
this.right.infixOrder();
}
}
//后序遍历
public void postOrder() {
//递归向左子树后序遍历
if (this.left != null) {
this.left.postOrder();
}
//递归向右子树后序遍历
if (this.right != null) {
this.right.postOrder();
}
//输出父节点
System.out.println(this);
}
//前序遍历查找
public Node preOrderSearch(int no) {
Node resNode = null;
//比较当前结点
if (this.no == no) {
return this;
}
//左子树递归查找
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if (resNode != null) //在左子树找到就返回,上层的递归就不用向右子树找了
return resNode;
//右子树递归查找
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
//中序遍历查找
public Node infixOrderSearch(int no){
Node resNode = null;
//左子树递归查找
if(this.left!=null){
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) //在左子树找到就返回,上层的递归就不用向右子树找了
return resNode;
//比较当前结点
if (this.no == no) {
return this;
}
//右子树递归查找
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
//后序遍历查找
public Node postOrderSearch(int no){
Node resNode = null;
//左子树递归查找
if(this.left!=null){
resNode = this.left.postOrderSearch(no);
}
if (resNode != null) //在左子树找到就返回,上层的递归就不用向右子树找了
return resNode;
//右子树递归查找
if (this.right != null) {
resNode = this.right.postOrderSearch(no);
}
if (resNode != null) //在右子树找到就返回
return resNode;
//比较当前结点
if(this.no == no){
return this;
}
return null;
}
//递归删除结点
//叶子结点直接删除,非叶子结点删除子树
public void delNode(int no){
//左子结点不为空且左子结点就是要删除的结点
if(this.left!=null&&this.left.no == no){
this.left = null;
return;
}
//右子结点不为空且右子结点就是要删除的结点
if(this.right !=null&&this.right.no==no){
this.right=null;
return;
}
//向左子树进行递归删除
if(this.left!=null){
this.left.delNode(no);
}
//向右子树进行递归删除
if(this.right!=null){
this.right.delNode(no);
}
}
}