java顺序表和树的实现
一、顺序表
1.线性表
//java顺序表的实现,如ArrayList就是用线性表实现的,优点是查找快,缺点是添加或删除要移动很多元素,速度慢 public class SequenceList {private int MAXLENGTH;//顺序表大小private int count;//线性表存在数据个数private Data[] data;//数据储存private static class Data{String name;int stuNo;int scores;}public void init(int maxLength){this.MAXLENGTH=maxLength;data=new Data[MAXLENGTH];}//添加一条数据public void add(Data d){if(count==MAXLENGTH){System.out.println("顺序表已满!不可添加");}else{data[count]=d;count++;System.out.println("添加成功!");}}//插入任意一条数据public void insert(Data d,int position){if(count==MAXLENGTH||position>MAXLENGTH||position<0){System.out.println("顺序表已满或者插入位置有问题!不可插入");}else{for(int i=count;i>=position;i--){data[i+1]=data[i];}data[position]=d;count++;System.out.println("插入成功!");}}//删除数据public void del(int position){if(position>count+1||position<0){System.out.println("删除位置有误!");}else{for(int i=position;i<count;i++){data[i]=data[i+1];}count--;System.out.println("删除成功!");}}//更新一个数据public void updata(Data d,int position){if(position>count+1||position<0){System.out.println("更新位置有误!");}else{data[position]=d;System.out.println("更新成功!");}}//查询一个数据public Data sel(int position){if(position>count+1||position<0){System.out.println("查询位置有误!");return null;}else{return data[position];}}public static void main(String[] args){SequenceList sl=new SequenceList();sl.init(10);Data d=new Data();sl.add(d);sl.insert(d, 0);sl.del(0);} }
2.链式表
public class SingleList {private Node_Single head = null;//头节点private Node_Single tail = null;//尾节点(空节点)相当于哨兵元素/*** 初始化一个链表(设置head )* @param key*/public void initList(Node_Single node){head = node;head.next = tail;}/*** 添加一个元素* @param node*/public void addTolist(Node_Single node){if(head == null){initList(node);}else{Node_Single tmp = head;head = node;node.next = tmp;}}/*** 遍历链表,删除某一个节点* @param node* @param myList*/public void deleteNode(Node_Single node,SingleList myList){if(myList == null){return ;}Node_Single tmp =null;for(tmp = myList.getHead();tmp!=null;tmp = tmp.next){if(tmp.next !=null && node.getKey().equals(tmp.next.getKey())){//该元素和后一个元素相同。指针指向下一元素的下一元素if(tmp.next.next != null){tmp.next = tmp.next.next;}else{tmp.next = null;}}}}public void printList(SingleList myList){Node_Single tmp =null;for(tmp = myList.getHead();tmp!=null;tmp = tmp.next){System.out.println(tmp.getKey());}}public Node_Single getHead() {return head;}public void setHead(Node_Single head) {this.head = head;}public Node_Single getTail() {return tail;}public void setTail(Node_Single tail) {this.tail = tail;}public static void main(String[] args){SingleList myList = new SingleList();Node_Single node_1 = new Node_Single("1");Node_Single node_2 = new Node_Single("2");Node_Single node_3 = new Node_Single("3");Node_Single node_4 = new Node_Single("4");Node_Single node_5 = new Node_Single("5");Node_Single node_6 = new Node_Single("6");Node_Single node_7 = new Node_Single("7");myList.addTolist(node_1);myList.addTolist(node_2);myList.addTolist(node_3);myList.addTolist(node_4);myList.addTolist(node_5);myList.addTolist(node_6);myList.addTolist(node_7);myList.deleteNode(node_3, myList);myList.printList(myList);}public static class Node_Single {public String key;//节点的值public Node_Single next;//指向下一个的指针public Node_Single(String key){//初始化headthis.key = key;this.next = null;}public Node_Single(String key,Node_Single next){this.key = key;this.next = next;}public String getKey() {return key;}public void setKey(String key) {this.key = key;}public Node_Single getNext() {return next;}public void setNext(Node_Single next) {this.next = next;}@Overridepublic String toString() {return "Node_Single [key=" + key + ", next=" + next + "]";}} }
三、二叉树
import java.util.Stack;public class BinaryTree { private TreeNode root=null; public BinaryTree(){ root=new TreeNode(1,"rootNode(A)"); } /** * 创建一棵二叉树 * <pre> * A * B C * D E F * </pre> * @param root * @author WWX */ public void createBinTree(TreeNode root){ TreeNode newNodeB = new TreeNode(2,"B"); TreeNode newNodeC = new TreeNode(3,"C"); TreeNode newNodeD = new TreeNode(4,"D"); TreeNode newNodeE = new TreeNode(5,"E"); TreeNode newNodeF = new TreeNode(6,"F"); root.leftChild=newNodeB; root.rightChild=newNodeC; root.leftChild.leftChild=newNodeD; root.leftChild.rightChild=newNodeE; root.rightChild.rightChild=newNodeF; } public boolean isEmpty(){ return root==null; } //树的高度 public int height(){ return height(root); } //节点个数 public int size(){ return size(root); } private int height(TreeNode subTree){ if(subTree==null) return 0;//递归结束:空树高度为0 else{ int i=height(subTree.leftChild); int j=height(subTree.rightChild); return (i<j)?(j+1):(i+1); } } private int size(TreeNode subTree){ if(subTree==null){ return 0; }else{ return 1+size(subTree.leftChild) +size(subTree.rightChild); } } //返回双亲结点 public TreeNode parent(TreeNode element){ return (root==null|| root==element)?null:parent(root, element); } public TreeNode parent(TreeNode subTree,TreeNode element){ if(subTree==null) return null; if(subTree.leftChild==element||subTree.rightChild==element) //返回父结点地址 return subTree; TreeNode p; //现在左子树中找,如果左子树中没有找到,才到右子树去找 if((p=parent(subTree.leftChild, element))!=null) //递归在左子树中搜索 return p; else //递归在右子树中搜索 return parent(subTree.rightChild, element); } public TreeNode getLeftChildNode(TreeNode element){ return (element!=null)?element.leftChild:null; } public TreeNode getRightChildNode(TreeNode element){ return (element!=null)?element.rightChild:null; } public TreeNode getRoot(){ return root; } //在释放某个结点时,该结点的左右子树都已经释放, //所以应该采用后续遍历,当访问某个结点时将该结点的存储空间释放 public void destroy(TreeNode subTree){ //删除根为subTree的子树 if(subTree!=null){ //删除左子树 destroy(subTree.leftChild); //删除右子树 destroy(subTree.rightChild); //删除根结点 subTree=null; } } public void traverse(TreeNode subTree){ System.out.println("key:"+subTree.key+"--name:"+subTree.data);; traverse(subTree.leftChild); traverse(subTree.rightChild); } //前序遍历 public void preOrder(TreeNode subTree){ if(subTree!=null){ visted(subTree); preOrder(subTree.leftChild); preOrder(subTree.rightChild); } } //中序遍历 public void inOrder(TreeNode subTree){ if(subTree!=null){ inOrder(subTree.leftChild); visted(subTree); inOrder(subTree.rightChild); } } //后续遍历 public void postOrder(TreeNode subTree) { if (subTree != null) { postOrder(subTree.leftChild); postOrder(subTree.rightChild); visted(subTree); } } //前序遍历的非递归实现 public void nonRecPreOrder(TreeNode p){ Stack<TreeNode> stack=new Stack<TreeNode>(); TreeNode node=p; while(node!=null||stack.size()>0){ while(node!=null){ visted(node); stack.push(node); node=node.leftChild; } while(stack.size()>0){ node=stack.pop(); node=node.rightChild; } } } //中序遍历的非递归实现 public void nonRecInOrder(TreeNode p){ Stack<TreeNode> stack =new Stack<BinaryTree.TreeNode>(); TreeNode node =p; while(node!=null||stack.size()>0){ //存在左子树 while(node!=null){ stack.push(node); node=node.leftChild; } //栈非空 if(stack.size()>0){ node=stack.pop(); visted(node); node=node.rightChild; } } } //后序遍历的非递归实现 public void noRecPostOrder(TreeNode p){ Stack<TreeNode> stack=new Stack<BinaryTree.TreeNode>(); TreeNode node =p; while(p!=null){ //左子树入栈 for(;p.leftChild!=null;p=p.leftChild){ stack.push(p); } //当前结点无右子树或右子树已经输出 while(p!=null&&(p.rightChild==null||p.rightChild==node)){ visted(p); //纪录上一个已输出结点 node =p; if(stack.empty()) return; p=stack.pop(); } //处理右子树 stack.push(p); p=p.rightChild; } } public void visted(TreeNode subTree){ subTree.isVisted=true; System.out.println("key:"+subTree.key+"--name:"+subTree.data);; } /** * 二叉树的节点数据结构 * @author WWX */ private class TreeNode{ private int key=0; private String data=null; private boolean isVisted=false; private TreeNode leftChild=null; private TreeNode rightChild=null; public TreeNode(){} /** * @param key 层序编码 * @param data 数据域 */ public TreeNode(int key,String data){ this.key=key; this.data=data; this.leftChild=null; this.rightChild=null; } } //测试 public static void main(String[] args) { BinaryTree bt = new BinaryTree(); bt.createBinTree(bt.root); System.out.println("the size of the tree is " + bt.size()); System.out.println("the height of the tree is " + bt.height()); System.out.println("*******(前序遍历)[ABDECF]遍历*****************"); bt.preOrder(bt.root); System.out.println("*******(中序遍历)[DBEACF]遍历*****************"); bt.inOrder(bt.root); System.out.println("*******(后序遍历)[DEBFCA]遍历*****************"); bt.postOrder(bt.root); System.out.println("***非递归实现****(前序遍历)[ABDECF]遍历*****************"); bt.nonRecPreOrder(bt.root); System.out.println("***非递归实现****(中序遍历)[DBEACF]遍历*****************"); bt.nonRecInOrder(bt.root); System.out.println("***非递归实现****(后序遍历)[DEBFCA]遍历*****************"); bt.noRecPostOrder(bt.root); } }
转载于:https://www.cnblogs.com/yzjT-mac/p/5872881.html
java顺序表和树的实现相关推荐
- JAVA顺序表的简单实现
import java.util.Scanner;class DATA{ //模拟一个班级的学生记录String key;String name;int age;}class SLType{stati ...
- java 顺序表的实现_顺序表的简单实现(Java)
采用Java实现数据结构中的顺序表 /** *Apr 15, 2013 *Copyright(c)JackWang *All rights reserve *@Author JackWang */ p ...
- Java顺序表就地逆置_顺序表的就地逆置问题
问题描述:编写一个顺序表的成员函数,实现对顺序表就地逆置的操作.所谓逆置,就是把(a1,a2,a3,...,an)变成(an,an-1,..,a2,a1):所谓就地,即逆置后的数据元素仍在原来顺序表的 ...
- java顺序表增删查改_Java实现顺序表的增删改查
public class MyArrayList { private int[] array; //代表的是存在数据的数组 //array.length代表的是数组的容量 private int ...
- java顺序表冒泡排序_冒泡排序就这么简单 - Java3y的个人空间 - OSCHINA - 中文开源技术交流社区...
冒泡排序就这么简单 在我大一的时候自学c语言和数据结构,我当时就接触到了冒泡排序(当时使用的是C语言编写的).现在大三了,想要在暑假找到一份实习的工作,又要回顾一下数据结构与算法的知识点了. 排序对我 ...
- Java顺序表 实现扑克牌游戏简单 (梭哈 / 斗牛)
简单的扑克牌游戏 梭哈: 梭哈用的是扑克牌共52张牌,因为不容易出好牌,也有去掉 234567 的简易玩法,规则玩法花样很多. 在这里我们采用:52 张牌,3 个人,一人 5 张牌,按规则 ...
- 中国近代史以内最详细的JAVA顺序表
public class MyArrayList {public long[] array; //存放数据private int size; //元素个数public MyArrayList(){ar ...
- Java数据结构(1)---顺序表
以下程序在JDK1.5.0_05环境下调试通过,程序分3个文件,放在同一目录下 //List.java 顺序表抽象数据类型的接口定义 public interface List { publ ...
- Java中的查找树和哈希表(一级)
下面我们来看一下JAVA中有哪些查找树和哈希表,我们分两块内容来讲呗,第一块我们首先来讲查找树,第二块我们来讲哈希表,JAVA里面我们有一个TreeSet,还有一个TreeMap,他们底层都是使用了红 ...
- 【数据结构与算法】顺序表V3.0的Java实现
更新说明 经过了顺序表V1.0的根据接口编写成型.顺序表V2.0的功能大幅度增强,这里推出了顺序表V3.0,功能的丰富性不及V2.0,但加入了迭代器,代码的编写也更加的合理了,应该说是比较好的作品了. ...
最新文章
- 3层-CNN卷积神经网络预测MNIST数字
- BMC:幼年特发性关节炎患儿肠道菌群的特征、生物标记的识别及其在临床预测中的作用...
- 根据名字,获取线程,进程。
- CorelDraw技巧|设计师要了解数位板怎么用
- net的retrofit--WebApiClient库
- CISCO认证涨价了
- 阿里云服务器安装docker开发环境
- C/C++链接过程相关
- SocksCap代理
- 【Pix4d精品教程】Pix4d空三后处理:点云分类与过滤、DSM精编生成DEM、生成等高线案例详解
- Excel如何根据身份证号码计算年龄
- ubuntu上传代码文件到github
- w3wp ash oracle,巧妙使用ASH信息
- Uncaught SyntaxError: The requested module ‘/node_modules/.vite/vue.js?v=bd1817bb‘ does not provide
- 【译】 WebP 支持:超出你想象
- opencv中findContours 和drawContours画图函数
- 工欲善其事,必先利其器(Eclipse篇)
- RSI指标使用技巧和参数设置
- 计算机网络与英语教学,计算机网络与大学英语教学整合探析
- 博客——使用 Redis 实现博客编辑的自动保存草稿功能
热门文章
- php处理post序列化,使用jQuery POST和php序列化和提交表单
- lisp方格网法计算土方量_飞时达土方软件多级边坡土方量计算(选方格点放坡)...
- Unity Bug解决分析思路
- GC Roots 是什么?哪些对象可以作为 GC Root
- filebeat配置介绍
- windows自动导出oracle数据库,Oracle数据库的自动导出备份脚本(windows环境)
- 【渝粤教育】广东开放大学 供应链与物流管理 形成性考核 (56)
- 数据预处理第1讲:标准化
- 【Nature论文浅析】基于模型的AlphaGo Zero
- OpenCV轮廓vectorvector