二叉树链表存储、链表、栈、堆的结构实现(Java)

目录

二叉树的实现

链表的实现

栈的实现

        栈的数组实现

        栈的链表实现

队列的实现

        队列的数组实现

        队列的集合实现

        两个栈实现队列


二叉树的实现

package binarytree;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 21:06
 */

public class TreeNode {
    public int value;
    public TreeNode leftTreeNode;
    public TreeNode rightTreeNode;

    public TreeNode(int value) {
        this.value = value;
    }

    public String toString() {
        return "TreeNode{value=" + this.value + ", leftTreeNode=" + this.leftTreeNode + ", rightTreeNode=" + this.rightTreeNode + '}';
    }
}
package binarytree;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 21:09
 */

public class Test {

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(5);
        TreeNode node2 = new TreeNode(4);
        TreeNode node3 = new TreeNode(7);
        TreeNode node4 = new TreeNode(6);
        node1.leftTreeNode = node2;
        node1.rightTreeNode = node3;
        node3.leftTreeNode = node4;
        System.out.println(node1);
    }
}

测试结果

TreeNode{value=5, leftTreeNode=TreeNode{value=4, leftTreeNode=null, rightTreeNode=null}, rightTreeNode=TreeNode{value=7, leftTreeNode=TreeNode{value=6, leftTreeNode=null, rightTreeNode=null}, rightTreeNode=null}}

Process finished with exit code 0

链表的实现

package link;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 21:13
 */
public class ListNode {
    int value;
    ListNode next;

    public ListNode(int value) {
        this.value = value;
    }

    public String toString() {
        return "ListNode{value=" + this.value + ", next=" + this.next + '}';
    }
}
package link;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 21:13
 */
public class Test {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(5);
        ListNode node3 = new ListNode(9);
        node1.next = node2;
        node2.next = node3;
        System.out.println(node1);
    }
}

测试结果 

 ListNode{value=1, next=ListNode{value=5, next=ListNode{value=9, next=null}}}

Process finished with exit code 0

栈的实现

        栈的数组实现

package stack;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 21:18
 */
public class StackArray<E> {
    private E[] arr = (E[])(new Object[12]);
    private int flag = 0;

    public StackArray() {
    }

    public void add(E x) {
        this.arr[this.flag] = x;
        ++this.flag;
        if (flag == arr.length) { //扩容之后
            E[] brr = (E[])(new Object[arr.length * 2]);

            for(int i = 0; i < flag; ++i) {
                arr[i] = brr[i];
            }

            arr = brr;
            System.out.println("栈已满,不能放入");
        }
    }

    public E get() {
        if (flag == 0) {
            System.out.println("栈已空");
            return null;
        } else {
            E result = arr[flag - 1];
            --flag;
            return result;
        }
    }
}
package stack;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 21:20
 */
public class TestArray {
    public static void main(String[] args) {
        StackArray<Integer> x1 = new StackArray();
        Integer i;
        for(i = 0; i < 10; i = i + 1) {
            x1.add(i);
        }

        for(i = 0; i < 15; i = i + 1) {
            System.out.print(x1.get() + " ");
        }
    }


}

 9 8 7 6 5 4 3 2 1 0 栈已空
null 栈已空
null 栈已空
null 栈已空
null 栈已空
null 
Process finished with exit code 0

        栈的链表实现

package stack;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:50
 */
public class StackNode<T> {
    class Node<T> {
        private T t;
        private Node next;
    }

    private Node<T> head;

    //构造函数初始化头指针
    StackLink() {
        head = null;
    }

    //入栈
    public void push(T t) {
        if (t == null) {
            throw new NullPointerException("参数不能为空");
        }
        if (head == null) {
            head = new Node<T>();
            head.t = t;
            head.next = null;
        } else {
            Node<T> temp = head;
            head = new Node<>();
            head.t = t;
            head.next = temp;
        }
    }

    //出栈
    public T pop() {
        T t = head.t;
        head = head.next;
        return t;
    }
}
package stack;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:53
 */
public class TestLink {
    public static void main(String[] args) {
        StackNode stack = new StackNode();
        stack.push("useful");
        stack.push("is ");
        stack.push("Java ");
        System.out.print(stack.pop());
        System.out.print(stack.pop());
        System.out.print(stack.pop());
    }
}

 Java is useful
Process finished with exit code 0

        两个队列实现一个栈

package datastructure;

import java.util.LinkedList;

/**
 * @description:
 * @author: gyj
 * @date: 2022/6/7 21:42
 */
public class StackTest {
    LinkedList<Integer> queue1 = new LinkedList<Integer>();
    LinkedList<Integer> queue2 = new LinkedList<Integer>();

    public void push(int value){
        queue1.addLast(value);
    }

