简单数构

来源:互联网 发布:ubuntu 14.10下载 编辑:程序博客网 时间:2024/06/06 07:51

链表:链表与数组有一定的相似性,但是也有一定的区别。数组在查找遍历方面快速简单,数组是通过下表索引查找;而链表在插入删除方面方便简单。数组在插入方面要移动一大堆的数据,而链表确实修改索引就可以轻松达到

获取链表的长度算法

public int getSize() {int num = 0;if(head==null){//空列表}else{ //非空列表num++;//加上头结点Node node=head.getNaxt(); //获取头结点后的节点while(node!=null){//节点非空num++;//节点数累加node=node.getNaxt();//节点指针后移}}return num;}

 添加节点的方法:要判断是否为头结点

@Override/** * 添加数据节点到链表的末尾 */public void add(String date) {Node node =new Node(date,null);if(getSize()==0){//空自定义列表head=node;}else{Node node1=get(getSize()-1);node1.setNaxt(node);}}/** * 添加元素到指定索引位置 */@Overridepublic void add(int index, String date) {Node node =new Node(date,null);if(index<0||index>getSize()){//越界try {throw new Exception("越界");} catch (Exception e) {e.printStackTrace();}}else if(index==0){//空列表时候插入的节点为头结点node.setNaxt(head);head=node;}else{//插入节点到指定的索引出Node node1=get(index-1);Node node2=get(index);node.setNaxt(node2);node1.setNaxt(node);}}

 删除节点方法,要判断要删除的节点是否在链表当中

/** * 删除指定的数据元素节点 */@Overridepublic void del(String date) {int i=fint(date);if(i!=-1){//要删除的数据节点在列表当中get(i-1).setNaxt(get(i+1));//删除节点}}/** * 删除指定索引的节点 */@Overridepublic void del(int intdex) {if(intdex<0||intdex>getSize()){//索引越界try {throw new Exception("越界");} catch (Exception e) {e.printStackTrace();return;}}//删除节点Node node1=get(intdex-1);Node node2=get(intdex+1);node1.setNaxt(node2);}

 查找节点方法:要判断链表是否为空,节点是否是在链表当中

/** * 查找指定索引的节点 */public String fint(int intdex) {return get(intdex).getData();//放回指定索引的节点的数据}/** * 查找指定数据元素的节点 */public int fint(String deta) {int num=0;Node node=head;if(node.getData().equals(deta)){//头结点的数据等于要查找的元素return num;//放回节点数}while(!node.getData().equals(deta)&&node.getNaxt()!=null){//节点后移查找元素num++;node=node.getNaxt();}if(num==getSize()&&!node.getData().equals(deta)){//查找的元素不在列表中try {throw new Exception("不存在该数据的节点");} catch (Exception e) {e.printStackTrace();}return -1;}else return num;}

 获取某节点的方法属于链表操作的核心位置是其他方法的基础,不仅要判断连表是否为空是否存在,索引是否越界

/** * 获取指定的索引的节点 */public Node get(int intdex){//if(intdex<0){//列表为空 try {throw new Exception("无该节点");} catch (Exception e) {e.printStackTrace();return null;}}int count = 0;// 如果index为0的情况,则直接返回头节点if (count == intdex) {//查找到元素return head;}// 判断索引是不是已经越界if (intdex < getSize()) {// 如果没有越界的情况,获取头节点的下一个节点Node node = head.getNaxt();while (node != null) {count++;if (count == intdex) {return node;}node = node.getNaxt();}} else {// 如果索引位置为负数,打印异常栈try {throw new Exception("索引越界");} catch (Exception e) {e.printStackTrace();}}return null;}

 

 

 

 

二叉树:

建立预置的二叉树本质上与实现链表差不多,只不过是在节点中增加多一个索引“指针”,先是把每个节点实例化为一个个孤立的节点,之后通过一定的顺序把节点连接起来。这样简单的二叉树就可以建立起来,在二叉树当中,满二叉树、完全二叉树是相对简单一点的,但是一般的二叉树还是会有一定的难度,在中间的某处的节点得度为一,这个是非常头疼的,但是我们可以把它想成完全二叉树来构建,在度为一的节点处以特殊的标志来代表空节点以及该节点的子树在建立完全二叉树时候,我们可以把所有的双亲节点单独出来,其中最后一个双亲节点再从双亲节点当中单独出来,以便对最后一(两)个叶子做处理

/** * 建立二叉树 */public void creadTree( ){//创建孤立节点for(int i=0;i<array.length;i++){TreeNode tNode=new TreeNode(array[i],null,null);list.add(tNode);} //遍历双亲节点,不包括最后一个双亲节点for(int i=0;i<list.size()/2-1;i++){//建立左子树的链接list.get(i).setLNode(list.get(i*2+1));//建立有子树的链接list.get(i).setRNode(list.get(i*2+2));}//最后一个双亲节点建立左节点的链接list.get(list.size()/2-1).setLNode(list.get((list.size()/2-1)*2+1));//若存在右节点则建立右节点if((list.size()/2-1)*2+2<list.size()){list.get(list.size()/2-1).setLNode(list.get((list.size()/2-1)*2+2));}}

 树的遍历通过递归是最简单的方法,但是在递归当中一定的处理递归出口

0 0