排序二叉樹的基本操作

來源:互聯網
上載者:User

標籤:turn   str   node   new   中序遍曆   輸入   empty   四種   sys   

描述

二叉樹的構建、插入新的結點、樹的先中後序以及層序四種遍曆

 

代碼

import java.util.LinkedList;import java.util.Queue;class Node{    public int data;    public Node left;    public Node right;    public Node(int data){        this.data=data;        this.left=null;        this.right=null;    }}public class BinaryTree {    private Node root;    public BinaryTree(){        root=null;    }    /*    * 插入到排序二叉樹    * */    public void insert(int data){        Node newNode=new Node(data);        if(root==null){            root=newNode;            return;        }        Node node=root;        Node parent;        while(true){            parent=node;            if(data<node.data){                node=node.left;                if(node==null){                    parent.left=newNode;                    return;                }            }else{                node=node.right;                if(node==null){                    parent.right=newNode;                    return;                }            }        }    }    /*    * 將數值輸入構建二叉樹    * */    public void buildTree(int[] data){        for(int i=0;i<data.length;i++){            insert(data[i]);        }    }    /*    * 先序遍曆    * */    public void preOrder(){        preOrder(root);    }    public void preOrder(Node node){        if(node==null) return;        System.out.print(node.data+" ");        preOrder(node.left);        preOrder(node.right);    }    /*    * 中序遍曆    * */    public void inOrder(){        inOrder(root);    }    public void inOrder(Node node){        if(node==null) return;        preOrder(node.left);        System.out.print(node.data+" ");        preOrder(node.right);    }    /*    * 後序遍曆    * */    public void postOrder(){        postOrder(root);    }    public void postOrder(Node node){        if(node==null) return;        preOrder(node.left);        preOrder(node.right);        System.out.print(node.data+" ");    }    /*    * 層序遍曆    * */    public void layerTranverse(){        Node node = root;        Queue<Node> queque=new LinkedList<Node>();        queque.add(root);        while(!queque.isEmpty()){            node=queque.poll();            System.out.print(node.data+" ");            if(node.left!=null) queque.add(node.left);            if(node.right!=null) queque.add(node.right);        }    }    public static void main(String[] args) {        BinaryTree bitree=new BinaryTree();        int[] data={2,8,7,4,9,3,1,6,7,5};        bitree.buildTree(data);        System.out.print("二叉樹的先序遍曆:");        bitree.preOrder();        System.out.println();        System.out.print("二叉樹的中序遍曆:");        bitree.inOrder();        System.out.println();        System.out.print("二叉樹的後序遍曆:");        bitree.postOrder();        System.out.println();        System.out.print("二叉樹的層序遍曆:");        bitree.layerTranverse();        System.out.println();    }}

 

排序二叉樹的基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.