What is the most efficient way to build an unsorted binary tree in java?

末鹿安然 提交于 2021-01-27 04:42:11

问题


I need to create an unsorted binary tree (one requirement is that it is unsorted) that holds a String as its value. My class outline looks like this:

public class Node {

 private String desc;
 private Node leftNode = null;
 private Node rightNode = null;

 public Node(String desc) {
  this.desc = desc;
 }

 public String getDesc() {
  return desc;
 }

 public Node getLeftNode() {
  return leftNode;
 }

 public Node getRightNode() {
  return rightNode;
 }
}

Eventually I want to be able to replace any node that matches a String description with a new node that has a new description (including duplicates with the old description).

So my question is, what is the best way to handle the insertion of Nodes when creating an unsorted binary tree?

I thought of two ways. The first would be to just have two methods, setLeftNode(Node root, String desc) and setRightNode(Node root, String desc) that someone could call with a Node of their choice as the root. If there already is a left/right Node, then it would just advance down until it hit a node that didn't have a left Node. But this could introduce problems by producing super large heights.

The second way I thought of would be to have a dedicated root Node, in this case the first Node created, and then to just build new Nodes in order.

So what is the best way to create an unsorted binary tree?


回答1:


public class BinaryTree{
    private BinaryTree right;
    private BinaryTree left;
    private String data;        

    public BinaryTree(String s){
        data = s;
        right = null;
        left = null;           
    }

    public void setLeft (BinaryTree l){ left  = l; }
    public void setRight(BinaryTree r){ right = r; }        
}

Your question suggests that the tree should be balanced and so on insertion of an element, you should recursively check number of nodes at each side of the tree:

public int checkTree(){
    if(left == null && right == null){
        return 1;
    }else if(left == null){
        return 1 + right.checkTree();
    }else if(right == null){
        return 1 + left.checkTree();
    }else{
        return 1 + left.checkTree() + right.checkTree();
    }
}

public void insert(BinaryTree bt){
    if(left == null){
        setLeft(bt);
    }else if(right == null){
        setRight(bt);
    }else{
        if(left.checkTree() <= right.checkTree()){
            left.insert(bt);
        }else{
            right.insert(bt);
        }
    }
}






EDITED:

public class BinaryTree {
    private BinaryTree right;
    private BinaryTree left;
    private String data;
    private int weight;

    public BinaryTree(String s){
        data = s;
        right = null;
        left = null; 
        weight = 1;
    }    

    public void setLeft (BinaryTree l){ 
        left  = l;
        weight++;
    }

    public void setRight(BinaryTree r){
        right = r;
        weight++;
    } 

    public int getWeight(){ return weight; }

    public void insert(BinaryTree bt){
        if(left == null){
            setLeft(bt);
        }else if(right == null){
            setRight(bt);
        }else{
            if(left.getWeight() <= right.getWeight()){
                left.insert(bt);
                weight++;
            }else{
                right.insert(bt);
                weight++;
            }
        }
    }    
}    



回答2:


by definition a binary tree has its lowest elements on the left, and the highest on the right. But if you really want that all messed up (sorted) you can call a rand function that results in 0 or 1, and if 0 then go to left, if 1 go to right, randomly. That will result in an unsorted tree




回答3:


Eventually I want to be able to replace any node that matches a String description with a new node that has a new description (including duplicates with the old description).

For that you will have to search your entire tree as:

private Node searchBasedOnValue(String desc, Node currentNode)
{  
    Node result = null
    if (currentNode == null)
        return null;
    if (currentNode.getDesc().equals(desc)) 
        return currentNode ;
    if (currentNode.getLeftNode() != null)
        result = searchBasedOnValue(desc,currentNode.getLeftNode());
    if (result == null)
        result = searchBasedOnValue(desc,currentNode.getRightNode());
    return result;
}

IMO, a regular Binary Tree is never sorted the one which is sorted is called Binary Search Tree. For insertion you need to handle the way you want it. May be you can insert nodes alternatively to left and right child of your tree so that it is balanced to some extent. That is up to you how you take care of that.

I have not seen much practical usage for regular Binary Tree as most of the times we use Binary Search Tree which have better performance (lg(n)) in terms of insertion, deletion and lookup.




回答4:


If it is unsorted, why build a binary tree at all? You can't search it without doing a full scan, so you might as well put everything in an array, as accessing any element will be O(n), due to the inability to search.




回答5:


This is the fastest way to build an unsorted binary tree without any constraints on its shape:

First you need a constructor like this:

   public Node(String desc, Node left, Node right) {
       this.desc = desc;
       this.left = left;
       this.right = right;
   }

Then build the tree like this:

   Node root = null;
   for (String input: ...) {
      root = new Node(input, root, null);
   }

Obviously, this gives you an unbalanced, unsorted tree for which searching entails looking at all of the nodes. However, if the tree is unsorted, then the fact that the tree is unbalanced makes no difference.

In general, searching an unsorted tree has the same complexity as searching a list, and the code is more complex.



来源:https://stackoverflow.com/questions/30146632/what-is-the-most-efficient-way-to-build-an-unsorted-binary-tree-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!