二叉搜索树
2016-03-31 11:56
363 查看
package 二叉树; public class 二叉树 { public static void main(String[] args) { Tree tree = new Tree(12, 13.5); tree.insert(8, 4.6); tree.insert(16, 5.8); tree.insert(13, 10.3); tree.insert(20, 13.3); tree.insert(23, 7.9); tree.insert(18, 3.4); tree.insert(19, 13.5); tree.inOrder(tree.root); System.out.println("------"); tree.remove(16); tree.inOrder(tree.root); } } // 树节点 class Node { private int iData; private double dData; private Node leftChild; private Node rightChild; public Node() { } public Node(int iData, double dData) { this.iData = iData; this.dData = dData; this.leftChild = null; this.rightChild = null; } public int getiData() { return iData; } public void setiData(int iData) { this.iData = iData; } public double getdData() { return dData; } public void setdData(double dData) { this.dData = dData; } 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 printNode() { System.out.println("iData: " + iData + " dData: " + dData); } } // 树 class Tree { Node root; public Tree() { } public Tree(int iData, double dData) { root = new Node(iData, dData); } public Node search(int key) { Node result = root; // 从根开始 while (result.getiData() != key) { if (result.getiData() > key) result = result.getLeftChild(); else if (result.getiData() < key) result = result.getRightChild(); if (null == result) break; } return result; } public void insert(int iData, double dData) { Node newNode = new Node(iData, dData); if (null == root) root = newNode; else { Node current = root; // 当前遍历结点 Node currentParent = current; // 当前遍历结点父节点 boolean isLeft = false; while (null != current) { if (iData > current.getiData()) { currentParent = current; current = current.getRightChild(); isLeft = false; } else if (iData < current.getiData()) { currentParent = current; current = current.getLeftChild(); isLeft = true; } } if (isLeft) currentParent.setLeftChild(newNode); else currentParent.setRightChild(newNode); } } // 中序遍历 public void inOrder(Node localRoot) { if (null != localRoot) { inOrder(localRoot.getLeftChild()); localRoot.printNode(); inOrder(localRoot.getRightChild()); } } // 返回删除的结点 public Node remove(int key) { boolean isLeft = false; // 寻找要删除的结点 Node current = root; Node parent = current; while (key != current.getiData()) { if (key > current.getiData()) { isLeft = false; parent = current; current = current.getRightChild(); } else if (key < current.getiData()) { isLeft = true; parent = current; current = current.getLeftChild(); } if (null == current) break; } // 判断是否找到键值为key的结点 if (null == current) { System.out.println("找不到键值为" + key + "的结点。"); return null; } // 找到了键值为key的结点 // 判断是否为叶子结点 如果是叶子结点 直接置父节点相应子树为null if (null == current.getLeftChild() && null == current.getRightChild()) { if (current == root) // 判断一下该结点是否为根节点 root = null; else if (isLeft) parent.setLeftChild(null); else parent.setRightChild(null); } // 不是叶子结点 判断是否只有一个子节点 // 如果该结点只有右孩子 则 如果该结点是父节点的左孩子 就将该结点的右孩子赋给父节点的左孩子 反之 赋给父节点的右孩子 else if (null == current.getLeftChild()) { if (current == root) root = current.getRightChild(); if (isLeft) parent.setLeftChild(current.getRightChild()); else parent.setRightChild(current.getRightChild()); } // 如果该结点只有左孩子 则 如果该结点是父节点的左孩子 就将该结点的左孩子赋给父节点的左孩子 反之 赋给父节点的右孩子 else if (null == current.getRightChild()) { if (current == root) root = current.getLeftChild(); if (isLeft) parent.setLeftChild(current.getLeftChild()); else parent.setRightChild(current.getLeftChild()); } else { Node successorNode = getSuccessor(current); if (current == root) root = successorNode; else if (isLeft) parent.setLeftChild(successorNode); else parent.setRightChild(successorNode); successorNode.setLeftChild(current.getLeftChild()); } return current; } // 获取后继结点 private Node getSuccessor(Node delNode) { Node successorParentNode = delNode; Node successorNode = delNode; Node current = delNode.getRightChild(); while (null != current) { successorParentNode = successorNode; successorNode = current; current = current.getLeftChild(); } if (successorNode != delNode.getRightChild()) { successorParentNode.setLeftChild(successorNode.getRightChild()); successorNode.setRightChild(delNode.getRightChild()); } return successorNode; } }
相关文章推荐
- 解决CATextLayer绘制出来的字体模糊问题
- [Unity3d插件KGFMapSystem]非常不错的小地图的制作
- jQuery 的选择器
- 单元格点击效果,一闪就没了
- Java微信公众平台开发(八)——多媒体消息回复
- Backbone的model学习笔记一
- SpringMVC中的DispatcherServlet (及WebApplicationContext部分)
- EditeText的maxlength中文、字符长度限制
- 视频编解码学习之一:理论基础
- HMTL5的 video 在IOS7中碰到的坑
- Android面试回忆录
- 以platform总线方式注册杂项设备驱动
- centos6.7安装oracle11gR2安装步骤记录
- Android-动态加载和静态加载Fragment
- Java微信公众平台开发【番外篇】(七)——公众平台测试帐号的申请
- HIBERNATE与 MYBATIS的对比
- 金额转换,阿拉伯数字转换成中国传统形式
- 打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。
- MongoDB使用小结:一些常用操作分享
- cocos2dx中CC_CALLBACK_N的理解