1.二叉树的类
一些基本方法:求高度、求节点个数、前序遍历、后序遍历、中序遍历
package binaryTree;
/**
* @author MING 二叉树的实现类
*/
public class BinaryTree {
private TreeNode rootNode = null;
public BinaryTree() {
// 根节点的key从1开始
rootNode = new TreeNode(1, "rootNode(A)");
}
/**
* 创建一棵二叉树
*
* <pre>
* A
* B C
* D E F
* </pre>
*
* @param rootNode
*/
public void reateBinTree(TreeNode rootNode) {
TreeNode treeNodeB = new TreeNode(2, "B");
TreeNode treeNodeC = new TreeNode(3, "C");
TreeNode treeNodeD = new TreeNode(4, "D");
TreeNode treeNodeE = new TreeNode(5, "E");
TreeNode treeNodeF = new TreeNode(6, "F");
rootNode.leftChild = treeNodeB;
rootNode.rightChild = treeNodeC;
rootNode.leftChild.leftChild = treeNodeD;
rootNode.leftChild.rightChild = treeNodeE;
rootNode.rightChild.rightChild = treeNodeF;
}
// 返回数的高度
public int height(TreeNode treeNode) {
if (treeNode == null) {
return 0;
} else {
int i = height(treeNode.leftChild);
int j = height(treeNode.rightChild);
i++;
j++;
return (i > j) ? i : j;
}
}
// 返回节点的个数
public int size(TreeNode treeNode) {
if (treeNode == null) {
return 0;
} else {
return 1 + size(treeNode.leftChild) + size(treeNode.rightChild);
}
}
/**
* 求二叉树中任一节点的双亲
*
* @param root
* @param element
* //第一次调用的时候,传入根节点和此任一节点
* @return
*/
public TreeNode parent(TreeNode root, TreeNode element) {
if (root == null || root == element) {
return null;
} else {
if (root.leftChild == element || root.rightChild == element) {
return root;
} else {
TreeNode p = parent(root.leftChild, element);
TreeNode q = parent(root.rightChild, element);
if (p == null && q == null)
return null;
else
return (p != null) ? p : q;
}
}
}
public void visted(TreeNode subNode){
subNode.isVisted= true;
System.out.println("key:"+subNode.getKey()+" name:"+subNode.getData());
}
// 前序遍历
public void preOrder(TreeNode subNode){
if(subNode!=null){
visted(subNode);
preOrder(subNode.leftChild);
preOrder(subNode.rightChild);
}
}
// 后序遍历
public void postOrder(TreeNode subNode){
if(subNode!=null){
postOrder(subNode.leftChild);
postOrder(subNode.rightChild);
visted(subNode);
}
}
// 中序遍历
public void inOrder(TreeNode subNode){
if(subNode!=null){
inOrder(subNode.leftChild);
visted(subNode);
inOrder(subNode.rightChild);
}
}
/**
* 测试
*/
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.reateBinTree(bt.rootNode);
// 测试高度
int height = bt.height(bt.rootNode);
System.out.println("二叉树的高度是" + height);
// 测试节点个数
int size = bt.size(bt.rootNode);
System.out.println("二叉树的节点数是" + size);
// 测试双亲节点
TreeNode e = bt.rootNode.leftChild.rightChild;
TreeNode eParent = bt.parent(bt.rootNode, e);
System.out.println(eParent.getData());
//测试前序遍历
bt.preOrder(bt.rootNode);
}
}
2.二叉树的节点的类
package binaryTree;
/**
* @author MING 二叉树的节点类
*/
public class TreeNode {
private int key = 0; // 节点在二叉树中的索引(位置),从0开始。
private String data = null; // 该节点对象存储的数据
public TreeNode leftChild = null;
public TreeNode rightChild = null;
public boolean isVisted = false;//节点的一个标记,默认未被访问过
public TreeNode() {
}
public TreeNode(int key, String data) {
this.key = key;
this.data = data;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}