1. 程式人生 > >二叉樹的基本操作——java實現

二叉樹的基本操作——java實現

樹 的資料結構


public class TreeNode {
    private int data;
    private TreeNode LeftNode;
    private TreeNode RightNode;

    public TreeNode() {
        // TODO Auto-generated constructor stub
    }
    public TreeNode(int data,TreeNode left,TreeNode right) {
        this.data=data;
        this
.LeftNode=left; this.RightNode=right; } public int getData() { return data; } public void setData(int data) { this.data = data; } public TreeNode getLeftNode() { return LeftNode; } public void setLeftNode(TreeNode leftNode) { LeftNode = leftNode; } public
TreeNode getRightNode() { return RightNode; } public void setRightNode(TreeNode rightNode) { RightNode = rightNode; } }

基本操作

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


public class shujuJiegou {

    //遞迴前序遍歷
    public void preOrder
(TreeNode root) { if(root!=null) { System.out.print(root.getData()+" "); preOrder(root.getLeftNode()); preOrder(root.getRightNode()); } } //非遞迴前序 public void preOrderNoDigui(TreeNode root) { Stack<TreeNode> stack=new Stack<>(); while (true) { while (root!=null) { System.out.print(root.getData()+" "); stack.push(root); root=root.getLeftNode(); } if(stack.isEmpty()) break; root=stack.pop(); root=root.getRightNode(); } } //遞迴中序 public void inOrder(TreeNode root) { if(root!=null) { inOrder(root.getLeftNode()); System.out.print(root.getData()+" "); inOrder(root.getRightNode()); } } //非遞迴中序 public void inOrderNoDigui(TreeNode root) { Stack<TreeNode> stack=new Stack<>(); while (true) { while (root!=null) { stack.push(root); root=root.getLeftNode(); } if(stack.isEmpty()) break; root=stack.pop(); System.out.print(root.getData()+" "); root=root.getRightNode(); } } //遞迴後序 public void deOrder(TreeNode root) { if (root!=null) { deOrder(root.getLeftNode()); deOrder(root.getRightNode()); System.out.print(root.getData()+" "); } } //非遞迴後序 public void deOrderNoDigui(TreeNode root) { Stack<TreeNode> stack=new Stack<>(); while (true) { if(root!=null) { stack.push(root); root=root.getLeftNode(); }else { if(stack.isEmpty()) return; if(stack.lastElement().getRightNode()==null) { root=stack.pop(); System.out.print(root.getData()+" "); while (root==stack.lastElement().getRightNode()) { System.out.print(stack.lastElement().getData()+" "); root=stack.pop(); if(stack.isEmpty()) break; } } if(!stack.isEmpty()) root=stack.lastElement().getRightNode(); else { root=null; } } } } //層次遍歷 public void levelOrder(TreeNode root) { TreeNode temp; Queue<TreeNode> queue=new LinkedList<>(); queue.offer(root); while (!queue.isEmpty()) { temp=queue.poll(); System.out.print(temp.getData()+" "); if(temp.getLeftNode()!=null) { queue.offer(temp.getLeftNode()); } if(temp.getRightNode()!=null) { queue.offer(temp.getRightNode()); } } } public static void main(String[] args) { TreeNode node10=new TreeNode(10,null,null); TreeNode node8=new TreeNode(8,null,null); TreeNode node9=new TreeNode(9,null,node10); TreeNode node4=new TreeNode(4,null,null); TreeNode node5=new TreeNode(5,node8,node9); TreeNode node6=new TreeNode(6,null,null); TreeNode node7=new TreeNode(7,null,null); TreeNode node2=new TreeNode(2,node4,node5); TreeNode node3=new TreeNode(3,node6,node7); TreeNode node1=new TreeNode(1,node2,node3); shujuJiegou tree=new shujuJiegou(); System.out.println("------前序遍歷-------"); tree.preOrder(node1); System.out.println(); tree.preOrderNoDigui(node1); System.out.println(); System.out.println("------中序遍歷-------"); tree.inOrder(node1); System.out.println(); tree.inOrderNoDigui(node1); System.out.println(); System.out.println("------後序遍歷-------"); tree.deOrder(node1); System.out.println(); tree.deOrderNoDigui(node1); System.out.println(); System.out.println("------層次遍歷-------"); tree.levelOrder(node1); System.out.println(); tree.levelOrder(node1); System.out.println(); } }