Java学习笔记之数据结构中的树

数据结构中二叉树的代码如下:

  1 package tech2;
  2 
  3 public class BTNode<E> {
  4     private E data;
  5     private BTNode<E> left;
  6     private BTNode<E> right;
  7 
  8     public BTNode(E root) {
  9         data = root;
 10     }
 11 
 12     /**
 13      * BTNode<E> 的构造函数
 14      * 
 15      * @param data
 16      *            结点
 17      * @param left
 18      *            左子树
 19      * @param right
 20      *            右子树
 21      */
 22     public BTNode(E data, BTNode<E> left, BTNode<E> right) {
 23         this.data = data;
 24         this.left = left;
 25         this.right = right;
 26     }
 27 
 28     // get 和 set 方法
 29     public E getData() {
 30         return data;
 31     }
 32 
 33     public void setData(E data) {
 34         this.data = data;
 35     }
 36 
 37     public BTNode<E> getLeft() {
 38         return left;
 39     }
 40 
 41     public void setLeft(BTNode<E> left) {
 42         this.left = left;
 43     }
 44 
 45     public BTNode<E> getRight() {
 46         return right;
 47     }
 48 
 49     public void setRight(BTNode<E> right) {
 50         this.right = right;
 51     }
 52 
 53     public boolean isLeaf() {
 54         return (left == null) && (right == null);
 55     }
 56 
 57     /**
 58      * 查找从本结点开始的最左结点的数据
 59      * 
 60      * @return
 61      */
 62     public E getLeftmostData() {
 63         if (left == null)
 64             return data;
 65         else
 66             return left.getLeftmostData();
 67     }
 68 
 69     public E getRightmostData() {
 70         if (right == null)
 71             return data;
 72         else
 73             return right.getRightmostData();
 74     }
 75 
 76     /**
 77      * 删除最左结点的方法,当删除时最左结点不可能有两个孩子,否则就递归到下一个了
 78      * 
 79      * @return 返回的是比原来小的树的根节点的引用
 80      */
 81     public BTNode<E> removeLeftmost() {
 82         if (left == null)
 83             return right; // 最右边的结点在根处,因为没有左孩子
 84         else {
 85             left = left.removeLeftmost(); // 从这里开始递归了
 86             return this; // 到这里是完成,并且返回
 87         }
 88     }
 89 
 90     public BTNode<E> removeRightmost() {
 91         if (right == null)
 92             return right;
 93         else {
 94             right = right.removeRightmost();
 95             return this;
 96         }
 97     }
 98 
 99     /**
100      * 递归复制一个树
101      * 
102      * @param source
103      *            要被复制的树
104      * @return 返回的是一个新树的对象
105      */
106     public static <E> BTNode<E> treeCopy(BTNode<E> source) {
107         BTNode<E> leftCopy, rightCopy;
108         if (source == null)
109             return null;
110         else {
111             leftCopy = treeCopy(source.left);
112             rightCopy = treeCopy(source.right);
113             return new BTNode<E>(source.data, leftCopy, rightCopy);
114         }
115     }
116 
117     /**
118      * 前序遍历
119      */
120     public void preorderPrint() {
121         System.out.println(data);
122         if (left != null)
123             left.preorderPrint();
124         if (right != null)
125             right.preorderPrint();
126     }
127 
128     /**
129      * 中序遍历
130      */
131     public void inorderPrint() {
132         if (left != null)
133             left.inorderPrint();
134         System.out.println(data);
135         if (right != null)
136             right.inorderPrint();
137     }
138 
139     /**
140      * 后序遍历
141      */
142     public void postorderPrint() {
143         if (left != null)
144             left.postorderPrint();
145         if (right != null)
146             right.postorderPrint();
147         System.out.println(data);
148     }
149 
150     public void print(int depth) {
151         int i;
152         // 打印缩进和当前结点中的数据
153         for (i = 1; i <= depth; i++)
154             System.out.println("  ");
155         System.out.println(data);
156         // 打印左子树
157         if (left != null)
158             left.print(depth + 1);
159         else if (right != null) {
160             for (i = 1; i <= depth + 1; i++)
161                 System.out.println("  ");
162             System.out.println("--");
163         }
164 
165         // 打印右子树
166         if (right != null)
167             right.print(depth + 1);
168         else if (left != null) {
169             for (i = 1; i <= depth; i++)
170                 System.out.println("  ");
171             System.out.println("--");
172         }
173     }
174 
175     /**
176      * 统计树共有多少个结点
177      * 
178      * @param root
179      * @return 返回结点数
180      */
181     public static <E> int treeSize(BTNode<E> root) {
182         if (root == null)
183             return 0;
184         else
185             return 1 + treeSize(root.left) + treeSize(root.right);
186     }
187 
188     /**
189      * 二叉树的深度递归算法
190      * 
191      * @param root
192      * @return 返回树的深度
193      */
194     public static <E> int depth(BTNode<E> root) {
195         if (root == null)
196             return 0;
197         else {
198             int m = depth(root.left);
199             int n = depth(root.right);
200 
201             // 树的深度每次依靠这个三目运算符后加一来递增的
202             return (m > n ? m : n) + 1;
203         }
204     }
205 
206 }

 

posted @ 2012-09-07 21:21  Lowp  阅读(290)  评论(0编辑  收藏  举报