How to implement a tree data-structure in Java?

前端 未结 24 2510
鱼传尺愫
鱼传尺愫 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: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  {
    
        private TreeNode root;
    
    
        public BinaryTreeWithoutRecursion (){
            root = null;
        }
    
    
        public void insert(T data){
            root =insert(root, data);
    
        }
    
        public TreeNode  insert(TreeNode node, T data ){
    
            TreeNode newNode = new TreeNode<>();
            newNode.data = data;
            newNode.right = newNode.left = null;
    
            if(node==null){
                node = newNode;
                return node;
            }
            Queue> queue = new Queue>();
            queue.enque(node);
            while(!queue.isEmpty()){
    
                TreeNode 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 root){
            if(root!=null){
    
                inOrderPrint(root.left);
                System.out.println(root.data);
                inOrderPrint(root.right);
            }
    
        }
    
        public void postOrderPrint(TreeNode 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 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  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();
    
        }
    
    }
    

提交回复
热议问题