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 }
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
- fwnx - 4.7 tree contains ; recursive
- FWNX- build a binary search tree using array - java version <<recursive>>
- fwnx-same tree - leetcodde-
- recursive tree
- FWNX - find the same father - TREE
- Recursive tree是神马
- Inorder - Tree - traverse - recursive and not recursive
- Inorder Tree Traversal | Iterative & Recursive
- Preorder Tree Traversal | Iterative & Recursive
- Postorder Tree Traversal | Iterative & Recursive
- fwnx- find the common ancestor in a tree
- Binary Tree Preorder Traversal (Recursive solution)
- recursive
- recursive
- recursive
- FWNX- build a full binary tree via an arrary- java version
- Recursive backtrack & Non-recursive backtrack & Subset Tree & Permutation Tree 递归回溯与非递归回溯 排列树与子集树
- Can't locate File/Copy/Recursive.pm in @INC (@INC contains:
- [LeetCode] Candy
- Linux c编程一站式学习笔记(02), Return语句
- Ruby命令行参数介绍
- [LeetCode] Unique Binary Search Trees
- DP34 流水线调度问题 Assembly Line Scheduling @geeksforgeeks
- fwnx - 4.7 tree contains ; recursive
- Ch1-5: Write a method to replace all spaces in a string with ‘%20’.
- fwnx- find the common ancestor in a tree
- [LeetCode] Spiral Matrix
- cocos2d-x游戏开发系列教程-中国象棋06-游戏规则
- [LeetCode] Minimum Path Sum
- IRP IO_STACK_LOCATION 《寒江独钓》内核学习笔记(1)
- DP35 最长等差数列 Longest Arithmetic Progression @geeksforgeeks
- 从网络获取图片方法