2个二叉搜索树的交集

时间:2011-04-20 10:22:50

标签: java algorithm binary-search-tree

嘿,所以我想创建一个新树,它基本上是2个给定二叉搜索树的交集(交集的数学定义)。我有一个方法打印出树的特定级别的所有节点,我有一个方法可以找出树的深度。我到目前为止粘贴我的工作,虽然它是不完整的,我坚持使用logic.Help将不胜感激。

    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);
          }
}

4 个答案:

答案 0 :(得分:3)

您必须同时遍历两棵树并且同步&#34;同步&#34;。

我建议为您的班级实施Iterable接口。然后你从两棵树中得到第一个值。如果它们相等,则将它放在新树中,并从两个迭代器中获取下一个值。如果没有,请使用较小的值迭代迭代器,直到获得的值至少与另一个迭代器的最后一个值一样大。冲洗并重复。

答案 1 :(得分:0)

两棵树的交集可能是两棵树中的节点?

鉴于您必须探索树来执行此操作,为什么不进行有序遍历,存储节点然后在有序列表上执行交叉操作?

答案 2 :(得分:0)

我对这种交集的建议很简单:

给定树A和树B,找到树C = A \相交B:

1:复制树A或B.为了清楚起见,我们假设A.     这个副本现在是你的树C.现在让我们“修剪”它 2:对于c = C.root_node和b = B.root_node:
    如果b == c,
        用节点b.left,c.left
重复该过程         用节点b.right,c.right
重复该过程     否则,
        删除c(从而删除所有后续的孩子,暗示他们是不平等的)

如果这个实现可行,它将避免使用迭代器等,并归结为简单的递归遍历。 (Like this!

询问您是否希望进一步澄清。

问候。

答案 3 :(得分:0)

对于查找两个二叉搜索树的交集的递归实现,我提出了以下代码。我不太确定时间的复杂性,但它确实可行。

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);
}

}

相关问题