通过编写自定义链表和二叉树增进对链表和二叉树的了解
来源:互联网 发布:信捷plc编程线usb驱动 编辑:程序博客网 时间:2024/05/16 08:11
链表是一种很常用很基础的存储方式,在很多更复杂的数据结构中都需要借用到链表来进行数据存储。java的解。
首先可以先编写一个接口LinkListInterface来提供所需要实现的方法:public int size() ,public Node get(int index),public void add(String value),public void add(String value, int index),public void remove(int index)来实现增删查的功能。
链表需要通过节点连接起来,由此需要自定义一个节点类Node来存储数据,其中包含数据域属性String data,下一个节点数据Node next,同时写出数据的set方法和get方法。
自定义链表的实现如下:
//头节点
Private Node head;
//获取链表长度
public int size() {
int count = 0;
//判断当前头节点是否为空
if(head == null){
return count;
}else{
//如果头节点存在则累加计数器一次
count++;
//获取下一个节点
Node node = head.getNext();
while(node!=null){
count++;
node = node.getNext();
}
}
return count;
}
//获取当前索引位置的节点
public Node get(int index) {
//拿到指定索引位置的节点对象
if(index<0){
//如果索引位置为负数,打印异常栈
try {
throw new Exception("索引不能为负数:"+index);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
int count = 0;
//如果index为0的情况,则直接返回头节点
if(count==index){
return head;
}
//判断索引是否已经越界
if(index <size()){
//没有越界的情况,获取头节点的下一节点
Node node = head.getNext();
while(node!=null){
count++;
if(count == index){
return node;
}
node =node.getNext();
}
}else{
//如果索引位置为负数,打印异常栈
try {
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
//往链表中添加节点
public void add(String value) {
//链表添加节点
//创建一个新的节点
Node node = new Node(value, null);
if(size()==0){
head = node;
}else{
Node node1 = get(size()-1);
node1.setNext(node);
}
}
//往链表指定位置添加节点
public void add(String value, int index) {
//链表添加节点
//创建一个新的节点
Node node = new Node(value, null);
if(head == null){
head = node;
return;
}
//先拿到当前索引位置的节点
if(index == 0){
node.setNext(head);
head = node;
return;
}
Node node_current = get(index);
Node node_parent = get(index-1);
node_parent.setNext(node);
node.setNext(node_current);
}
//删除节点
public void remove(int index) {
if(size()==0){
try {
throw new Exception("当前链表长度为0,无法执行删除操作!");
} catch (Exception e) {
e.printStackTrace();
}
return;
}
else if(size()-1<index){
try {
throw new Exception("删除越界!");
} catch (Exception e) {
e.printStackTrace();
return;
}
}
if(index==size()-1){
Node node_remove = get(index);
node_remove = null;
}else if(index==0){
head =head.getNext();
}else{
Node node_remove2= get(index);
Node node_past = get(index-1);
Node node_next = get(index+1);
node_past.setNext(node_next);
node_remove2.setNext(null);
node_remove2 = null;
}
}
二叉树是有序树的一种,也是较为常见的一种数据结构。可有根节点分出子节点进行树形结构储存。二叉树的实现方法与链表的实现方法有一定的共同之处。实现方法如下:
//创建二叉树
public void createTree(){
//将数据封装成一个个节点
for(int i=0;i<value.length;i++){
TreeNode node = new TreeNode(value[i], null, null);
list.add(node);
}
//通过list重新构建完全二叉树
for(int i =0;i<list.size()/2-1;i++){
//给父节点设置左右节点
list.get(i).setLeftNode(list.get(2*i+1));
list.get(i).setRightNode(list.get(2*i+2));
}
int lastIndex = list.size()/2-1;
//给最后一个父节点添加子节点
TreeNode parentNode = list.get(lastIndex);
parentNode.setLeftNode(list.get(2*lastIndex+1));
//如果总节点数为奇数个,则最后一个父节点存在右节点
if(list.size()%2==1){
parentNode.setRightNode(list.get(2*lastIndex+2));
}
}
//线序遍历树
public void preOrder(TreeNode root){
if(root==null){
return;
}
//先访问根节点
System.out.print(root.getValue()+" ");
//在访问左节点
preOrder(root.getLeftNode());
//在访问右节点
preOrder(root.getRightNode());
}
/**
* 中序遍历树
*/
public void centerOrder(TreeNode root){
if(root==null){
return;
}
//先访问左节点
centerOrder(root.getLeftNode());
//再访问根节点
System.out.print(root.getValue()+" ");
//再访问右节点
centerOrder(root.getRightNode());
}
/**
* 后序遍历树
*/
public void endOrder(TreeNode root){
if(root==null){
return;
}
//先访问左节点
endOrder(root.getLeftNode());
//再访问右节点
endOrder(root.getRightNode());
//再访问根节点
System.out.print(root.getValue()+" ");
}
- 通过编写自定义链表和二叉树增进对链表和二叉树的了解
- 特别的数据存储:自定义链表和二叉树
- 二叉树的简单介绍和二叉树的二叉链表存储表示
- 链表和二叉树
- 链表和二叉树
- (1)建立二叉树的二叉链表。 (2)写出对用二叉链表存储的二叉树进行先序、中序和后序遍历的递归和非递归算法。 (3)写出对用二叉链表存储的二叉树进行层次遍历算法。 (4)求二叉树的所有叶子及结点总数。
- 二叉树的二叉链表创建
- 二叉树的二叉链表实现
- 二叉树的二叉链表实现
- 二叉树的二叉链表存储
- 建立二叉树的二叉链表
- 简单了解树和二叉树
- 二叉树、二叉链表
- 二叉树:二叉链表
- 二叉树的二叉链表表示和实现
- 二叉搜索树和双向链表
- 二叉搜索树和双向链表
- 二叉搜索树和双向链表
- 各种排序算法特点
- 2015年2月4日
- cmake的语法及使用用例
- WV.42-Fibonacci斐波那契数列前20项
- 2月4号 集训第四天
- 通过编写自定义链表和二叉树增进对链表和二叉树的了解
- Ecshop与Jquery冲突的完美解决方案
- 雷观(二十一):想创业,需要刺激;去创业,需要勇气;创业成功,需要智慧
- 【推荐】IDA sp-analysis failed 不能F5的 解决方案之(二)
- 产生冠军
- iphone5s关闭voiceOver,关闭不了
- eclipse+cygwin+cdt搭建c/c++开发环境
- HDOJ 题目1495 非常可乐(BFS)
- 确定比赛名次