Блог пользователя nHimanshu

Автор nHimanshu, история, 14 месяцев назад, По-английски

Here is my code in java but these is issue in my code can anybody correct it without change my logic, hey coders!!! please show your debugging skill and solve it:::

import org.w3c.dom.Node;

import  java.util.*;
// BST TREE CLASS
class  TreeNode{
    int data;
    TreeNode left,right;
    TreeNode(int data){
        this.data=data;
        this.left=null;
        this.right=null;
    }
}

//Merge 2 BST
public class Main {
   //insert AT BST method
    private static TreeNode insertATBST(TreeNode root,int x){
        if(root==null){
            root=new TreeNode(x);
            return root;
        }
        if(x>root.data){
            root.right=insertATBST(root.right,x);
        }
        else root.left=insertATBST(root.left,x);
        return root;
    }
   private static TreeNode head=null;
    private  static TreeNode pre=null;
    private static TreeNode bstToDll(TreeNode root){
if(root==null) return null;
//inorder form to use form conversion
bstToDll(root.left);
if(pre==null) head=root;
else{
    root.left=pre;
    pre.right=root;
}
pre=root;
bstToDll(root.right);
return head;
    }
    private static TreeNode mergeBST(TreeNode root1,TreeNode root2){
        TreeNode head=null;
        TreeNode tail=null;
        while(root1!=null && root2!=null){
           if(root1.data<root2.data){
               if(head==null){
                   head=root1;
                   tail=root1;
                   root1=root1.right;
               }
               else{
                   tail.right=root1;
                   root1.left=tail;
                   tail=root1;
                   root1=root1.right;
               }
           }
           else{
               if(head==null){
                   head=root2;
                   tail=root2;
                   root2=root2.right;
               }
               else{
                   tail.right=root2;
                   root2.left=tail;
                   tail=root2;
                   root1=root1.right;
               }
           }
        }
        if(root1!=null){
            tail.right=root1;
            root1.left=tail;
            root1=root1.right;
        }
        if(root2!=null){
            tail.right=root2;
            root2.left=tail;
            root2=root2.right;
        }
        return head;
    }
    private static int countTreeNodes(TreeNode root){
        TreeNode temp=root;
        int cnt=0;
        while(temp!=null){
           cnt++;
           temp=temp.right;
        }
        return  cnt;
    }

     private static TreeNode DllToBst(TreeNode root,int n){
        if(n<=0 || root==null) return null;
        TreeNode left=DllToBst(root,n/2);
        TreeNode newHead=root;
        newHead.left=left;
        root= root.right;
        newHead.right=DllToBst(root,n-n/2-1);
        return  newHead;
    }
    private static void inOrderTraversal(TreeNode root) {
        if (root == null)
            return;
        inOrderTraversal(root.left);
        System.out.println(root.data);
        inOrderTraversal(root.right);
    }
    public static void main(String[] args) {
        int[] arr1={1,2,3};
        int[] arr2={4,5,6};
        TreeNode root1=null; // nullify root object
        TreeNode root2=null;
        for (int j : arr1) {
            root1 = insertATBST(root1, j);
        }
        for (int j : arr2) {
            root2 = insertATBST(root2, j);
        }

        root1=bstToDll(root1);
        head=null; pre=null;
        root2=bstToDll(root2);
        TreeNode root=null;
      root=  mergeBST(root1,root2);
        int cnt=countTreeNodes(root);
//        while(root!=null) {
//            System.out.println(root.data);
//            root=root.right;
//        }
        root=DllToBst(root,cnt);



    }
}
  • Проголосовать: нравится
  • -4
  • Проголосовать: не нравится

»
14 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

If I am not mistaken, it must be that 1 tree has all of the values greater than all of the values of the other. Otherwise, you have to reconstruct the tree on all values, or just insert elements from one to another 1 by 1. (If you do small to large merging it will still be quite fast)