A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孙沛 中级黑马   /  2012-9-8 14:30  /  1187 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 孙沛 于 2012-9-8 14:32 编辑

早上看数据结构时,二叉平衡树就不怎么看得懂,尝试写代码发现无法进展下去,麻烦懂的人教下

2 个回复

倒序浏览
  1. /**
  2. * 二叉树加入节点,以及按照中序排序.
  3. */
  4. class BinaryTree<T extends Comparable<T>> {

  5.         private Node root;// 二叉树的根节点

  6.         class Node {
  7.                 private T data;
  8.                 private Node left;// 左节点
  9.                 private Node right;// 右节点

  10.                 public Node(T data) {
  11.                         this.data = data;
  12.                 }

  13.                 /**
  14.                  * 添加节点
  15.                  *
  16.                  * @param newNode
  17.                  *            要添加的节点,按中序排序.
  18.                  */
  19.                 public void addNode(Node newNode) {
  20.                         if (newNode.data.compareTo(this.data) <= 0) {
  21.                                 if (this.left == null) {
  22.                                         this.left = newNode;
  23.                                 } else {
  24.                                         this.left.addNode(newNode);
  25.                                 }
  26.                         }
  27.                         if (newNode.data.compareTo(this.data) > 0) {
  28.                                 if (this.right == null) {
  29.                                         this.right = newNode;
  30.                                 } else {
  31.                                         this.right.addNode(newNode);
  32.                                 }
  33.                         }
  34.                 }

  35.                 /**
  36.                  * 按中序遍历并打印.
  37.                  */
  38.                 public void printNode() {
  39.                         if (this.left != null) {
  40.                                 this.left.printNode();
  41.                         }
  42.                         System.out.println(this.data);
  43.                         if (this.right != null) {
  44.                                 this.right.printNode();
  45.                         }
  46.                 }
  47.         }

  48.         /**
  49.          * 为二叉树增加一个节点,如果没有根节点,新增的节点就为根节点,否则将按照中序,为根节点增加新的节点.
  50.          *
  51.          * @param data
  52.          *            节点中的的一个属性,此属性的类型不确定,通过实例化二叉树来决定此数据的具体类型.
  53.          */
  54.         public void add(T data) {
  55.                 Node newNode = new Node(data);
  56.                 if (this.root == null) {
  57.                         this.root = newNode;
  58.                 } else {
  59.                         this.root.addNode(newNode);
  60.                 }
  61.         }

  62.         /**
  63.          * 将二叉树的所有节点按中序打印出来
  64.          */
  65.         public void print() {
  66.                 this.root.printNode();
  67.         }

  68. }

  69. public class BinaryTreeTest {

  70.         public static void main(String[] args) {
  71.                 BinaryTree<Integer> bt = new BinaryTree<Integer>();
  72.                 bt.add(3);
  73.                 bt.add(5);
  74.                 bt.add(1);
  75.                 bt.add(0);
  76.                 bt.add(1);
  77.                 bt.add(9);
  78.                 bt.print();
  79.         }

  80. }
复制代码
回复 使用道具 举报
说实话在java的编程中很少会直接用到底层的数据结构的东西,这些数据结构在java中都是被封装好的,比如说列表,哈希表,图,二叉树,比如各种排序,等等,直接使用就可以,基本上有个大概了解就可以,而你要了解二叉树,首先你要明白在编程语言中,什么是树的概念,这就涉及到底层的算法设计思想,这种思想其实注重的不是代码,而是解决问题的思路,这就是数据结构,一般大学里面都是有这样的专门课程,如果你想深入了解建议买本数据结构的书看看,或者看看教程
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马