二叉树的结构与实现
2012-03-15 16:19
204 查看
树型结构是用来存取数据的效率比较好的一种数据结构,增,删,改效率都比前面介绍的数据结构要高。缺点就是实现起来比较复杂。下面以二叉树为例子,来说明数型结构的特点:
请看下例:
二叉树的实现:
运行的结果如图
请看下例:
二叉树的实现:
class JD { int data;// 数据 JD left; // 左儿子 JD right;// 右儿子 public JD(int data) { this.data = data; } public String toString() { return data + ""; } }; // 该类实现了增,删,改,查等特性 class Tree{ JD root; JD parrent; boolean b; public boolean add(int d) {// 增加数据的方法 JD jd = new JD(d); if (root == null){ // 如果根节点为空,那么把新节点加给根节点 root = jd; }else { JD current = root; while (current != null) {// 是找到一个位置加新节点 if (d == current.data)// 如果已经存在,则直接返回false 表示加失败 return false; else if (d > current.data) {// 如果该值大于当前节点,那么应该往右边找 parrent = current; // 记录要加新节点的父节点 b = true; // 记录是左边还是右边, current = current.right;// current.right=current } else if (d < current.data) { parrent = current; b = false; current = current.left;// current.left=current } }// end while if (b)// 如果是右儿子为空 ,就加父节点的右边 parrent.right = jd; else parrent.left = jd; } return true; } public JD find(int d) {// 查询的方法 JD current = root; while (current != null) { if (current.data == d) return current; else { parrent = current;// 记录找到节点的父节点,以方便删除操作 if (d > current.data) { current = current.right; b = true; } else if (d < current.data) { current = current.left; b = false; } } }// end while return current; } public boolean delete(int d) { JD current = find(d); if (current == null){ return false; }else if (current.left == null && current.right == null) // 如果要删除的节点是页节点 { if (current == root)// 如果是根节点(也是页节点),直接让根节点==null root = null; else if (b) parrent.right = null; else parrent.left = null; } else if (current.left == null) {// 如果删除节点只有右节点 if (b) parrent.right = current.right; else parrent.left = current.right; } else if (current.right == null) {// 如果删除节点只有左节点 if (b)// 该变量记录了要删除的节点,在其父节点的左边还是右边 parrent.right = current.left; else parrent.left = current.left; } else { JD temp = fenlie(current); // 分裂节点 if (b) parrent.right = temp; else parrent.left = temp; } return true; } public JD fenlie(JD c) { JD temp = c.right; JD p = temp;// 用来记录要删除节点右儿子那边的最小节点 JD pp = temp;// 用来记录要删除节点右儿子那边的最小节点的父节点 while (temp != null) {// 找到要删除节点右儿子那边的最小节点 pp = p; p = temp; temp = temp.left; } if (pp == p) {// 如果删除节点的右儿子节点没有左儿子 pp.left = c.left;// 把删除节点左儿子加到删除节点的右儿子的左边 return pp; } else { pp.left = p.right;// 把找到的节点的右儿子部分加到该节点父节点的左边 p.left = c.left;// 把删除节点的左儿子加到分裂节点的左边 p.right = c.right;// 把删除节点的右儿子加到分裂节点的右边 return p; } } public boolean modify(int s, int m) {// 修改数据的方法=先删除后增加,这样还是有//顺序的 delete(s); return add(m); } public void print(JD jd) {// 递归中序遍历该有序二叉树 if (jd != null) { print(jd.left); System.out.print(jd + " "); print(jd.right); } } public void p() { print(root); } } class TestTree { public static void main(String[] args) { Tree t = new Tree(); t.add(5); t.add(7); t.add(3); t.add(9); t.add(1); t.add(8); t.add(13); t.add(4); t.p(); System.out.println("\n" + "-------------改变----------"); t.modify(8, 18);// 把接点值为8的接点,把该值改为18 t.delete(9);// 删除接点值为9的接点 t.p(); } }
运行的结果如图
![](http://hi.csdn.net/attachment/201203/15/0_13317997597YB9.gif)
相关文章推荐
- 树和二叉树的存储结构的实现(C/C++实现)
- 二叉树的链式存储结构 C++代码实现
- 【数据结构与算法】(六) c 语言实现简单的二叉树静态创建及先序、中序、后序遍历
- 用python实现二叉树结构
- 数据结构之二叉树实现
- 大话数据结构 code 第五章 02二叉树链式结构实现_BiTreeLink
- 顺序结构实现二叉树的建立和各种遍历
- 二叉树应用-Huffman树类模板的实现(数据结构基础 第6周)
- 二叉树的实现数据结构
- 二叉树顺序结构实现/链式结构实现
- 建立线索二叉链表结构,实现二叉树的中序线索化及中序线索二叉树的遍历算法
- 链式结构实现二叉树
- 数据结构C语言实现之二叉树链式结构
- 实验五 树和二叉树的实验1 树的顺序结构的实现
- 大话数据结构 code 第6章 01二叉树顺序结构实现_BiTreeArray
- 程序实现二叉树的结构及其操作
- 二叉树的链式存储结构 前序 后序 中序 层序遍历操作实现 判断是否完全二叉树
- 二叉树 二叉树的性质 存储结构 遍历二叉树 C实现二叉树的创建和遍历 线索二叉树
- 数据结构与算法(C#实现)系列---二叉树
- 数据结构之二叉树――链式存储结构(php代码实现)