树——父节点表示

package com.answer.binaryTree;

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

public class Tree {
    private static class Node{
        private int parent;
        private String data;
        public Node(){}
        public Node(String data){
            this.data=data;
        }
        public Node(String data,int parent){
            this.data=data;
            this.parent=parent;
        }
    }
    private int DEFAULT_SIZE=16;//默认容器大小
    private int treeSize=0;
    private Node[] nodes;//存放节点
    private int nodeNum;//记录节点个数
    public Tree(String data){
        this.treeSize=DEFAULT_SIZE;
        nodes=new Node[treeSize];
        nodes[0]=new Node(data,-1);
        nodeNum++;
    }
    public Tree(String data,int size){
        this.treeSize=size;
        nodes=new Node[treeSize];
        nodes[0]=new Node(data,-1);
        nodeNum++;
    }
    public int pos(Node node){
        for(int i=0;i<treeSize;i++){
            if(node.equals(nodes[i])){
                return i;
            }
        }return -1;
    }
    public void add(String data,Node parent){
        for(int i=0;i<treeSize;i++){
            if(nodes[i]==null){
                nodes[i]=new Node(data,pos(parent));
                nodeNum++;
                return;
            }
        }
    }
    public Node root(){
        return nodes[0];
    }
    public List<Node> getChild(Node parent){
        ArrayList<Node> list=new ArrayList<>();
        for(int i=0;i<treeSize;i++){
            if(nodes[i]!=null&&nodes[i].parent==pos(parent)){
                list.add(nodes[i]);
            }
        }return list;
    }
    public int deep(){
        int max=0;
        for(int i=0;i<treeSize&&nodes[i]!=null;i++){
            int def=1;
            int father=nodes[i].parent;
            while(father!=-1&&nodes[father]!=null){
                father=nodes[father].parent;
                def++;
            }
            if(max<def){
                max=def;
            }
        }return max;
    }
    public boolean empty(){
        return nodeNum==0;
    }
    public Node getPar(Node node){
        return nodes[node.parent];
    }

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

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dream答案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值