Skip to content

Commit 02de769

Browse files
committed
添加树的一些算法
1 parent c115f0c commit 02de769

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

src/main/java/cn/byhieg/algorithmtutorial/BinaryTree.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ public static int getNodes(Node root) {
162162
return getNodes(root.left) + getNodes(root.right) + 1;
163163
}
164164

165+
/**
166+
* 得到树的叶子节点的数目
167+
* @param root
168+
* @return
169+
*/
165170
public static int getLeafs(Node root) {
166171
if (root == null) {
167172
return 0;
@@ -173,6 +178,70 @@ public static int getLeafs(Node root) {
173178

174179
}
175180

181+
182+
/**
183+
* 计算树的深度
184+
* @param root
185+
* @return
186+
*/
187+
public static int getHeight(Node root){
188+
if (root == null) {
189+
return 0;
190+
}
191+
int leftHeight = getHeight(root.left) + 1;
192+
int rightHeight = getHeight(root.right) + 1;
193+
return leftHeight > rightHeight ? leftHeight : rightHeight;
194+
}
195+
196+
/**
197+
* 计算第K层的节点数
198+
* @param root
199+
* @param k
200+
* @return
201+
*/
202+
public static int calcKNodes(Node root, int k) {
203+
if (root == null || k < 0) {
204+
return 0;
205+
}
206+
if (k == 0){
207+
return 1;
208+
}
209+
return calcKNodes(root.left, k - 1) + calcKNodes(root.right, k - 1);
210+
211+
}
212+
213+
/**
214+
* 判断两个树的结构是否相同
215+
* @param root1
216+
* @param root2
217+
* @return
218+
*/
219+
public static boolean isCommon(Node root1, Node root2) {
220+
if (root1 == null && root2 == null) {
221+
return true;
222+
} else if (root1 == null || root2 == null) {
223+
return false;
224+
}else{
225+
boolean isLeftCommon = isCommon(root1.left, root2.left);
226+
boolean isRightCommon = isCommon(root1.right, root2.right);
227+
return isLeftCommon && isRightCommon;
228+
}
229+
}
230+
231+
/**
232+
* 得到树的镜像,即对于每一个节点,交换他们的左右孩子节点。
233+
* @param root
234+
*/
235+
public static void mirror(Node root) {
236+
if (root != null) {
237+
Node tmp = root.left;
238+
root.left = root.right;
239+
root.right = tmp;
240+
mirror(root.left);
241+
mirror(root.right);
242+
}
243+
}
244+
176245
public static class Node {
177246
public int data;
178247
public Node left;

src/test/java/cn/byhieg/algorithmtutorialtest/BinaryTreeTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.byhieg.algorithmtutorialtest;
22

33
import cn.byhieg.algorithmtutorial.BinaryTree;
4-
import cn.byhieg.iotutorial.bytestreamio.BufferdInputStreamExample;
54
import junit.framework.TestCase;
65

76
/**
@@ -10,7 +9,6 @@
109
*/
1110
public class BinaryTreeTest extends TestCase {
1211

13-
1412
BinaryTree.Node root = new BinaryTree.Node(1);
1513
public void setUp() throws Exception {
1614
super.setUp();
@@ -69,11 +67,22 @@ public void testLevelOrder() throws Exception {
6967
}
7068

7169
public void testGetNodes() throws Exception {
72-
System.out.println("节点数" + BinaryTree.getNodes(root));
70+
System.out.print("节点数" + BinaryTree.getNodes(root));
7371
}
7472

7573
public void testGetLeafs() throws Exception {
76-
System.out.println("叶子数" + BinaryTree.getLeafs(root));
74+
System.out.print("叶子数" + BinaryTree.getLeafs(root));
75+
}
76+
77+
public void testGetHeight() throws Exception {
78+
System.out.print("树的高度" + BinaryTree.getHeight(root));
7779
}
7880

81+
public void testCalcKNodes() throws Exception {
82+
System.out.print("第2层的节点数" + BinaryTree.calcKNodes(root,2));
83+
}
84+
85+
public void testMirror() throws Exception {
86+
BinaryTree.mirror(root);
87+
}
7988
}

0 commit comments

Comments
 (0)