posts - 13, comments - 7, trackbacks - 0, articles - 0

          數(shù)據(jù)結(jié)構(gòu)中的樹

          Posted on 2008-10-24 00:06 eyejava 閱讀(286) 評論(0)  編輯  收藏
          package tree;

          /**
           * 結(jié)點數(shù)據(jù)
           * @author jiaqiang
           *
           */
          public class Node {
              private int key;
              private int data;
              private Node leftChild;
              private Node rightChild;
              
              public Node() {}
              
              public Node(int key, int data) {
                  this.key = key;
                  this.data = data;
              }

              public int getKey() {
                  return key;
              }

              public int getData() {
                  return data;
              }

              public Node getLeftChild() {
                  return leftChild;
              }

              public void setLeftChild(Node leftChild) {
                  this.leftChild = leftChild;
              }

              public Node getRightChild() {
                  return rightChild;
              }

              public void setRightChild(Node rightChild) {
                  this.rightChild = rightChild;
              }
              
              public void display() {
                  System.out.print(data + " ");
              }
          }

          -------------------
          package tree;

          /**
           * 二叉樹
           * @author jiaqiang
           *
           */
          public class Tree {
              private Node root;
              private int size;
              
              /**
               * 查找某個值
               * @param key   要查找的值
               * @return Node 查找到的結(jié)點
               */
              public Node find(int key) {
                  Node current = root;
              
                  while ((current!=null) && (current.getKey()!=key)) {
                      if (key < current.getKey()) {           //go left
                          current = current.getLeftChild();
                      } else {
                          current = current.getRightChild();  //go right
                      }
                      
                      if (current == null) {
                          return null;
                      }
                  }
                  
                  return current;
              }
              
              /**
               * 插入某個值
               * @param key
               * @param data
               */
              public void insert(int key, int data) {
                  Node newNode = new Node(key, data);
                  
                  if (root == null) {  //空樹
                      root = newNode;
                      size++;
                  } else {             //not empty tree
                      Node current = root;
                      Node parent;     //作為插入結(jié)點的父結(jié)點
                      
                      while (true) {
                          parent = current;
                          if (key < current.getKey()) {
                              current = current.getLeftChild();
                              if (current == null) {
                                  parent.setLeftChild(newNode);
                                  size++;
                                  return;
                              }
                          } //end if go left
                          else {
                              current = current.getRightChild();
                              if (current == null) {
                                  parent.setRightChild(newNode);
                                  size++;
                                  return;
                              }
                          } //end if go right
                      } // end while (true)
                  }
              } //end insert
              
              /**
               * 中序遍歷,該方法為輔助方法
               * @param Node localRoot
               */
              private void inOrder(Node localRoot) {
                  if (localRoot != null) {
                      inOrder(localRoot.getLeftChild());
                      localRoot.display();
                      inOrder(localRoot.getRightChild());
                  } else
                      return;
              }
              
              /**
               * 中序遍歷
               */
              public void inOrder() {
                  inOrder(root);
              }
              
              public int getSize() {
                  return size;
              }

              public static void main(String[] args) {
                  Tree tree = new Tree();
                  tree.insert(23, 23);
                  tree.insert(20, 20);
                  tree.insert(24, 24);
               
                  
                  System.out.println(tree.getSize());
                  tree.inOrder();
              }

          }



          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 郁南县| 巫溪县| 宣汉县| 旌德县| 东乡| 江都市| 东乡县| 浦北县| 巩义市| 襄城县| 郁南县| 浦城县| 三明市| 屯门区| 八宿县| 庆云县| 长治县| 都兰县| 德州市| 衢州市| 和静县| 耒阳市| 宁武县| 九寨沟县| 故城县| 平舆县| 越西县| 张家口市| 永春县| 中江县| 崇信县| 成安县| 徐州市| 方城县| 潮州市| 青阳县| 嘉荫县| 神农架林区| 那曲县| 阿勒泰市| 西藏|