How to implement a tree data-structure in Java?

前端 未结 24 2496
鱼传尺愫
鱼传尺愫 2020-11-22 00:27

Is there any standard Java library class to represent a tree in Java?

Specifically I need to represent the following:

  • The sub-tree at any node can have
相关标签:
24条回答
  • 2020-11-22 01:03

    You can use any XML API of Java as Document and Node..as XML is a tree structure with Strings

    0 讨论(0)
  • 2020-11-22 01:05
    public class Tree {
        private List<Tree> leaves = new LinkedList<Tree>();
        private Tree parent = null;
        private String data;
    
        public Tree(String data, Tree parent) {
            this.data = data;
            this.parent = parent;
        }
    }
    

    Obviously you can add utility methods to add/remove children.

    0 讨论(0)
  • 2020-11-22 01:05

    You can use the HashTree class included in Apache JMeter that is part of the Jakarta Project.

    HashTree class is included in the package org.apache.jorphan.collections. Although this package is not released outside the JMeter project, you can get it easily:

    1) Download the JMeter sources.

    2) Create a new package.

    3) Copy on it /src/jorphan/org/apache/jorphan/collections/ . All files except Data.java

    4) Copy also /src/jorphan/org/apache/jorphan/util/JOrphanUtils.java

    5) HashTree is ready to use.

    0 讨论(0)
  • 2020-11-22 01:07

    Along the same lines as Gareth's answer, check out DefaultMutableTreeNode. It's not generic, but otherwise seems to fit the bill. Even though it's in the javax.swing package, it doesn't depend on any AWT or Swing classes. In fact, the source code actually has the comment // ISSUE: this class depends on nothing in AWT -- move to java.util?

    0 讨论(0)
  • 2020-11-22 01:07

    There is no specific data structure in Java which suits to your requirements. Your requirements are quite specific and for that you need to design your own data structure. Looking at your requirements anyone can say that you need some kind of n-ary tree with some specific functionality. You can design your data structure in following way:

    1. Structure of the node of the tree would be like content in the node and list of children like: class Node { String value; List children;}
    2. You need to retrieve the children of a given string, so you can have 2 methods 1: Node searchNode(String str), will return the node that has the same value as given input (use BFS for searching) 2: List getChildren(String str): this method will internally call the searchNode to get the node having same string and then it will create the list of all string values of children and return.
    3. You will also be required to insert a string in tree. You will have to write one method say void insert(String parent, String value): this will again search the node having value equal to parent and then you can create a Node with given value and add to the list of children to the found parent.

    I would suggest, you write structure of the node in one class like Class Node { String value; List children;} and all other methods like search, insert and getChildren in another NodeUtils class so that you can also pass the root of tree to perform operation on specific tree like: class NodeUtils{ public static Node search(Node root, String value){// perform BFS and return Node}

    0 讨论(0)
  • 2020-11-22 01:07

    Please check the below code, where I have used Tree data structures, without using Collection classes. The code may have bugs/improvements but please use this just for reference

    package com.datastructure.tree;
    
    public class BinaryTreeWithoutRecursion <T> {
    
        private TreeNode<T> root;
    
    
        public BinaryTreeWithoutRecursion (){
            root = null;
        }
    
    
        public void insert(T data){
            root =insert(root, data);
    
        }
    
        public TreeNode<T>  insert(TreeNode<T> node, T data ){
    
            TreeNode<T> newNode = new TreeNode<>();
            newNode.data = data;
            newNode.right = newNode.left = null;
    
            if(node==null){
                node = newNode;
                return node;
            }
            Queue<TreeNode<T>> queue = new Queue<TreeNode<T>>();
            queue.enque(node);
            while(!queue.isEmpty()){
    
                TreeNode<T> temp= queue.deque();
                if(temp.left!=null){
                    queue.enque(temp.left);
                }else
                {
                    temp.left = newNode;
    
                    queue =null;
                    return node;
                }
                if(temp.right!=null){
                    queue.enque(temp.right);
                }else
                {
                    temp.right = newNode;
                    queue =null;
                    return node;
                }
            }
            queue=null;
            return node; 
    
    
        }
    
        public void inOrderPrint(TreeNode<T> root){
            if(root!=null){
    
                inOrderPrint(root.left);
                System.out.println(root.data);
                inOrderPrint(root.right);
            }
    
        }
    
        public void postOrderPrint(TreeNode<T> root){
            if(root!=null){
    
                postOrderPrint(root.left);
    
                postOrderPrint(root.right);
                System.out.println(root.data);
            }
    
        }
    
        public void preOrderPrint(){
            preOrderPrint(root);
        }
    
    
        public void inOrderPrint(){
            inOrderPrint(root);
        }
    
        public void postOrderPrint(){
            inOrderPrint(root);
        }
    
    
        public void preOrderPrint(TreeNode<T> root){
            if(root!=null){
                System.out.println(root.data);
                preOrderPrint(root.left);
                preOrderPrint(root.right);
            }
    
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            BinaryTreeWithoutRecursion <Integer> ls=  new BinaryTreeWithoutRecursion <>();
            ls.insert(1);
            ls.insert(2);
            ls.insert(3);
            ls.insert(4);
            ls.insert(5);
            ls.insert(6);
            ls.insert(7);
            //ls.preOrderPrint();
            ls.inOrderPrint();
            //ls.postOrderPrint();
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题