ツリー——親ノード表示

1704 ワード

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 getChild(Node parent){
        ArrayList list=new ArrayList<>();
        for(int i=0;i list=tree.getChild(root);
        for(Node n:list){
            System.out.println(n.data);
        }
        System.out.println(tree.getPar(list.get(0)).data);

    }
}