1. 程式人生 > >Java-線索二叉樹的實現

Java-線索二叉樹的實現

概念性的東西,自行百度。

按照國際管理,直接上程式碼來分析。

1、Node節點類

package com.tree.thread;

/**
 * Author: lihao
 * Date:2017/8/30
 * Description:ThreadBinaryTree Node
 */
public class Node
{
    private int data;
    private Node left;
    private boolean leftIsThread;        // 左孩子是否為線索
    private Node right;
    private boolean rightIsThread;       // 右孩子是否為線索

    public Node(int data)
    {
        this.data = data;
        this.left = null;
        this.leftIsThread = false;
        this.right = null;
        this.rightIsThread = false;
    }

    public int getData()
    {
        return data;
    }

    public void setData(int data)
    {
        this.data = data;
    }

    public Node getLeft()
    {
        return left;
    }

    public void setLeft(Node left)
    {
        this.left = left;
    }

    public boolean isLeftIsThread()
    {
        return leftIsThread;
    }

    public void setLeftIsThread(boolean leftIsThread)
    {
        this.leftIsThread = leftIsThread;
    }

    public Node getRight()
    {
        return right;
    }

    public void setRight(Node right)
    {
        this.right = right;
    }

    public boolean isRightIsThread()
    {
        return rightIsThread;
    }

    public void setRightIsThread(boolean rightIsThread)
    {
        this.rightIsThread = rightIsThread;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj instanceof Node)
        {
            Node temp = (Node) obj;
            if (temp.getData() == this.data)
            {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode()
    {
        return super.hashCode() + this.data;
    }
}

2、建立二叉樹、二叉樹中序線索化(線索化有3種,此處單講中序)

package com.tree.thread;

public class ThreadTree {
    private Node root;         // 根節點
    private Node pre = null;   // 線索化的時候儲存前驅

    public ThreadTree() {
        this.root = null;
        this.pre = null;
    }

    public ThreadTree(int[] data) {
        this.pre = null;
        this.root = createTree(data, 0);   // 建立二叉樹
    }

    /**
     * 建立二叉樹
     */
    public Node createTree(int[] data, int index) {
        if (index >= data.length) {
            return null;
        }
        Node node = new Node(data[index]);
        node.setLeft(createTree(data, 2 * index + 1));
        node.setRight(createTree(data, 2 * index + 2));
        return node;
    }

    /**
     * 將以root為根節點的二叉樹線索化  中序法
     */
    public void inThread(Node root) {
        if (root != null) {
            inThread(root.getLeft());     // 線索化左孩子
            if (null == root.getLeft())   // 左孩子為空
            {
                root.setLeftIsThread(true);    // 將左孩子設定為線索
                root.setLeft(pre);
            }
            if (pre != null && null == pre.getRight())  // 右孩子為空
            {
                pre.setRightIsThread(true);
                pre.setRight(root);
            }
            pre = root;                 //每次將當前節點設定為pre
            inThread(root.getRight());       // 線索化右孩子
        }
    }

    /**
     * 中序遍歷線索二叉樹
     */
    public void inThreadList(Node root) {
        if (root == null) {
            return;
        }
        //查詢中序遍歷的起始節點
        while (root != null && !root.isLeftIsThread()) {
            root = root.getLeft();
        }
        while (root != null) {
            System.out.print(root.getData() + ",");
            if (root.isRightIsThread())   // 如果右孩子是線索
            {
                root = root.getRight();
            } else         // 有右孩子
            {
                root = root.getRight();
                while (root != null && !root.isLeftIsThread()) {
                    root = root.getLeft();
                }
            }
        }

    }


    /**
     * 中序遍歷
     */
    public void inList(Node root) {
        if (root != null) {
            inList(root.getLeft());
            System.out.print(root.getData() + ",");
            inList(root.getRight());
        }
    }

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }
}