下载此文档

数据结构笔记.doc


文档分类: | 页数:约11页 举报非法文档有奖
1/11
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/11 下载此文档
文档列表 文档介绍
二叉树
public class BinaryTreeTest {
  public static void main(String[] args) {
    new BinaryTreeTest().run();
  }
  static class Node {
    Node left;
    Node right;
    int value;
    public Node(int value) {
       = value;
    }
  }
  public void run() {
    // build the simple tree from chapter 11.
    Node root = new Node(5);
    ("Binary Tree Example");
    ("Building tree with root value " + );
    insert(root, 1);
    insert(root, 8);
    insert(root, 6);
    insert(root, 3);
    insert(root, 9);
    ("Traversing tree in order");
    printInOrder(root);
    ("Traversing tree front-to-back from location 7");
    printFrontToBack(root, 7);
  }
  public void insert(Node node, int value) {
    if (value < ) {
      if ( != null) {
        insert(, value);
      } else {
        ("  Inserted " + value + " to left of "
            + );
         = new Node(value);
      }
    } else if (value > ) {
      if ( != null) {
        insert(, value);
      } else {
        ("  Inserted " + value + " to right of "
            + );
         = new Node(value);
      }
    }
  }
  public void printInOrder(Node node) {
    if (node != null) {
      printInOrder();
      ("  Traversed " + );
      printInOrder();
    }
  }
  /**
   * uses in-order traversal when the origin is less than the node's value
   * 
   * uses reverse-order traversal when the origin is greater than the node's
   * order
   */
  public void printFrontToBack(Node node, int camera) {
    if (node == null)
      return;
    if ( > camera) {
      // print in order
      printFrontToBack(, camera);
      ("  Traversed " + );
   

数据结构笔记 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数11
  • 收藏数0 收藏
  • 顶次数0
  • 上传人mh900965
  • 文件大小74 KB
  • 时间2018-03-21