Balanced Binary Search Tree

后端 未结 2 1132
情书的邮戳
情书的邮戳 2021-01-17 01:54

I need to build a balanced binary search tree. So far my program inserts the numbers from 1 to 26, but my program does not build it into a balanced binary search tree. If an

相关标签:
2条回答
  • 2021-01-17 02:29

    Since your tree does not self-balance, whether or not it's balanced will depend on the order of insertion of the elements.

    If you want your tree to be balanced regardless, you will need to take care of the balancing in your class. For example, take a look at the Red-Black Tree data structure.

    0 讨论(0)
  • 2021-01-17 02:47
    public class BinarySearchTree {
      TreeNode root;
    
      public BinarySearchTree(){
        root = new TreeNode();
      }
    
      public TreeNode getRoot(){
        return root;
      }
      public void insert(int data) {
        root = insert(root, data);
      }//Insert method checking to see where to put the nodes
    
    //  public void insert(TreeNode node, int data){
    //    TreeNode node1 = new TreeNode(data);
    //    if (root == null) { 
    //      root = node1; 
    //    } 
    //    else{
    //      TreeNode parIns = root;//Parent
    //      TreeNode insNode = root;//Insertion Node
    //      
    //      while(insNode != null){
    //        parIns = insNode;
    //        
    //        if(data < insNode.getData()){//If the data is less than the data coming in place it on the left
    //          insNode = insNode.getLeft();
    //        }else{//Place it on the right
    //          insNode = insNode.getRight();
    //        }
    //      }//Searching where to put the node
    //      
    //      if(data < parIns.getData()){
    //        parIns.setLeft(node1);
    //      }else{
    //        parIns.setRight(node1);
    //      }
    //      
    //    }
    //  }
    
      private TreeNode insert(TreeNode node, int data) { 
        if(root.data == 0)
          root.data = data;
        else if (node==null) { 
          node = new TreeNode(data); 
        } 
        else { 
          if (data <= node.data) { 
            node.leftTreeNode = insert(node.leftTreeNode, data); 
          } 
          else { 
            node.rightTreeNode = insert(node.rightTreeNode, data); 
          } 
        } 
    
        return(node); // in any case, return the new pointer to the caller 
      } 
      public void printPreOrder(){
        printPreOrder(root);
      }
      public void printPreOrder(TreeNode n){
        if(n != null){
          n.print();//N
          printPreOrder(n.getLeft());//L
          printPreOrder(n.getRight());//R
        }
      }
    
        public TreeNode balance(int[] a, int start, int end){
        TreeNode node = new TreeNode();
        if(start <= end){
          int mid = start + (end - start) /2;
          node.data = a[mid];
    
          if(root.data == 0)
            root = node;
          node.leftTreeNode = balance(a, start, mid -1);/*Make the left child if the node coming in is
           less than the mid node */
    
    
          node.rightTreeNode = balance(a, mid + 1, end);/*Make the rigth child if the node is
           greater than the mid node*/
        }
          else{
          return null;
          }
    
    
        return node;
      }
    
    
    
      public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();
        //int[] a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,21,22,23,24,25,26};
        int[] a = new int[26];
        for(int i = 0; i < 26; i++){
          a[i] = i + 1;
        }
        for(int i = 1; i <= 26; i++)
          tree.insert(i);
    
        tree.printPreOrder();
        BinarySearchTree tree2 = new BinarySearchTree();
        tree2.balance(a, 0, 25);
        System.out.println("Now I am going to balance my tree");
        tree2.printPreOrder();
    
      }
    
    }
    
    
    
    public class TreeNode {
    
      TreeNode leftTreeNode, rightTreeNode;// the nodes
      int data;
      //int size;
    
    
    
      public TreeNode(){//Constructer
        leftTreeNode = null;
        rightTreeNode = null;
        data = 0;
      }
    
      public TreeNode(int newData){//Constructer with new Data coming in for comparison
        leftTreeNode = null;
        rightTreeNode = null;
        data = newData;
      }
    
      public TreeNode getLeft(){
        return leftTreeNode;
      }
      public TreeNode getRight(){
        return rightTreeNode;
      }
    
      public void setLeft(TreeNode leftTreeNode){
        this.leftTreeNode = leftTreeNode;
      }
      public void setRight(TreeNode rightTreeNode){
        this.rightTreeNode = rightTreeNode;
      }
      public int getData(){
        return data;
      }
    
    
    //    public boolean isEmpty(){//Checking to see if the the root is empty
    //      if(size == 0) return true;
    //      else return false;
    
    
    
      public void print(){
        System.out.println("Data is: " + getData());
      }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题