二叉树的相关操作
来源:互联网 发布:如何看淘宝商品的类目 编辑:程序博客网 时间:2024/04/27 13:17
package acm.result.test1;
/**
* @author BTiger
*
*/
public class Main {
/**
* 二叉树遍历,三种方法
*
* @param args
*/
public static void main(String[] args) {
Tree tree = getTree();
Node root = tree.getRoot();
// 先根遍历
System.out.println("先根遍历");
tree.rootFirst(root);
System.out.println();
// 中根遍历
System.out.println("中根遍历");
tree.middleFirst(root);
System.out.println();
// 后根遍历
System.out.println("后根遍历");
tree.rootLast(root);
System.out.println();
// 求二叉树节点个数
System.out.println("二叉树的节点个数是:" + tree.getNodeCount());
// 求二叉树高度
System.out.println("二叉树高度是:" + tree.height());
// 查找给定数值
System.out.println("C的对应的节点是:" + tree.search("C").data);
// 查找给定数值对应节点的父节点
Node s = tree.search("H");
Node parent = tree.getParent(s);
if (parent != null)
System.out.println("H的父母节点是:" + parent.data);
// 查找所有祖先节点
System.out.print("H的祖先节点分别是:");
while (parent != null) {
System.out.print(parent.data + " ");
parent = tree.getParent(parent);
}
}
/**
* 构造二叉树
*
* @return
*/
private static Tree getTree() {
Node root = new Node();
Node b = new Node();
Node c = new Node();
Node d = new Node();
Node e = new Node();
Node f = new Node();
Node g = new Node();
Node h = new Node();
root.setData("A");
b.setData("B");
c.setData("C");
d.setData("D");
e.setData("E");
f.setData("F");
g.setData("G");
h.setData("H");
root.setLeftNode(b);
root.setRightNode(c);
b.setLeftNode(d);
d.setRightNode(g);
c.setLeftNode(e);
c.setRightNode(f);
f.setLeftNode(h);
Tree tree = new Tree();
tree.setRoot(root);
return tree;
}
}
class Tree {
public Node root = null;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public boolean isEmpty() {
return this.root == null;
}
/**
* 先根遍历
*
* @param root
*/
public void rootFirst(Node root) {
System.out.print(root.getData() + " ");// 打印根节点
if (root.getLeftNode() != null) {// 打印左节点
rootFirst(root.getLeftNode());
}
if (root.getRightNode() != null) {// 打印右节点
rootFirst(root.getRightNode());
}
}
/**
* 中根遍历
*
* @param root
*/
public void middleFirst(Node root) {
if (root.getLeftNode() != null) {// 打印左节点
middleFirst(root.getLeftNode());
}
System.out.print(root.getData() + " ");// 打印根节点
if (root.getRightNode() != null) {// 打印右节点
middleFirst(root.getRightNode());
}
}
/**
* 后根遍历
*
* @param root
*/
public void rootLast(Node root) {
if (root.getLeftNode() != null) {// 打印左节点
rootLast(root.getLeftNode());
}
if (root.getRightNode() != null) {// 打印右节点
rootLast(root.getRightNode());
}
System.out.print(root.getData() + " ");// 打印根节点
}
/**
* 获取节点个数
*
* @return
*/
public int getNodeCount() {
return getNodeCount(root);
}
public int getNodeCount(Node root) {
if (root != null)
return 1 + getNodeCount(root.leftNode)
+ getNodeCount(root.rightNode);
else
return 0;
}
/**
* 求高度 从根节点的左节点找即可
*
* @param
*/
public int height() {
return height(root);
}
public int height(Node root) {
if (root != null) {
int ld = height(root.leftNode);
int rd = height(root.rightNode);
return ld > rd ? ld + 1 : rd + 1;
}
return 0;
}
/**
* 查找给定数据对应的节点
*/
public Node search(String value) {
return search(root, value);
}
public Node search(Node root, String value) {
if (root != null) {
if (value.equals(root.getData())) {
return root;
} else {
Node ls = search(root.leftNode, value);
return (ls != null) ? ls : search(root.rightNode, value);
}
}
return null;
}
/**
* 查找给定数据对应的节点的父母节点
*/
public Node getParent(Node node) {
return getParent(root, node);
}
public Node getParent(Node root, Node node) {
Node find = null;
if (root != null) {
if (root.leftNode == node || root.rightNode == node) {
find = root;
} else {
find = getParent(root.leftNode, node);
return (find != null) ? find : getParent(root.rightNode, node);
}
}
return find;
}
}
class Node {
public String data;
public Node leftNode = null, rightNode = null;
public boolean isLeaf() {
return this.leftNode == null && this.rightNode == null;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
}
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树的相关操作
- 二叉树相关操作
- 二叉树相关操作
- 二叉树相关操作
- 二叉树相关操作
- 二叉树 相关操作
- 二叉树相关操作
- Dalvik——如何控制vm
- 在Eclipse中使用JUnit4进行单元测试(高级篇)
- 成为Java高手的25个学习要点
- 让UltraEdit_17.30.0支持Verilog HDL
- AndroidManifest.xml中一些常用的属性
- 二叉树的相关操作
- Android-ListView中嵌套(ListView)控件时的问题
- 一键自动填写IP地址的方法
- 在java.awt.Image时,getWidth()和getHeight()为-1的解决方法
- 基于jQuery的表格特效(隔行、滑动、点击变色)
- Linux下查看CPU使用率!系统资源使用情况!
- 《布道之道——引领团队拥抱技术创新》
- zoj2412题解
- 20个颇具创意的移动网站设计案例