Intersection of 2 binary search trees

依然范特西╮ 提交于 2019-12-13 19:22:51

问题


Hey, So I want to create a new tree which is basically the intersection (mathematical definition of intersection) of 2 given binary search trees. I have a method that prints out all the nodes at a particular level of the tree and I have a method that can find out the depth of the tree.I am pasting my work so far though it is incomplete and I'm stuck with the logic.Help will be appreciated.

    public static Bst<Customer> intersect (Bst<Customer> a, Bst<Customer> b){
    Bst<Customer> result = new Bst<Customer>();
    BTNode<Customer> cur1;
    BTNode<Customer> cur2;
    BTNode<Customer> cur3;
    cur1=a.root;
    cur2=b.root;
    cur3=result.root;
    int Resultdepth;
    if(a.maxDepth()<b.maxDepth())
        Resultdepth=a.maxDepth();
    else
        Resultdepth=b.maxDepth();

    if(cur1==null || cur2==null){ // Handeling the root case intially
        result = null;
    }
    else 
      cur3.item.set_account_id(cur1.item.get_accountid()+ cur2.item.get_accountid());

    cur1=cur1.left;
    cur2=cur2.left;
    cur3=cur3.left;       

    while(<some check>){

    }


    return result;

}


    public int maxDepth(){
        return mD(root);
    }

    int mD(BTNode<E> node){
       if (node==null) {
            return(0);
        }
       else {
            int lDepth = mD(node.left);
            int rDepth = mD(node.right);
            // use the larger + 1
            return(Math.max(lDepth, rDepth) + 1);
        }
    }

     // for printing the nodes at a particular level and giving the starting level
      public void PrintAt(BTNode<E> cur, int level, int desiredLevel) {
         if (cur == null) {
            return;
        }
         if (level == desiredLevel) {
             System.out.print(cur.item.toString() + "");
          }
         else {
             PrintAt(cur.left, level+1, desiredLevel);
             PrintAt(cur.right, level+1, desiredLevel);
          }
}

回答1:


You have to traversal both trees in order at the same time and "in sync".

I'd suggest to implement the Iterable interface for your class. Then you get the first values from both trees. If they are equal, put it in the new tree, and get the next values from both iterators. If not, iterate the iterator with the smaller values until the value you get is as least as big as the last value from the other iterator. Rinse and repeat.




回答2:


The intersection of two trees is presumably the nodes that are in both trees?

Given that you'll have to explore the tree to do this, why not just do an in-order traversal, store the nodes and then do an intersection operation on ordered lists?




回答3:


My suggestion for such an intersection is simple:

Given tree A and tree B, to find tree C = A \intersect B:

1: Copy either tree A or B. Let us assume A for clarity.
This copy is now your tree C. Now let's 'trim' it.
2: For c = C.root_node and b = B.root_node:
if b==c,
Repeat the procedure with nodes b.left, c.left
Repeat the procedure with nodes b.right, c.right
else,
Remove c (thereby removing all subsequent children, it is implied they are unequal)

If this implementation would work, it would avoid the use of iterators and the like, and boil down to a simple recursive traversal. (Like this!)

Ask if you would like further clarification.

Regards.




回答4:


For the recursive implementation of finding intersection of two binary search trees , I came up with the following code. I am not very sure of the time complexity, but it does work all right.

void BST::findIntersection(cell *root1, cell * root2) {

if(root1 == NULL ) { 
//  cout<<"Tree 1 node is null , returning"<<endl;  
    return;
}
if(root2 == NULL) {
//  cout<<"Tree 2 node is null , returning"<<endl;  
    return;
}
//cout<<"Comparing tree 1 : "<<root1->data<< "   and tree 2 : " << root2->data<<endl;
if(root1->data==root2->data) {
//  cout<<"tree 1 equal to tree 2 "<<endl;
    insert(root1->data);
//  cout<<"Inserting in new tree : "<<root1->data<<endl;
    findIntersection(root1->left,root2->left);
    findIntersection(root1->right, root2->right);
}
else if(root1->data>root2->data) {
//  cout<<"tree 1 > tree 2 "<<endl;
    findIntersection(root1,root2->right);
    findIntersection(root1->left, root2);
}
else  {
//  cout<<"tree 1 < tree 2 "<<endl;
    findIntersection(root1->right,root2);
    findIntersection(root1, root2->left);
}

}



来源:https://stackoverflow.com/questions/5728821/intersection-of-2-binary-search-trees

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