栈,队列和二叉树的实现
来源:互联网 发布:linux安装mysql数据库 编辑:程序博客网 时间:2024/05/18 16:19
1.栈
public class Stack {
private T[] arr = (T[]) new Object[1];
private int n = 0;
public boolean isEmpty(){
return n == 0;
}
public int size(){
return n;
}
public void resize(int max){
T[] temp = (T[]) new Object[max];
for(int i = 0; i < n; i++){
temp[i] = arr[i];
}
arr = temp;
}
public boolean push(T t){
if(n == arr.length){
resize(n*2);
}
arr[n++] = t;
return true;
}
public T pop(){
if(isEmpty()){
//空栈
return null;
}
if(n <= arr.length/4){
//调整栈的大小
resize(arr.length/2);
}
return arr[--n];
}
}
2.队列
public class Queue {
private Node head;
private Node tail;
private int n = 0;
private class Node{
T value;
Node nextNode;
}
public boolean isEmpty(){
return n == 0;
}
public int size(){
return n;
}
public boolean enQueue(T t){
Node oldLast = tail;
tail = new Node();
tail.value = t;
tail.nextNode = null;
if(isEmpty()){
head =tail;
}else{
oldLast.nextNode = tail;
}
n++;
return true;
}
public T deQueue(){
if(isEmpty()){
//队列为空
return null;
}
Node lastHead = head;
head = head.nextNode;
n--;
return lastHead.value;
}
}
3 二叉树
public class TreeNode {
private T value;
private TreeNode leftNode;
private TreeNode rightNode;
public void init(T t){
value = t;
}
public TreeNode findNode(T t){
if(t.equals(value)){
return this;
}
TreeNode ptr = null;
if(leftNode != null){
if((ptr=leftNode.findNode( t)) != null){
return ptr;
}
}
if(rightNode != null){
if((ptr = rightNode.findNode( t)) != null){
return ptr;
}
}
return null;
}
public boolean insert( T t){
TreeNode node = new TreeNode<>();
node.value = t;
if(leftNode == null){
leftNode = node;
return true;
}
if(rightNode == null){
rightNode = node;
return true;
}
return false;
}
public int depth(){
if(leftNode == null && rightNode == null){
return 1;
}
int leftDepth = leftNode.depth();
int rightDepth = rightNode.depth();
return leftDepth > rightDepth ? leftDepth+1 :rightDepth+1;
}
public void levelTree(){
//节点队列
Queue> queue = new Queue<>();
//值队列
Queue valueQueue = new Queue<>();
//根节点的值加入值队列
valueQueue.enQueue(this.value);
//左子节点加入队列
if(leftNode != null){
queue.enQueue(leftNode);
}
//右子节点加入队列
if(rightNode != null){
queue.enQueue(rightNode);
}
while(!queue.isEmpty()){
TreeNode node = queue.deQueue();
if(node.leftNode != null){
queue.enQueue(node.leftNode);
}
if(node.rightNode != null){
queue.enQueue(node.rightNode);
}
valueQueue.enQueue(node.value);
}
while(!valueQueue.isEmpty()){
System.out.println(valueQueue.deQueue());
}
}
public void leftTree(){
if(leftNode != null){
leftNode.leftTree();
}
System.out.println(this.value);
if(rightNode != null){
rightNode.leftTree();
}
}
public void midTree(){
System.out.println(this.value);
if(leftNode != null){
leftNode.midTree();
}
if(rightNode != null){
rightNode.midTree();
}
}
}
阅读全文
0 0
- 栈,队列和二叉树的实现
- 基于栈和队列实现二叉树的遍历
- 队列和堆栈实现二叉树的遍历
- 遍历二叉树——递归和非递归(栈和队列的应用)实现
- 二叉队列的实现
- 二叉树、栈、队列、链表的Java代码实现
- 队列实现二叉树的遍历
- 二叉堆的实现和详解(优先队列的基础)
- 基于二叉堆实现的优先队列和堆排序
- 二叉树遍历之栈和队列
- 二叉树(利用栈和队列实现递归和非递归遍历,构建等)
- 二叉树的序列化和反序列化java借助队列实现
- 用java实现二叉查找树、堆和优先队列
- 二叉树 队列实现 模板
- 数据结构与实现——数组、矩阵、链表、队列、栈、对象、二叉树和红黑树
- 栈和队列的实现
- 栈和队列的实现
- 栈和队列的实现
- 图的创建和深度优先遍历
- 使用openssl的md5库
- Servlet国际化流程(纯干货随笔)
- 让子栏目或文章样式交替出现
- 讲一下《雷神3》
- 栈,队列和二叉树的实现
- Sqoop Flume 数据采集引擎
- hbase 启动报错
- 自动化测试第二课:打开浏览器
- java多线程设计模式之Producer-Consumer模式(一)
- Linux多线程通信
- unity-lua开发环境
- Lengthening Sticks(容斥,组合数学)
- PDO执行execute方法后无任何返回,通过try catch 得到如下异常信息