    public int pop() {
        if (sSize() != 0) {
            if (!queue1.isEmpty()) {
                putN_1ToAnother();
                return queue1.removeFirst();
            } else {
                putN_1ToAnother();
                return queue2.removeFirst();
            }
        } else {
            System.out.println("栈已空,不能出栈");
            return -1;
        }

    }

    public int sSize() {
        return queue1.size() + queue2.size();
    }

    public void putN_1ToAnother()//从非空中出队n-1个到另一个队列
    {
        if (!queue1.isEmpty()) {
            while (queue1.size() > 1) {
                queue2.addLast(queue1.removeFirst());
            }
        } else if (!queue2.isEmpty()) {
            while (queue2.size() > 1) {
                queue1.addLast(queue2.removeFirst());
            }
        }
    }

    public static void main(String[] args) {
        StackTest stack = new StackTest();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        stack.push(5);
        stack.push(6);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}

4
3
6
5
2
1
栈已空,不能出栈
-1

Process finished with exit code 0

队列的实现

        队列的数组实现

package queue;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:02
 */
public class QueueArray {

    int[] a = new int[5];
    int i = 1; //数组下标

    //入队
    public void in(int m) {
        a[i++] = m;
    }

    //出队
    public int out() {
        int index = 0;
        int temp = a[1];
        for (int j = 1; j < i; j++) {
            a[j - 1] = a[j];
            index++;
        }
        i = index;
        return temp;
    }
}
package queue;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:05
 */
public class TestArray {
    public static void main(String[] args) {
        //测试队列
        System.out.println("数组测试队列:");
        QueueArray queue = new QueueArray();
        queue.in(1);
        queue.in(2);
        queue.in(3);
        System.out.println(queue.out());
        System.out.println(queue.out());
        queue.in(4);
        System.out.println(queue.out());
        System.out.println(queue.out());
        queue.in(5);
        System.out.println(queue.out());
    }
}

测试结果 

 数组实现测试队列:
1
2
3
4
5

Process finished with exit code 0

        队列的集合实现

package queue;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:05
 */
public class QueueList {
    List<Integer> list = new ArrayList<Integer>();
    int index = 0;  //下标

    //入队
    public void in(int n){
        list.add(n);
        index++;
    }

    //出队
    public int out(){
        if(!list.isEmpty()){
            index--;
            return list.remove(0);
        }
        return -1;
    }
}
package queue;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:06
 */
public class TestList {
    public static void main(String[] args) {
        //测试队列
        System.out.println("集合实现测试队列:");
        QueueList queue = new QueueList();
        queue.in(1);
        queue.in(2);
        queue.in(3);
        System.out.println(queue.out());
        System.out.println(queue.out());
        queue.in(4);
        System.out.println(queue.out());
        System.out.println(queue.out());
        queue.in(5);
        System.out.println(queue.out());
    }
}

测试结果 

集合实现测试队列:
1
2
3
4
5

Process finished with exit code 0

        两个栈实现队列


   队列的主要操作有两个入队操作和出队操作,先进先出
   入队的操作和入栈的操作类似,而出队操作则是先进去的元素先出队,而栈则是栈顶元素先出,即后入栈的先出栈
   假设两个栈A和栈B,A主要用来处理入队操作,B用于处理出队操作。入队操作和入栈操作类似,
  直接将元素压入栈即可。出队的时候,实现我们假设栈B为空,则要把栈A的第一个元素(即栈底元素)弹出,需要将A中元素逆转过来,所以要先把栈A的元素全部出栈,并按顺序压入栈B中,这样每次栈B弹出的栈顶元素就是栈A相对应的栈底元素,就是出队操作。
  若B不为空,则代表之前从A复制过来的元素还没有完全出栈,要出栈的时候直接弹出即可。若栈B的元素都弹出来了,就需要从A中补充。
    
   入队:将元素压入栈A
   出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;如果不为空,栈B将剩余元素出栈。

package queue;


import java.util.Stack;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:05
 */
public class QueueStack {
    Stack<Integer> stackA = new Stack<Integer>();
    Stack<Integer> stackB = new Stack<Integer>();

    //入队
    public void in(int n) {
        stackA.push(n);
    }

    //出队
    public int out() {
        if(stackB.isEmpty()){
            while (stackA.size() > 0) {
                stackB.push(stackA.pop());
            }
        }
        return stackB.pop();
    }
}
package queue;

/**
 * @description:
 * @author: gyj
 * @date: 2022/3/28 22:09
 */
public class TestStack {
    public static void main(String[] args) {

        System.out.println("两个栈实现一个队列:");
        QueueStack queue = new QueueStack();
        queue.in(1);
        queue.in(2);
        queue.in(3);
        System.out.println(queue.out());
        System.out.println(queue.out());
        queue.in(4);
        System.out.println(queue.out());
        System.out.println(queue.out());
        queue.in(5);
        System.out.println(queue.out());

    }
}

 测试结果

两个栈实现一个队列:
1
2
3
4
5

Process finished with exit code 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赚钱去流浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值