数据结构:二叉树的二叉链表存储--Java实现
来源:互联网 发布:淘宝开店要哪些软件 编辑:程序博客网 时间:2024/05/24 03:08
public class TwoLinkTree<E> {// 内部节点类public static class TreeNode {Object data;TreeNode left;TreeNode right;public TreeNode(Object data) {this.data = data;}public TreeNode(Object data, TreeNode left, TreeNode right) {this.data = data;this.left = left;this.right = right;}public TreeNode() {}}// rootprivate TreeNode root;// 默认方式创建二叉树public TwoLinkTree() {root = new TreeNode();}// 指定元素创建二叉树public TwoLinkTree(E data) {root = new TreeNode(data);}// 判定二叉树是否为空public boolean empty() {return root == null;}// 获取根节点public TreeNode getRoot() {if (empty()) {throw new RuntimeException("无根节点");} else {return root;}}// 获取指定节点的左节点public E leftChild(TreeNode node) {if (node == null) {throw new RuntimeException("该节点为空");}return node.left == null ? null : (E) node.left.data;}// 获取指定节点的右节点public E rightChild(TreeNode node) {if (node == null) {throw new RuntimeException("该节点为空");}return node.right == null ? null : (E) node.right.data;}// 向二叉树中添加节点public TreeNode addNote(TreeNode parent, E data, boolean isleft) {if (parent == null) {throw new RuntimeException("无法添加此节点,此节点为空");}if (isleft && parent.left != null) {throw new RuntimeException("此节点以有左节点,无法添加");}if (!isleft && parent.right != null) {throw new RuntimeException("此节点以有右节点,无法添加");}TreeNode newNode = new TreeNode(data);if (isleft) {parent.left = newNode;} else {parent.right = newNode;}return newNode;}// 获取二叉树的深度public int deepin() {return deepin(root);}// 获取指定节点的子树的深度public int deepin(TreeNode node) {if (node == null) {return 0;}if (node.left == null && node.right == null) {return 1;} else {int leftdeep = deepin(node.left);int rightdeep = deepin(node.right);int max = (leftdeep > rightdeep ? leftdeep : rightdeep);return max + 1;}}}
0 0
- 数据结构:二叉树的二叉链表存储--Java实现
- 数据结构:二叉树的三叉链表存储--Java实现
- [数据结构][二叉链表]二叉树链式存储结构的二叉链表实现
- 二叉树的二叉链表存储及其Java实现
- 二叉树的二叉链表存储(java实现)
- 数据结构之---C语言实现二叉树的二叉链表存储表示
- 数据结构:二叉树的链式存储实现
- java链表的数据结构和二叉树的实现
- 二叉树的二叉链表存储简单实现
- 二叉树的三叉链表存储及其Java实现
- 二叉树的三叉链表存储(java实现)
- 二叉树的三叉链表存储及其Java实现
- Java基础 - 二叉树的二叉链表存储
- 用Java实现二叉链表方式存储的二叉树
- 数据结构--二叉树的存储
- 数据结构_树_二叉树的建立、遍历、复制与移除_二叉链表存储_C++实现
- 二叉树的二叉链表存储
- 【数据结构】之二叉树的java实现
- SpringMVC 用注解Annotation驱动的IoC功能@Autowired @Component
- curl Php配置
- [Android]如何导入已有的外部数据库
- pydbg学习
- wxWidgets典型错误
- 数据结构:二叉树的二叉链表存储--Java实现
- C++解析Json
- 《编程之美》---精确表达浮点数子问题---辗转相除法求取最大公约数
- Arcgis10 文件-导出地图选项没了
- 中移动强推五模手机 千元低端机面临成本难题
- 常用算法技巧及常识
- 我爱你
- 第十一讲学习,添加地名
- SSH 类库问题 java.lang.NoClassDefFoundError: Could not initialize class