-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 add TreeNode construce, toString, travel method
- Loading branch information
XiaTianliang
committed
Oct 22, 2016
1 parent
f31fbae
commit 9606d81
Showing
10 changed files
with
709 additions
and
168 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
package binarytree; | ||
|
||
import sun.reflect.generics.tree.Tree; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
import java.util.Stack; | ||
|
||
/** | ||
* Created by tianliangxia on 16-10-22. | ||
*/ | ||
|
||
enum Direction{ | ||
left,right | ||
} | ||
|
||
class Element{ | ||
public TreeNode node; | ||
public Direction direction; | ||
public Element(TreeNode node, Direction direction){ | ||
this.node = node; | ||
this.direction = direction; | ||
} | ||
} | ||
|
||
public class TreeNode { | ||
public int val; | ||
public TreeNode left=null, right=null; | ||
public TreeNode(int val){ | ||
this.val=val; | ||
} | ||
|
||
/*** | ||
* @param str [1,2,#] #->null | ||
* @return | ||
*/ | ||
public static TreeNode construct(String str){ | ||
//构建树 | ||
String[] values; | ||
if(str.charAt(0)=='[' && str.charAt(str.length()-1)==']'){ | ||
values=str.substring(1,str.length()-1).split(","); | ||
}else{ | ||
values=str.split(","); | ||
} | ||
//System.out.println(values); | ||
if(values.length<3){ | ||
return null; | ||
} | ||
TreeNode root = new TreeNode(Integer.valueOf(values[0])); | ||
Queue<TreeNode> qu = new LinkedList<>(); | ||
qu.add(root); | ||
TreeNode node; | ||
int count=0; | ||
while (!qu.isEmpty()){ | ||
for(int i=0,j=qu.size();i<j;i++){ | ||
node=qu.remove(); | ||
if(!values[count+1].equals("#")){ | ||
node.left = new TreeNode(Integer.valueOf(values[count+1])); | ||
qu.add(node.left); | ||
} | ||
if(!values[count+2].equals("#")){ | ||
node.right = new TreeNode(Integer.valueOf(values[count+2])); | ||
qu.add(node.right); | ||
} | ||
count+=2; | ||
} | ||
} | ||
return root; | ||
} | ||
|
||
public static String toString(TreeNode root){ | ||
//宽搜 | ||
//用于比较结构是否 | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append('['); | ||
Queue<TreeNode> qu = new LinkedList<>(); | ||
qu.add(root); | ||
TreeNode node; | ||
while (!qu.isEmpty()){ | ||
for(int i=0,j=qu.size();i<j;i++){ | ||
node=qu.remove(); | ||
if(node == null){ | ||
stringBuilder.append("#,"); | ||
}else { | ||
stringBuilder.append(node.val+","); | ||
qu.add(node.left); | ||
qu.add(node.right); | ||
} | ||
} | ||
} | ||
stringBuilder.deleteCharAt(stringBuilder.length()-1); | ||
stringBuilder.append(']'); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
public static String preOrderTravel(TreeNode root){ | ||
if(root == null) | ||
return null; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
Stack<TreeNode> stack = new Stack<>(); | ||
stack.add(null); | ||
while (root != null){ | ||
stringBuilder.append(root.val+","); | ||
if(root.right != null){ | ||
stack.push(root.right); | ||
} | ||
if(root.left != null){ | ||
root = root.left; | ||
}else { | ||
root=stack.pop(); | ||
} | ||
} | ||
stringBuilder.deleteCharAt(stringBuilder.length()-1); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
public static String preOrderTravel2(TreeNode root){ | ||
if(root==null) | ||
return null; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
Stack<TreeNode> stack = new Stack<>(); | ||
while (root!= null || !stack.isEmpty()){ | ||
if(root != null){ | ||
stringBuilder.append(root.val+","); | ||
stack.push(root); | ||
root = root.left; | ||
}else { | ||
root=stack.pop(); | ||
root=root.right; | ||
} | ||
} | ||
stringBuilder.deleteCharAt(stringBuilder.length()-1); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
public static String inOrderTravel(TreeNode root){ | ||
if(root==null) | ||
return null; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
Stack<TreeNode> stack = new Stack<>(); | ||
while (root!= null || !stack.isEmpty()){ | ||
if(root != null){ | ||
stack.push(root); | ||
root = root.left; | ||
}else { | ||
root=stack.pop(); | ||
stringBuilder.append(root.val+","); | ||
root=root.right; | ||
} | ||
} | ||
stringBuilder.deleteCharAt(stringBuilder.length()-1); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
public static String inOrderTravel2(TreeNode root){ | ||
if(root==null) | ||
return null; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
Stack<TreeNode> stack = new Stack<>(); | ||
while (root!= null || !stack.isEmpty()){ | ||
while (root!=null){ | ||
stack.push(root); | ||
root = root.left; | ||
} | ||
root=stack.pop(); | ||
stringBuilder.append(root.val+","); | ||
root=root.right; | ||
|
||
} | ||
stringBuilder.deleteCharAt(stringBuilder.length()-1); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
public static String postOrderTravel(TreeNode root){ | ||
if(root==null) | ||
return null; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
Stack<Element> stack = new Stack<>(); | ||
Element element; | ||
while (root!= null || !stack.isEmpty()){ | ||
while (root!=null){ | ||
stack.push(new Element(root, Direction.left)); | ||
root = root.left; | ||
} | ||
element=stack.pop(); | ||
|
||
if(element.direction.equals(Direction.left)){ | ||
element.direction = Direction.right; | ||
stack.push(element); | ||
root= element.node.right; | ||
}else{ | ||
stringBuilder.append(element.node.val+","); | ||
root=null; | ||
} | ||
} | ||
stringBuilder.deleteCharAt(stringBuilder.length()-1); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
public static void reverse(TreeNode root){ | ||
if(root!=null){ | ||
reverse(root.left); | ||
reverse(root.right); | ||
TreeNode tmp = root.left; | ||
root.left = root.right; | ||
root.right = tmp; | ||
} | ||
} | ||
|
||
/*** | ||
* 判断pattern是否是root树结构中的一部分 网易游戏藏宝图 | ||
* @param root | ||
* @param pattern | ||
* @return | ||
*/ | ||
public static boolean match(TreeNode root, TreeNode pattern){ | ||
if(root == null && pattern == null) | ||
return true; | ||
if(root == null) | ||
return false; | ||
if(pattern == null) | ||
return true; | ||
|
||
return (root.val==pattern.val && (match(root.left, pattern.left) && match(root.right, pattern.right) | ||
|| match(root.right, pattern.left) && match(root.left, pattern.right))) | ||
|| match(root.left, pattern) || match(root.right, pattern); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package utils; | ||
|
||
/** | ||
* Created by tianliangxia on 16-10-22. | ||
*/ | ||
public class StringUtils { | ||
public static boolean isEmpty(String str){ | ||
return str==null || str.length()==0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package binarytree; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.transaction.TransactionRequiredException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by tianliangxia on 16-10-22. | ||
*/ | ||
public class TreeNodeTest { | ||
private TreeNode treeNode; | ||
|
||
@Before | ||
public void before(){ | ||
treeNode = new TreeNode(0); | ||
} | ||
|
||
@Test | ||
public void toStringTest(){ | ||
TreeNode root = new TreeNode(0); | ||
String res = TreeNode.toString(root); | ||
Assert.assertEquals(res,"[0,#,#]"); | ||
root.left = new TreeNode(1); | ||
res= TreeNode.toString(root); | ||
Assert.assertEquals(res,"[0,1,#,#,#]"); | ||
|
||
} | ||
|
||
@Test | ||
public void constructTest(){ | ||
String str="[0,#,#]"; | ||
TreeNode root = TreeNode.construct(str); | ||
String res = TreeNode.toString(root); | ||
assertEquals(str,res); | ||
|
||
str="[1,2,3,4,#,#,6,#,#,#,#]"; | ||
root = TreeNode.construct(str); | ||
res = TreeNode.toString(root); | ||
assertEquals(str, res); | ||
} | ||
|
||
@Test | ||
public void preOrderTravelTest(){ | ||
TreeNode root = TreeNode.construct("[1,2,3,4,#,#,6,#,#,#,#]"); | ||
assertEquals(TreeNode.preOrderTravel(root), "1,2,4,3,6"); | ||
|
||
root = TreeNode.construct("[1,2,3,8,4,5,#,#,#,6,#,9,7,#,#,#,#,#,#]"); | ||
assertEquals(TreeNode.preOrderTravel(root), "1,2,8,4,6,3,5,9,7"); | ||
|
||
} | ||
|
||
@Test | ||
public void preOrderTravelTest2(){ | ||
TreeNode root = TreeNode.construct("[1,2,3,4,#,#,6,#,#,#,#]"); | ||
assertEquals(TreeNode.preOrderTravel2(root), "1,2,4,3,6"); | ||
|
||
root = TreeNode.construct("[1,2,3,8,4,5,#,#,#,6,#,9,7,#,#,#,#,#,#]"); | ||
assertEquals(TreeNode.preOrderTravel2(root), "1,2,8,4,6,3,5,9,7"); | ||
|
||
} | ||
|
||
@Test | ||
public void inOrderTravelTest(){ | ||
TreeNode root = TreeNode.construct("[1,2,3,4,#,#,5,#,#,#,#]"); | ||
assertEquals(TreeNode.inOrderTravel(root), "4,2,1,3,5"); | ||
|
||
root = TreeNode.construct("[1,2,3,8,4,5,#,#,#,6,#,9,7,#,#,#,#,#,#]"); | ||
assertEquals(TreeNode.inOrderTravel(root), "8,2,6,4,1,9,5,7,3"); | ||
|
||
} | ||
|
||
@Test | ||
public void inOrderTravelTest2(){ | ||
TreeNode root = TreeNode.construct("[1,2,3,4,#,#,5,#,#,#,#]"); | ||
assertEquals(TreeNode.inOrderTravel2(root), "4,2,1,3,5"); | ||
|
||
root = TreeNode.construct("[1,2,3,8,4,5,#,#,#,6,#,9,7,#,#,#,#,#,#]"); | ||
assertEquals(TreeNode.inOrderTravel2(root), "8,2,6,4,1,9,5,7,3"); | ||
} | ||
|
||
@Test | ||
public void PostOrderTravelTest(){ | ||
TreeNode root = TreeNode.construct("[1,2,3,4,#,#,5,#,#,#,#]"); | ||
assertEquals(TreeNode.postOrderTravel(root), "4,2,5,3,1"); | ||
|
||
root = TreeNode.construct("[1,2,3,8,4,5,#,#,#,6,#,9,7,#,#,#,#,#,#]"); | ||
assertEquals(TreeNode.postOrderTravel(root), "8,6,4,2,9,7,5,3,1"); | ||
} | ||
|
||
@Test | ||
public void reverseTest(){ | ||
TreeNode root = TreeNode.construct("[1,2,3,8,4,5,#,#,#,6,#,9,7,#,#,#,#,#,#]"); | ||
TreeNode.reverse(root); | ||
assertEquals(TreeNode.toString(root), "[1,3,2,#,5,4,8,7,9,#,6,#,#,#,#,#,#,#,#]"); | ||
} | ||
|
||
@Test | ||
public void matchTest(){ | ||
TreeNode root = TreeNode.construct("[1,2,3,8,4,5,#,#,#,6,#,9,7,#,#,#,#,#,#]"); | ||
TreeNode pattern = TreeNode.construct("[2,8,4,#,#,6,#,#,#]"); | ||
assertEquals(true, TreeNode.match(root, pattern)); | ||
|
||
pattern = TreeNode.construct("[2,#,4,6,#,#,#]"); | ||
assertEquals(true, TreeNode.match(root, pattern)); | ||
} | ||
|
||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.