-
package com.wp;
-
-
//import java.util.*;
-
-
public class BinaryTreeNode {
-
-
int m_nValue;
-
BinaryTreeNode m_pLeft;
-
BinaryTreeNode m_pRight;
-
-
-
public static BinaryTreeNode CreateBinaryTreeNode(int value)
-
{
-
BinaryTreeNode pNode = new BinaryTreeNode();
-
pNode.m_nValue = value;
-
pNode.m_pLeft = null;
-
pNode.m_pRight = null;
-
-
return pNode;
-
}
-
-
public static void ConnectTreeNodes(BinaryTreeNode pParent, BinaryTreeNode pLeft, BinaryTreeNode pRight)
-
{
-
if(pParent != null)
-
{
-
pParent.m_pLeft = pLeft;
-
pParent.m_pRight = pRight;
-
}
-
}
-
-
-
//先序遍历二叉树
-
public static void InorderTree(BinaryTreeNode pRoot)
-
{
-
-
if(pRoot != null)
-
{
-
-
System.out.print(pRoot.m_nValue);
-
-
if(pRoot.m_pLeft != null)
-
InorderTree(pRoot.m_pLeft);
-
-
if(pRoot.m_pRight != null)
-
InorderTree(pRoot.m_pRight);
-
}
-
}
-
-
-
//完全二叉树
-
// 1
-
/// \
-
//2 3
-
///\ / \
-
//4 5 6 7
-
public static void test1(){
-
-
BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
-
BinaryTreeNode pNode2 = CreateBinaryTreeNode(2);
-
BinaryTreeNode pNode3 = CreateBinaryTreeNode(3);
-
BinaryTreeNode pNode4 = CreateBinaryTreeNode(4);
-
BinaryTreeNode pNode5 = CreateBinaryTreeNode(5);
-
BinaryTreeNode pNode6 = CreateBinaryTreeNode(6);
-
BinaryTreeNode pNode7 = CreateBinaryTreeNode(7);
-
-
ConnectTreeNodes(pNode1, pNode2, pNode3);
-
ConnectTreeNodes(pNode2, pNode4, pNode5);
-
ConnectTreeNodes(pNode3, pNode6, pNode7);
-
-
InorderTree(pNode1);
-
-
}
-
-
// 不是完全二叉树,但是平衡二叉树
-
// 1
-
/// \
-
//2 3
-
///\ \
-
//4 5 6
-
// /
-
// 7
-
-
public static void test2(){
-
-
BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
-
BinaryTreeNode pNode2 = CreateBinaryTreeNode(2);
-
BinaryTreeNode pNode3 = CreateBinaryTreeNode(3);
-
BinaryTreeNode pNode4 = CreateBinaryTreeNode(4);
-
BinaryTreeNode pNode5 = CreateBinaryTreeNode(5);
-
BinaryTreeNode pNode6 = CreateBinaryTreeNode(6);
-
BinaryTreeNode pNode7 = CreateBinaryTreeNode(7);
-
-
-
ConnectTreeNodes(pNode1, pNode2, pNode3);
-
ConnectTreeNodes(pNode2, pNode4, pNode5);
-
ConnectTreeNodes(pNode3, null, pNode6);
-
ConnectTreeNodes(pNode5, pNode7, null);
-
-
InorderTree(pNode1);
-
-
}
-
-
// 1
-
// /
-
// 2
-
// /
-
// 3
-
// /
-
// 4
-
// /
-
// 5
-
-
public static void test3(){
-
-
BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
-
BinaryTreeNode pNode2 = CreateBinaryTreeNode(2);
-
BinaryTreeNode pNode3 = CreateBinaryTreeNode(3);
-
BinaryTreeNode pNode4 = CreateBinaryTreeNode(4);
-
BinaryTreeNode pNode5 = CreateBinaryTreeNode(5);
-
-
ConnectTreeNodes(pNode1, pNode2, null);
-
ConnectTreeNodes(pNode2, pNode3, null);
-
ConnectTreeNodes(pNode3, pNode4, null);
-
ConnectTreeNodes(pNode4, pNode5, null);
-
-
InorderTree(pNode1);
-
-
}
-
//树中只有一个node
-
public static void test4(){
-
-
BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
-
ConnectTreeNodes(pNode1, null, null);
-
-
InorderTree(pNode1);
-
}
-
-
//树中没有节点
-
public static void test5(){
-
-
InorderTree(null);
-
}
-
-
public static void main(String[] args){
-
-
test1();
-
test2();
-
test3();
-
test4();
-
test5();
-
}
-
-
}
阅读(1245) | 评论(0) | 转发(0) |