树-链表

98 阅读1分钟

树——子节点链表示

\

父节点表示法是让每个节点记住它的父节点的索引,子节点链是让父节点记住它的所有子节点。

package com.answer.binaryTree;

        import java.util.ArrayList;
        import java.util.List;

public class TreeChild {
    private static class SonNode{
        private int pos;
        private SonNode next;
        public SonNode(int pos,SonNode next){
            this.pos=pos;
            this.next=next;
        }
    }
    private static class Node{
        private String data;
        private SonNode first;
        public Node(String data){
            this.data=data;
            this.first=null;
        }
    }
    private int DEFAULT_SIZE=16;
    private int treeSize=0;
    private Node[] nodes;
    private int nodeNum;
    public TreeChild(String data){
        this.treeSize=DEFAULT_SIZE;
        nodes=new Node[treeSize];
        nodes[0]=new Node(data);
        nodeNum++;
    }
    public TreeChild(String data,int size){
        this.treeSize=size;
        nodes=new Node[treeSize];
        nodes[0]=new Node(data);
        nodeNum++;
    }
    public void add(String data,Node parent){//向指定节点添加子节点
        for(int i=0;i<treeSize;i++){
            if(nodes[i]==null){
                nodes[i]=new Node(data);
                //添加到子节点链中
                if(parent.first==null){
                    parent.first=new SonNode(i,null);
                }else {
                    SonNode sonNode=parent.first;
                    while(sonNode.next!=null){
                        sonNode=sonNode.next;
                    }
                    sonNode.next=new SonNode(i,null);
                }nodeNum++;
                return;
            }
        }
    }
    public boolean empty(){
        return nodes[0]==null;
    }
    public Node root(){
        return nodes[0];
    }
    public List<Node> getChild(Node parent){
        List<Node> list=new ArrayList<>();
        SonNode fir=parent.first;
        while(fir!=null){
            list.add(nodes[fir.pos]);
            fir=fir.next;
        }return list;
    }
    public Node child(Node parent,int index){//返回指定父节点的子节点
        SonNode fir=parent.first;
        for(int i=0;fir!=null;i++){
            if(i==index){
                return nodes[fir.pos];
            }
            fir=fir.next;
        }return null;

    }
    public int deep(){
        return deep(root());
    }

    private int deep(Node node) {
        if(node.first==null){
            return 1;
        }else {
            int max=0;
            SonNode fir=node.first;
            while(fir!=null){
                int temp=deep(nodes[fir.pos]);
                if(temp>max){
                    max=temp;
                }
                fir=fir.next;
            }return max+1;
        }
    }
    public int pos(Node node){
        for(int i=0;i<treeSize;i++){
            if(nodes[i]==node){
                return i;
            }
        }return -1;
    }

    public static void main(String[] args) {
        TreeChild tree=new TreeChild("Root");
        Node root=tree.root();
        tree.add("A",root);
        tree.add("B",root);
        List<Node> list=tree.getChild(root);
        for(Node node:list){
            System.out.println(node.data);
        }
        tree.add("C",list.get(0));
        System.out.println(tree.child(list.get(0),0).data);
        System.out.println(tree.deep());
    }

}

\

三叉链表存储——二叉树(Java)

 每个节点记住其左右子节点以及父节点;

package com.answer.binaryTree;

public class ThreeLinkBinTree {
    private static class Node{
        private String data;
        private Node left;
        private Node right;
        private Node parent;
        public Node(){};
        public Node(String data){
            this.data=data;
        }
        public Node(String data,Node parent,Node left,Node right){
            this.data=data;
            this.left=left;
            this.right=right;
        }
    }
    private Node root;
    public ThreeLinkBinTree(){
        this.root=new Node();
    }
    public ThreeLinkBinTree(String data){
        this.root=new Node(data);
    }
    public Node add(Node parent,String data,boolean left){
        if(parent==null){
            throw new RuntimeException("父节点为空");
        }
        if(left&&parent.left!=null){
            throw new RuntimeException("已有左节点");
        }
        if(!left&&parent.right!=null){
            throw new RuntimeException("已有右节点");
        }
        Node node=new Node(data);
        if(left){
            parent.left=node;
        }else {
            parent.right=node;
        }
        node.parent=parent;
        return node;
    }
    public boolean empty(){
        return root==null;
    }
    public Node getRoot(){
        if(root==null){
            throw new RuntimeException("没有根节点");
        }
        return root;
    }
    public Node getPar(Node node){
        if(node==null){
            throw new RuntimeException("此节点为空");
        }return node.parent;
    }
    public Node getLeft(Node parent){
        if(parent==null){
            throw new RuntimeException("父节点为空");
        }
        return parent.left==null?null:parent.left;
    }
    public Node getRight(Node parent){
        if(parent==null){
            throw new RuntimeException("父节点为空");
        }
        return parent.right==null?null:parent.right;
    }
    public int deep(){
        return deep(this.root);
    }

    private int deep(Node node) {
        if(node==null){
            return 0;
        }
        if(node.left==null&&node.right==null){
            return 1;
        }else {
            int leftDeep=deep(node.left);
            int rightDeep=deep(node.right);
            int max=leftDeep>rightDeep?leftDeep:rightDeep;
            return max+1;
        }
    }

    public static void main(String[] args) {
        ThreeLinkBinTree tree=new ThreeLinkBinTree("Root");
        Node root=tree.getRoot();
        Node node1=tree.add(root,"A",true);
        Node node2=tree.add(root,"B",false);
        System.out.println(node1.parent.data);
        System.out.println(tree.deep());
    }
}

\