Coundy

             漫步風中,傾聽自己的腳步,在自我沉浸中,找尋逝去的靈魂

          posts - 27,comments - 2,trackbacks - 0
          code:
          /**
           *
           */
          package com.algorithms;

          /**
           * @author oracle
           *
           */
          class BinaryNode {

              private int id;

              private BinaryNode left;

              private BinaryNode right;

              public BinaryNode() {
              }

              // composite模式實現(xiàn)binay tree
              //   
              // public void addNode(BinaryNode node) {
              // if (node.getId() > id) {
              // if (left == null)
              // this.left = node;
              // else
              // this.left.addNode(node);
              // } else {
              // if (right == null)
              // this.right = node;
              // else
              // this.right.addNode(node);
              // }
              //
              // }

              public int getId() {
                  return id;
              }

              public void setId(int id) {
                  this.id = id;
              }

              public BinaryNode getLeft() {
                  return left;
              }

              public void setLeft(BinaryNode left) {
                  this.left = left;
              }

              public BinaryNode getRight() {
                  return right;
              }

              public void setRight(BinaryNode right) {
                  this.right = right;
              }

          }

          public class BinaryTree {

              // 添加一個節(jié)點到二叉樹
              // 下面的代碼顯示一個對象的名字相當于指向一個對象的handle
              public static void addNode(BinaryNode root, BinaryNode node) {
                  BinaryNode newnode = root;
                  while (newnode != null) {
                      if (newnode.getId() < node.getId()) {
                          if (newnode.getLeft() == null) {
                              newnode.setLeft(node);
                              newnode = null;
                          } else {
                              newnode = newnode.getLeft();
                          }
                      } else {
                          if (newnode.getRight() == null) {
                              newnode.setRight(node);
                              newnode = null;
                          } else
                              newnode = newnode.getRight();
                      }
                  }
              }

              /**
               * @param args
               */
              public static void main(String[] args) {
                  // TODO Auto-generated method stub
                  BinaryNode root = new BinaryNode();

                  root.setId(50);

                  BinaryNode one = new BinaryNode();

                  one.setId(80);

                  BinaryTree.addNode(root, one);

                  BinaryNode two = new BinaryNode();

                  two.setId(40);

                  BinaryTree.addNode(root, two);

                  BinaryNode three = new BinaryNode();

                  three.setId(90);

                  BinaryTree.addNode(root, three);
              }

          }


          posted on 2007-04-09 03:10 Coundy 閱讀(461) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 岱山县| 聂荣县| 江津市| 宿松县| 柘荣县| 印江| 林芝县| 高邑县| 保德县| 津市市| 固原市| 丰县| 扬州市| 绥棱县| 长海县| 河南省| 梁平县| 横峰县| 五莲县| 松原市| 牡丹江市| 舞钢市| 孙吴县| 定结县| 岳阳县| 彩票| 琼海市| 怀远县| 噶尔县| 青田县| 四川省| 鸡泽县| 麻栗坡县| 秦皇岛市| 拉孜县| 洛南县| 石景山区| 呼伦贝尔市| 长阳| 行唐县| 泾源县|