最常见数据结构
1.栈(先进后出,后进先出)
package stack;
public class Mystack {
private long[] arr;
private int top;
public Mystack(){
arr = new long [10];
top = -1;
}
public Mystack(int maxSize){
arr = new long[maxSize];
top = -1;
}
public void push(int value){
arr[++top] = value;
}
public long pop(){
return arr[top--];
}
public long peek(){
return arr[top];
}
public boolean isEmpty(){
return top==-1;
}
public boolean isFull(){
return top == arr.length-1;
}
}
2.队列(先进先出)
package Queue;
public class MyQueue {
private long[]arr;
private int elements;//有效数据大小
private int front;//队头
private int end;//队尾
public MyQueue(){
arr = new long[10];
elements = 0;
front = 0;
end = -1;
}
public MyQueue(int maxSize){
arr = new long[maxSize];
elements = 0;
front = 0;
end = -1;
}
//从队尾插入
public void insert(long value){
arr[++end] = value;
elements++;
}
public long remove() {
elements--;
return arr[front++];
}
/**
* 查看数据,从队头查看
*/
public long peek() {
return arr[front];
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return elements == 0;
}
/**
* 判断是否满了
*/
public boolean isFull() {
return elements == arr.length;
}
}
3.循环队列(先进先出)
package Queue;
public class MyCycleQueue {
private long[]arr;
private int elements;//有效数据大小
private int front;//队头
private int end;//队尾
public MyCycleQueue(){
arr = new long[10];
elements = 0;
front = 0;
end = -1;
}
public MyCycleQueue(int maxSize){
arr = new long[maxSize];
elements = 0;
front = 0;
end = -1;
}
//从队尾插入
public void insert(long value){
if(end == arr.length -1){
end = -1;
}
arr[++end] = value;
elements++;
}
public long remove() {
long value = arr[front++];
elements--;
if(front == arr.length){
front = 0;
}
return value;
}
/**
* 查看数据,从队头查看
*/
public long peek() {
return arr[front];
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return elements == 0;
}
/**
* 判断是否满了
*/
public boolean isFull() {
return elements == arr.length;
}
}
4.链表
节点Node
package LinkList;
public class Node {
public long data;//数据域
public Node next;//指针域
public Node(long value){
this.data = value;
}
public void display(){
System.out.print(data+"");
}
}
链表LinkList
package LinkList;
public class LinkList {
/**
* 链表
*/
//头节点
private Node first;
public LinkList(){
first = null;//可以不做,编译器默认为null
}
/**
*
* 插入节点,在头节点插入
*/
public void insertFirst(long value){
Node node = new Node(value);
node.next = first;
first = node;
}
/**
* 删除一个结点,在头结点后进行删除
*/
public Node deleteFirst() {
Node tmp = first;
first = tmp.next;
return tmp;
}
/**
* 显示方法
*/
public void display() {
Node current = first;
while (current != null) {
current.display();
current = current.next;
}
}
/**
* 查找方法
*/
public Node find(long value){
Node current = first;
while(current.data != value){
if(current.next == null){
return null;
}
current = current.next;
}
return current;
}
public Node delete(long value){
Node current = first;
Node previous = first;
while (current.data!=value){
if(current.next == null){
return null;
}
previous = current;
current = current.next;
}
//找到了
if(current == first){
first = first.next;
}
else{
previous.next = current.next;
}
return current;
}
}
5.双向链表
package LinkList.doublyLinklist;
public class Node {
public long data;//数据域
public Node next;//后一个node
public Node previous;//前一个node
public Node(){}
public Node(long value){
this.data = value;
}
public void display(){
System.out.print(data+"");
}
}
package LinkList.doublyLinklist;
public class Node {
public long data;//数据域
public Node next;//后一个node
public Node previous;//前一个node
public Node(){}
public Node(long value){
this.data = value;
}
public void display(){
System.out.print(data+"");
}
}
package LinkList.doublyLinklist;
public class LinkList {
private Node head;//头节点
private Node tail;//尾节点
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
public LinkList(){
head = null;
tail = null;
}
//插入一个节点,在头节点插入
public void insert(long value){
Node node = new Node(value);
if(isEmpty()){
head = node;
tail = node;
head.next = null;
head.previous = null;
}
else{
head.next = node;
node.previous = head;
head = node;
head.next = null;
}
}
//从头部删除
public Node delete(){
Node node = new Node();
if(head.next == null && head.previous ==null){
head = null;
tail = null;
return null;
}
else {
node = head;
head = head.previous;
head.next.previous =null;
head.next = null;
return node;
}
}
public boolean isEmpty(){
return (head == null);
}
public void display() {
if (!isEmpty()) {
head = tail;
if (head.next == null && head.previous == null) {
System.out.println(head.data);
}else {
while (head != null) {
System.out.print(head.data + " ");
if(head.next!=null)
head = head.next;
else break;
}
}
} else {
System.out.println("empty");
}
}
}
6.循环链表
循环链表是将普通链表的头节点的后续指针指向尾,把尾节点的前驱指针指向头节点。
7.双向循环链表
双向循环链表是将双向链表的头节点的后续指针指向尾,把尾节点的前驱指针指向头节点。
8.二叉树
背景:有序数组插入数据项和删除数据项太慢,链表查询速度太慢
树中能非常快速的查找数据项、插入数据项跟删除数据项
前序遍历:
1.访问根节点
2.前序遍历左子树
3.前序遍历右子树
/**
* 前(先)序遍历
*/
public void preOrder(Node node){
if(node != null) {
System.out.print(node.getData()+" ");
preOrder(node.getLeftChild());
preOrder(node.getRightChild());
}
}
中序遍历:
1.中序遍历左子树
2.访问根节点
3.前序遍历右子树
/**
* 中序遍历
*/
public void inOrder(Node node){
if(node != null) {
inOrder(node.getLeftChild());
System.out.print(node.getData()+" ");
inOrder(node.getRightChild());
}
}
后序遍历:
1.后序遍历左子树
2.后序遍历右子树
3.访问根节点
/**
* 后续遍历
*/
public void postOrder(Node node){
if(node != null) {
postOrder(node.getLeftChild());
postOrder(node.getRightChild());
System.out.print(node.getData()+" ");
}
}
二叉树的基本构造以及其操作
节点构造:
package tree.binaryTree;
public class Node {
private int data;
private Node leftChild;//左子节点
private Node rightChild;//右子节点
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public Node(int data) {
this.data = data;
}
public Node() {
}
}
二叉树构造:
public class BinaryTree {
private Node root;//根节点
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
}
插入二叉树的节点:原则:左边比右边小
public void insert(int value){//构造二叉搜索树,二叉排序树,
Node newNode = new Node(value);
Node parent;
Node current = root;
if (current == null){
root = newNode;
return;
}
else{
while (true){
parent = current;
if(current.getData() >= value){
if(current.getLeftChild() != null){
current = current.getLeftChild();
}
else {
current.setLeftChild(newNode);
return;
}
}
else {
if(current.getRightChild() != null){
current = current.getRightChild();
}
else {
current.setRightChild(newNode);
return;
}
}
}
}
}
查找节点
public Node find(int value){
Node current = root;
while (current.getData()!= value){
if (current.getData()>value){
current = current.getLeftChild();
}
else{
current = current.getRightChild();
}
if (current == null){
return null;
}
}
return current;
}
删除二叉树的节点:
1.该节点是叶子节点
只需要改变该节点父节点的引用值,设置其为null就行了
2.该节点有一个子节点
改变父节点的引用,将其直接指向要删除节点的子节点
3.该节点有两个子节点
需要使用他的中序后继来替代该节点。
中序后继:该节点的右子树节点的左子树的左子树的左子树的最后一个左节点,如果该节点只有一个右节点,则为该右节点
public void delete(int value){
Node current = root;
Node parent = root;
while (current.getData()!= value){
if (current.getData()>value){
parent = current;
current = current.getLeftChild();
}
else{
parent = current;
current = current.getRightChild();
}
if (current == null){
System.out.println("该节点不存在,无法删除");
}
}
//叶子节点
if(current.getLeftChild()==null && current.getRightChild() == null){
if (current == parent.getLeftChild()){
parent.setLeftChild(null);
}
else{
parent.setRightChild(null);
}
}
//该节点右节点为空,左节点不为空
else if(current.getLeftChild()!=null&¤t.getRightChild() == null){
if(current == parent.getLeftChild()){
parent.setLeftChild(current.getLeftChild());
current.setLeftChild(null);
}
else{
parent.setRightChild(current.getLeftChild());
current.setLeftChild(null);
}
}
//该节点左节点为空,右节点不为空
else if (current.getLeftChild()==null&& current.getRightChild()!=null){
if(current == parent.getLeftChild()){
parent.setLeftChild(current.getRightChild());
current.setRightChild(null);
}
else{
parent.setRightChild(current.getRightChild());
current.setRightChild(null);
}
}
//该节点左右节点均不为空
else{
if(current.getRightChild().getLeftChild() == null){
if (current == parent.getLeftChild()){
parent.setLeftChild(current.getRightChild());
}
else{
parent.setRightChild(current.getRightChild());
}
current.getRightChild().setLeftChild(current.getLeftChild());
}
else{
Node node = current.getRightChild();
Node node1 = node;
while (node.getLeftChild() != null){
node1 = node;
node = node.getLeftChild();
}
if (parent.getRightChild() == current){
parent.setRightChild(node);
}
else{
parent.setLeftChild(node);
}
node1.setLeftChild(node.getRightChild());
node.setLeftChild(current.getLeftChild());
node.setRightChild(current.getRightChild());
}
}
}