Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104518
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-23 12:37
文章分类

全部博文(41)

文章存档

2015年(1)

2014年(28)

2013年(12)

我的朋友

分类: Java

2014-08-31 12:40:55


点击(此处)折叠或打开

  1. package com.wp;

  2. //import java.util.*;

  3. public class BinaryTreeNode {
  4.     
  5.     int m_nValue;
  6.     BinaryTreeNode m_pLeft;
  7.     BinaryTreeNode m_pRight;
  8.     
  9.     
  10.    public static BinaryTreeNode CreateBinaryTreeNode(int value)
  11.     {
  12.         BinaryTreeNode pNode = new BinaryTreeNode();
  13.         pNode.m_nValue = value;
  14.         pNode.m_pLeft = null;
  15.         pNode.m_pRight = null;

  16.         return pNode;
  17.     }
  18.    
  19.   public static void ConnectTreeNodes(BinaryTreeNode pParent, BinaryTreeNode pLeft, BinaryTreeNode pRight)
  20.    {
  21.        if(pParent != null)
  22.        {
  23.            pParent.m_pLeft = pLeft;
  24.            pParent.m_pRight = pRight;
  25.        }
  26.    }

  27.     
  28.   //先序遍历二叉树
  29.    public static void InorderTree(BinaryTreeNode pRoot)
  30.    {

  31.        if(pRoot != null)
  32.        {
  33.         
  34.          System.out.print(pRoot.m_nValue);
  35.         
  36.            if(pRoot.m_pLeft != null)
  37.              InorderTree(pRoot.m_pLeft);

  38.            if(pRoot.m_pRight != null)
  39.                InorderTree(pRoot.m_pRight);
  40.        }
  41.    }

  42.   
  43. //完全二叉树
  44. // 1
  45. /// \
  46. //2 3
  47. ///\ / \
  48. //4 5 6 7
  49.    public static void test1(){
  50.     
  51.      BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
  52.      BinaryTreeNode pNode2 = CreateBinaryTreeNode(2);
  53.      BinaryTreeNode pNode3 = CreateBinaryTreeNode(3);
  54.      BinaryTreeNode pNode4 = CreateBinaryTreeNode(4);
  55.      BinaryTreeNode pNode5 = CreateBinaryTreeNode(5);
  56.      BinaryTreeNode pNode6 = CreateBinaryTreeNode(6);
  57.      BinaryTreeNode pNode7 = CreateBinaryTreeNode(7);

  58.      ConnectTreeNodes(pNode1, pNode2, pNode3);
  59.      ConnectTreeNodes(pNode2, pNode4, pNode5);
  60.      ConnectTreeNodes(pNode3, pNode6, pNode7);
  61.     
  62.      InorderTree(pNode1);
  63.     
  64.    }
  65.    
  66. // 不是完全二叉树,但是平衡二叉树
  67. // 1
  68. /// \
  69. //2 3
  70. ///\ \
  71. //4 5 6
  72. // /
  73. // 7
  74.    
  75.    public static void test2(){
  76.     
  77.      BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
  78.      BinaryTreeNode pNode2 = CreateBinaryTreeNode(2);
  79.      BinaryTreeNode pNode3 = CreateBinaryTreeNode(3);
  80.      BinaryTreeNode pNode4 = CreateBinaryTreeNode(4);
  81.      BinaryTreeNode pNode5 = CreateBinaryTreeNode(5);
  82.      BinaryTreeNode pNode6 = CreateBinaryTreeNode(6);
  83.      BinaryTreeNode pNode7 = CreateBinaryTreeNode(7);
  84.     
  85.     
  86.      ConnectTreeNodes(pNode1, pNode2, pNode3);
  87.      ConnectTreeNodes(pNode2, pNode4, pNode5);
  88.      ConnectTreeNodes(pNode3, null, pNode6);
  89.      ConnectTreeNodes(pNode5, pNode7, null);
  90.     
  91.      InorderTree(pNode1);
  92.     
  93.    }
  94.    
  95. //                    1
  96. //                  /
  97. //                2
  98. //               /
  99. //             3
  100. //            /
  101. //           4
  102. //         /
  103. //         5
  104.    
  105.  public static void test3(){
  106.     
  107.      BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
  108.      BinaryTreeNode pNode2 = CreateBinaryTreeNode(2);
  109.      BinaryTreeNode pNode3 = CreateBinaryTreeNode(3);
  110.      BinaryTreeNode pNode4 = CreateBinaryTreeNode(4);
  111.      BinaryTreeNode pNode5 = CreateBinaryTreeNode(5);

  112.      ConnectTreeNodes(pNode1, pNode2, null);
  113.      ConnectTreeNodes(pNode2, pNode3, null);
  114.      ConnectTreeNodes(pNode3, pNode4, null);
  115.      ConnectTreeNodes(pNode4, pNode5, null);
  116.     
  117.      InorderTree(pNode1);
  118.     
  119.  }
  120.  //树中只有一个node
  121.  public static void test4(){
  122.     
  123.      BinaryTreeNode pNode1 = CreateBinaryTreeNode(1);
  124.      ConnectTreeNodes(pNode1, null, null);
  125.     
  126.      InorderTree(pNode1);
  127.  }
  128.  
  129. //树中没有节点
  130.  public static void test5(){
  131.     
  132.      InorderTree(null);
  133.  }
  134.  
  135.    public static void main(String[] args){
  136.     
  137.      test1();
  138.      test2();
  139.      test3();
  140.      test4();
  141.      test5();
  142.    }
  143.    
  144. }


阅读(1219) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~