fwnx - 4.7 tree contains ; recursive

来源:互联网 发布:php substr函数 编辑:程序博客网 时间:2024/05/17 00:55
4.7 You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1.
pg 54
SOLUTION
Note that the problem here specifies that T1 has millions of nodes—this means that we should be careful of how much space we use. Let’s say, for example, T1 has 10 million nodes—this means that the data alone is about 40 mb. We could create a string representing the inorder and preorder traversals. If T2’s preorder traversal is a substring of T1’s preorder traversal, and T2’s inorder traversal is a substring of T1’s inorder traversal, then T2 is a substring of T1. We can check this using a suffix tree. However, we may hit memory limitations because suffix trees are extremely memory intensive. If this become an issue, we can use an alternative approach.
Alternative Approach: The treeMatch procedure visits each node in the small tree at most once and is called no more than once per node of the large tree. Worst case runtime is at most O(n * m), where n and m are the sizes of trees T1 and T2, respectively. If k is the number of occurrences of T2’s root in T1, the worst case runtime can be characterized as O(n + k * m).
1 boolean containsTree(TreeNode t1, TreeNode t2) {
2 if (t2 == null) return true; // The empty tree is always a subtree
3 else return subTree(t1, t2);
4 }
5
6 boolean subTree(TreeNode r1, TreeNode r2) {
7 if (r1 == null)
8 return false; // big tree empty & subtree still not found.
9 if (r1.data == r2.data) {
10 if (matchTree(r1,r2)) return true;
11 }
12 return (subTree(r1.left, r2) || subTree(r1.right, r2));
13 }
14
15 boolean matchTree(TreeNode r1, TreeNode r2) {
16 if (r2 == null && r1 == null)
17 return true; // nothing left in the subtree
18 if (r1 == null || r2 == null)
19 return false; // big tree empty & subtree still not found
20 if (r1.data != r2.data)
21 return false; // data doesn’t match
22 return (matchTree(r1.left, r2.left) &&
23 matchTree(r1.right, r2.right));
24 }

25 }


boolean isContains(Node big_root,Node smal_root){if(smal_root==null){return true;}elsereturn isSubtree(big_root,smal_root);}boolean isSubtree(Node big_root,Node smal_root){if(big_root==null){return false;}elseif(big_root.data == smal_root.data){return ismatch(big_root,smal_root);}return isSubtree(big_root.right_child,smal_root) || isSubtree(big_root.left_child,smal_root);}boolean ismatch(Node t1,Node t2){if(t1==null && t2 ==null){return true;}else{if(t1==null || t2 ==null){return false;}}if(t1.data != t2.data){return false;}return ismatch(t1.left_child,t2.left_child) && ismatch(t1.right_child,t2.right_child);}


0 0