### 树形结构的优点

**为什么要有树这种结构?**
数组和链表分析:
**数组**:在查找时因为有下标,所以查找的速度特别快还可以使用二分(插值)查找提高效率;
但是在增加或删除数据时,需将数组扩容或者减小容量(新创建一个数组将原数组需要的数组拷贝进去),
并将一些数据前移或者后移效率低下
**链表**:对于添加和删除操作效率较高,但是查找效率低(从头开始);
树结构可以改善这种情况。

### 二叉树

二叉树:
1.树有很多种,如果每个节点最多只有两个子节点就称为二叉树;
2.二叉树的每个字节点分为左子节点和右子节点;
3.如果二叉树的所有叶子节点都在最后一层,并且节点总数为2的n次方减一,(n为层数),则我们称为满二叉树;
4.如果所有叶子节点都在最后一层或者倒数第二层,而且最后一层的节点左连续,倒数第二层的叶子节点右连续,我们称之为完全二叉树。

二叉树的遍历

前序遍历

先输出父节点,在输出左节点,最后输出右节点。

中序遍历

先输出左节点,在输出父节点,最后输出右节点。

后序遍历

先输出左节点,在输出右节点,最后输出父节点。

二叉树的遍历代码实现

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree  btree =  new BinaryTree();
        Node root= new Node(1);
        Node nod2= new Node(2);
        Node nod3= new Node(3);
        Node nod4= new Node(4);
        root.left=nod2;
        root.right=nod3;
        nod2.left=nod4;
        btree.setRoot(root);
        //btree.preOrder();
        //btree.midOrder();
        btree.postOrder();
    }

}

class BinaryTree{
    private Node root;

    public void setRoot(Node root) {
        this.root = root;
    }
    //前序遍历
    public void preOrder(){
        if (this.root!=null){
            this.root.preOrder();
        }else{
            System.out.println("二叉树为空!");
        }
    }
    //前序遍历查找
    public Node preOrderSearch(int no){
        if (this.root!=null){
            return this.root.preOrderSearch(no);
        }else{
            return null;
        }
    }
    //中序遍历
    public void midOrder(){
        if (this.root!=null){
            this.root.midOrder();
        }else{
            System.out.println("二叉树为空!");
        }
    }
    //中序遍历查找
    public Node midOrderSearch(int no){
        if (this.root!=null){
            return this.root.midOrderSearch(no);
        }else{
            return null;
        }
    }
    //后序遍历
    public void postOrder(){
        if (this.root!=null){
            this.root.postOrder();
        }else{
            System.out.println("二叉树为空!");
        }
    }
    //后序遍历查找
    public Node postOrderSearch(int no){
        if (this.root!=null){
            return this.root.postOrderSearch(no);
        }else{
            return null;
        }
    }
}

class  Node{
    int no;
    Node left;
    Node  right;
    public Node(){
    }
    public Node(int no){
        this.no=no;
    }
    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                '}';
    }

    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }
    //遍历查找
    public Node preOrderSearch(int no){
        if(this.no==no){
            return this;
        }
        Node resNode =null;
        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 void midOrder(){
        if(this.left!=null){
            this.left.midOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.midOrder();
        }
    }
    //遍历查找
    public Node midOrderSearch(int no){
        Node resNode =null;
        if(this.left!=null){
            resNode=this.left.midOrderSearch(no);
        }
        if (resNode!=null){
            return resNode;
        }
        if(this.no==no){
            return this;
        }
        if(this.right!=null){
            resNode=this.right.midOrderSearch(no);
        }
        return resNode;
    }
    //后序遍历
    public void postOrder(){
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
        System.out.println(this);
    }
    //遍历查找
    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;
        }else{
            return null;
        }
    }
}

 

posted @ 2021-05-03 10:30  素色学习  阅读(36)  评论(0编辑  收藏  举报