数据结构
数据结构与算法
程序员阿蛮
阿蛮博客
展开
-
平衡二叉树实现
平衡二叉树是特殊的二叉排序树,代码也类似,只不过在创建、添加节点时需要进行判断。 package src.avl; public class AvlTreeDemo { public static void main(String[] args){ int[] arr = new int[]{10, 11, 7, 6, 8, 9}; AvlTree avl = new AvlTree(); for(int i=0; i < arr.lengt原创 2021-07-27 14:32:34 · 140 阅读 · 0 评论 -
二叉排序树实现
package src.bst; public class BinarySortTreeDemo { public static void main(String[] args){ int[] arr = new int[]{7, 3, 10, 12, 5, 1, 9, 2}; BinarySortTree bst = new BinarySortTree(); for(int i=0;i<arr.length;i++){原创 2021-07-27 10:14:31 · 177 阅读 · 0 评论 -
双向链表(非环形链表,有头节点)的增删改查
public class DoubleLinkedList { private Node headNode = new Node(0,"head"); public Node getHeadNode() { return headNode; } // 1、尾部添加 ---- 与单向链表一样 public void add(Node node){ // 由于不能修改头节点,创建临时节点 Node temp = hea原创 2021-07-09 15:41:35 · 57 阅读 · 0 评论 -
单链表的增删改查
除了尾部添加的方式,其他的增删改都需要找到前一个节点进行操作。 在有头结点的链表中,头节点不能改变。 // 定义一个链表管理节点 class SingleLinkedList { private Node headNode = new Node(0,"head"); // 1、尾部添加 public void add(Node node){ // 由于不能修改头节点,创建临时节点 Node temp = headNode; w.原创 2021-07-09 11:00:23 · 209 阅读 · 0 评论 -
数组实现循环队列
循环队列: 需要预留一个空间,即rear指向的空间,否则无法判断队列为空或者为满 空队列条件:rear == front 满队列条件:( rear + 1 ) % maxSize = front public class CircleArrayQueue { private int maxSize; private int[] queue; private int front; //队首 private int rear; //队尾 /* 说明:原创 2021-07-08 17:17:06 · 213 阅读 · 0 评论 -
数组实现简单队列
对于队列来说,需要队首和队尾指针变量,出队改变队首,入队改变队尾。此外需要队列的大小和存放数据的变量。代码如下: public class ArrayQueue { private int maxSize; private int[] queue; private int front; //队首 private int rear; //队尾 public ArrayQueue(int maxSize){ this.maxSize = maxS原创 2021-07-08 17:08:02 · 121 阅读 · 0 评论 -
数组实现栈
对于栈来说,只有入栈和出栈操作,所以只需要一个栈顶指针即可。另外需要初始化数组的大小和数据存放的空间。代码如下: public class ArrayStack { private int maxSize; private int[] stack; private int top = -1; // 栈指向栈顶元素,初始化为空 public ArrayStack(int maxSize){ this.maxSize = maxSize; th原创 2021-07-08 16:57:26 · 78 阅读 · 0 评论 -
二叉树的先序中序后序遍历
先看一下定义: (1)先序遍历:(根左右) (2)中序遍历:(左根右) (3)后序遍历:(左右根) 下面依次分析:(拿例子分析比较直接) 先序遍历 先来看一下先序遍历:先是根然后左分支然后右分支,这个懂的人都懂,但不懂的人就很迷茫了,感觉这句话不就是说明A在第一个,然后遍历左边再右边吗<博主以前就是这么想的>,其实不是的,下面给大家详细说一下把,至于中序与后序都是大同小异了。 先看一下整体,先根再左分支,然后右分支。说明先是A然后左边分支然后右边分支:A+A的左分支+A的右分支。再把左分支取出转载 2020-12-02 15:24:12 · 1779 阅读 · 0 评论