二叉树的基本操作实现
来源:互联网 发布:php怎么调用存储过程 编辑:程序博客网 时间:2024/05/16 14:47
1.2.1二叉树的基本操作实现:
下面介绍不带头结点二叉树二叉链表的类LinkBiTree < E >。
LinkBiTree < E >类只有一个成员字段head表示头引用。以下是LinkBiTree < E >类的实现。
import javax.xml.soap.Node;/** * @author刘勗 * @create2017-11-23-15:58 */public class LinkBiTree implements IBiTree{ private Node<E> head;//链表头引用指针 public Node<E> getHead(){ return head; } //构造一颗以val为根结点数据域信息,以二叉树lp和rp为左子树和右子树的二叉树 public LinkBiTree(E val, Node<E> lp,Node<E> rp){ Node<E> p =new Node<E>(val,lp,rp); head = p; } //构造函数,生成一颗以val为根节点数据域信息的二叉树 public LinkBiTree(E val){ this(val,null,null); } //构造函数,生成一颗空二叉树 public LinkBiTree(){ head = null; } //判断是否为空 public boolean isEmpty(){ return head ==null; } //获取根结点 public Node<E> Root(){ return head; } //获取结点的左孩子的结点 public Node<E> getLchild(Node<E> p){ return p.getLchild(); } //获取结点的右孩子的结点 public Node<E> getRchild(Node<E> p){ return p.getRchild(); } //将结点p的左子树插入值为val的新结点,原来的左子树成为新结点的左子树 public void insertL(E val,Node<E> p){ Node<E> tmp = new Node<E>(val); tmp.setLchild(p.getLchild()); p.setLchild(tmp); } //将结点p的右子树插入值为val的新结点,原来的右子树成为新结点的右子树 public void insertR(E val,Node<E> p){ Node<E> tmp = new Node<E>(val); tmp.setRchild(p.getRchild()); p.setRchild(tmp); } //若p非空,删除p的左子树 public Node<E> deleteL(Node<E> p){ if((p == null) || (p.getLchild() == null)){ return null; } Node<E> tmp = p.getLchild(); p.setLchild(null); return tmp; } //若p非空,删除p的右子树 public Node<E> deleteR(Node<E> p){ if((p == null) || (p.getRchild() == null)){ return null; } Node<E> tmp = p.getRchild(); p.setRchild(null); return tmp; } //编写算法,在二叉树中查找值为value的结点 public Node<E> search(Node<E> root,E value){ Node<E> p = root; if(p == root){ return null; } if(!p.getData().equals(value)){ return p; } if(p.getLchild()!=null){ return search(p.getLchild(),value); } return null; } //判断是否是否叶子结点 public boolean isLeaf(Node<E> p){ return ((p!=null)&&(p.getLchild()==null)&&(p.getRchild()==null)) }}
阅读全文
0 0
- 二叉树基本操作的程序实现
- Java实现二叉树的基本操作
- 二叉树基本操作的程序实现
- 二叉树基本操作的程序实现
- 二叉树的基本操作-递归实现
- 二叉树的基本操作实现
- Java实现二叉树的基本操作
- 二叉树的基本操作实现
- JAVA实现二叉树的基本操作
- C++实现二叉树的基本操作
- 二叉树的基本操作实现
- 二叉树的基本操作 (二叉链表实现)
- 二叉树的基本操作的c++实现
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- Presto Web UI选型
- Java的List.add()坑
- 未来休闲零食如何创发展?大人版零食或成新方向
- Storm简介
- 判断两个时间区间是否存在交集-Java实现
- 二叉树的基本操作实现
- 3、AOP
- C++编译器构造类的潜规则
- 一台服务器部署两个mysql
- Linux下安装MongoDB3.2.5(自测)
- SpringBoot 学习心得
- Openresty最佳案例 | 第9篇:Openresty实现的网关权限控制
- LeetCode --- 26. Remove Duplicates from Sorted Array
- 安装zabbix-server,进去里面页面提示:strtotime(): It is not safe to rely on the system's timezone settings.