资讯专栏INFORMATION COLUMN

二叉树的一些常见的操作

Aldous / 2219人阅读

摘要:节点类二叉树类实现了二叉树插入删除查找前序遍历中序遍历后序遍历层序遍历二叉树序列化和反序列化二叉树的常见操作增加删除查找先序中序后序层序遍历序列化二叉树先序层序序列化和反序列化先序反序列化层序序列化层序反序列化数据测试建立一棵二叉树参考资料

节点类
public class Node {
         public Node left;  
         public Node right;  
         public int data;  
         public Node(int data){  
                this.left = null;  
                this.right = null;  
                this.data = data;  
            }  
}
二叉树类

实现了二叉树插入、删除、查找、前序遍历、中序遍历、后序遍历、层序遍历、二叉树序列化和反序列化

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinaryTree {

    public Node root;
    public BinaryTree() {
        this.root=null;
    }
    /**
     * 二叉树的常见操作
     * 增加、删除、查找
     */
    public void insert(int data){
        Node node=new Node(data);
        if(this.root==null){
            this.root=node;
        }else{
            Node current=this.root;
            Node parent;
            while(true){
                parent=current;
                if(data
    /**
     * 先序、中序、后序、层序遍历
     */
    public void preOrderCur(Node head){
        if(head==null){
            return;
        }
        System.out.println(head.data+" ");
        preOrder(head.left);
        preOrder(head.right);
    }
    public void preOrder(Node head){
        if(head!=null){
            Stack stack=new Stack();
            stack.add(head);
            while(!stack.isEmpty()){
                head=stack.pop();
                System.out.println(head.data+" ");
                if(head.right!=null){
                    stack.push(head.right);
                }
                if(head.left!=null){
                    stack.push(head.left);
                }
            }
        }
    }
    
    public void inOrderCur(Node head){
        if(head==null){
            return ;
        }
        preOrder(head.left);
        System.out.println(head.data+" ");
        preOrder(head.right);
    }
    public void inOrder(Node head){
        if(head!=null){
            Stack stack=new Stack();
            while(!stack.isEmpty()||head!=null){
                if(head!=null){
                    stack.push(head);
                    head=head.left;
                }else{                
                    head=stack.pop();
                    System.out.println(head.data+" ");
                    head=head.right;
                }
            }
        }
    }
    
    public void posOrderCur(Node head){
        if(head==null){
            return;
        }
        preOrder(head.left);
        preOrder(head.right);
        System.out.println(head.data+" ");
    }
    public void posOrder(Node head){
        if(head!=null){
            Stack stack=new Stack();
            stack.push(head);
            Node c=null;
            while(!stack.isEmpty()){
                c=stack.peek();
                if(c.left!=null&&head!=c.left&&head!=c.right){
                    stack.push(c.left);
                }else if(c.right!=null &&head!=c.right){
                    stack.push(c.right);
                }else{
                    System.out.println(stack.pop().data+" ");
                    head=c;
                }
            }
        }
    }
    
    public void levelOrder(Node head){
        if(head==null){
            return;
        }
        Queue queue=new LinkedList();
        queue.offer(head);
        while(!queue.isEmpty()){
            head=queue.poll();
            System.out.println(head.data);  
            if(head.left!=null){
                queue.offer(head.left);
            }
            if(head.right!=null){
                queue.offer(head.right);
            }
        }
    }
/**
 * 序列化二叉树
 * 先序、层序序列化和反序列化
 */
    public String serialPre(Node head){
         if(head==null){
                return "#!";
            }
            String str=head.data+"!";
            str+=serialPre(head.left);
            str+=serialPre(head.right);
            return str;
    }
    /*先序反序列化*/
    public Node recoByPre(String preStr){
        String[] values=preStr.split("!");
        Queue queue=new LinkedList();
        for(int i=0;i!=values.length;i++){
            queue.offer(values[i]);
        }
        return reconPreOrder(queue);
    }
    public Node reconPreOrder(Queue queue){
        String value=queue.poll();
        if(value.equals("#")){
            return null;
        }
        Node head=new Node(Integer.valueOf(value));
        head.left=reconPreOrder(queue);
        head.right=reconPreOrder(queue);
        return head;
    }
    /*层序序列化*/
    public String serialLevel(Node head){
        if(head==null){
            return "#!";
        }
        String res=head.data+"!";
        Queue queue=new LinkedList();
        queue.offer(head);
        while(!queue.isEmpty()){
            head=queue.poll();
            if(head.left!=null){
                res+=head.left.data+"!";
                queue.offer(head.left);
            }else{
                res+="#!";
            }
            if(head.right!=null){
                res+=head.right.data+"!";
                queue.offer(head.right);
            }else{
                res+="#";
            }
        }
        return res;
    }
    /*层序反序列化*/
    public Node reconLevel(String str){
        String[] values=str.split("!");
        int index=0;
        Node head=createNode(values[index++]);
        Queue queue=new LinkedList();
        if(head!=null){
            queue.offer(head);
        }
        Node node=null;
        while(!queue.isEmpty()){
            node=queue.poll();
            node.left=createNode(values[index++]);
            node.right=createNode(values[index++]);
            if(node.left!=null){
                queue.offer(node.left);
            }
            if(node.right!=null){
                queue.offer(node.right);
            }
        }
        return head;
    }
    public Node createNode(String val){
        if(val.equals("#")){
            return null;
        }
        return new Node(Integer.valueOf(val));
    }
}

数据测试
public class test {
    public static void main(String[] args) {
        BinaryTree binTree=new BinaryTree();
        //建立一棵二叉树
        int[] data={25,15,10,5,36,65,52,45,42};
        for(int i=0;i
参考资料

《IT名企算法与数据结构题目最优解》左程云

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65166.html

相关文章

  • 使用JavaScript完成二叉一些基本操作

    摘要:另外,由于篇幅有限,本篇的重点在于二叉树的常见算法以及实现。常见的二叉树实现代码之前写过相关的文章,是关于如何创建及遍历二叉树的,这里不再赘述。同时我们注意到,在二叉树深度比较大的时候,我们光是比较左右是不够的。 本篇为复习过程中遇到过的总结,同时也给准备面试的同学一份参考。另外,由于篇幅有限,本篇的重点在于二叉树的常见算法以及实现。 常见的二叉树实现代码 之前写过相关的文章,是关于如...

    YPHP 评论0 收藏0
  • 数据结构与算法——叉树(上)

    摘要:什么是树前面说到的几种数据结构都是线性的,例如链表栈队列等,今天就来学习一种非线性的数据结构树。在上图中的几种二叉树中,有两个是比较特殊的,分别是满二叉树和完全二叉树。除了使用数组存储二叉树外,最常用的便是使用链表法来储存二叉树了。 1. 什么是树? 前面说到的几种数据结构都是线性的,例如链表、栈、队列等,今天就来学习一种非线性的数据结构——树。先来看看几种树的结构: showImg(...

    xeblog 评论0 收藏0
  • Map集合、散列表、红黑树介绍

    摘要:并且,我们的底层都是红黑树来实现的。红黑树是一种平衡二叉树因此它没有节点。 前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合: Collection总览 List集合就这么简单【源码剖析】 原本我是打算继续将Collection下的Set集合的,结果看了源码发现:Set集合实际上就是HashMap来构建的! showImg(https:/...

    2json 评论0 收藏0
  • 数据结构与算法——叉树(下)

    摘要:所以,如果中序遍历二叉搜索树,会得到一个有序的数据,时间复杂度是,所以二叉搜索树又叫做二叉排序树。所以,我们需要一种方式来维持二叉树的平衡,最好是将其维持为满二叉树或者完全二叉树,这就是后面会说到的平衡二叉查找树,常见的有树,红黑树。 1. 概述 前面的文章说到了二叉树,其实今天讲的二叉搜索(查找)树就是二叉树最常用的一种形式,它支持高效的查找、插入、删除操作,它的定义是这样的:对于树...

    Awbeci 评论0 收藏0

发表评论

0条评论

Aldous

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<