`
jayghost
  • 浏览: 429217 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Java实现Stack、Queue、BinaryTree

    博客分类:
  • Java
 
阅读更多
1、用数组实现Stack:
public class MyStack {
	private int maxSize;
	private Integer[] array;
	private int top;

	public MyStack(int maxSize) {
		this.maxSize = maxSize;
		this.array = new Integer[maxSize];
		this.top = -1;
	}

	public void push(int item) {
		if (isFull()) {
			System.out.println("Stack is full");
		} else {
			array[++top] = item;
		}
	}

	public Integer peek() {
		if (isEmpty()) {
			System.out.println("Stack is empty");
			return null;
		} else {
			return array[top];
		}
	}

	public Integer pop() {
		if (isEmpty()) {
			System.out.println("Stack is empty");
			return null;
		} else {
			return array[top--];
		}
	}

	public boolean isEmpty() {
		return top == -1;
	}

	public boolean isFull() {
		return top == maxSize - 1;
	}

	public static void main(String[] args) {
		MyStack myStack = new MyStack(10);
		myStack.push(1);
		myStack.push(3);
		System.out.println("peek: " + myStack.peek());
		myStack.push(5);
		System.out.println("pop: " + myStack.pop());
		System.out.println("peek: " + myStack.peek());
		myStack.push(6);
		System.out.println("peek: " + myStack.peek());
		System.out.println("pop: " + myStack.pop());
		System.out.println("pop: " + myStack.pop());
		System.out.println("pop: " + myStack.pop());
		System.out.println("pop: " + myStack.pop());
	}
}


2、用数组实现Queue:
public class MyQueue {
	private int maxSize;
	private Integer[] array;
	private int front;
	private int rear;
	private int itemCount;

	public MyQueue(int maxSize) {
		this.maxSize = maxSize;
		this.array = new Integer[maxSize];
		this.front = 0;
		this.rear = -1;
		this.itemCount = 0;
	}

	public void insert(int item) {
		if (isFull()) {
			System.out.println("Queue is full");
		} else {
			if (rear == maxSize - 1) {
				rear = -1;
			}
			array[++rear] = item;
			itemCount++;
		}
	}

	public Integer remove() {
		if (isEmpty()) {
			System.out.println("Queue is empty");
			return null;
		} else {
			int temp = array[front++];
			if (front == maxSize) {
				front = 0;
			}
			itemCount--;
			return temp;
		}
	}

	public Integer peek() {
		if (isEmpty()) {
			System.out.println("Queue is empty");
			return null;
		} else {
			return array[front];
		}
	}

	public boolean isEmpty() {
		return itemCount == 0;
	}

	public boolean isFull() {
		return itemCount == maxSize;
	}

	public static void main(String[] args) {
		MyQueue myQueue = new MyQueue(5);
		myQueue.insert(1);
		myQueue.insert(2);
		myQueue.insert(3);
		myQueue.insert(4);
		myQueue.insert(5);
		myQueue.insert(6);
		System.out.println("peek: " + myQueue.peek());
		myQueue.insert(5);
		System.out.println("remove: " + myQueue.remove());
		System.out.println("peek: " + myQueue.peek());
		myQueue.insert(6);
		System.out.println("peek: " + myQueue.peek());
		System.out.println("remove: " + myQueue.remove());
		System.out.println("remove: " + myQueue.remove());
		System.out.println("peek: " + myQueue.peek());
		System.out.println("remove: " + myQueue.remove());
		System.out.println("remove: " + myQueue.remove());
	}
}


3、递归实现BinaryTree:
public class MyBinaryTree {

	private static class Node {
		int data;
		Node left;
		Node right;

		public Node(int data) {
			this.data = data;
			this.left = null;
			this.right = null;
		}
	}

	public Node root;

	public MyBinaryTree() {
		root = null;
	}

	private Node insert(Node node, int data) {
		if (node == null) {
			node = new Node(data);
		} else {
			if (data <= node.data) {
				node.left = insert(node.left, data);
			} else {
				node.right = insert(node.right, data);
			}
		}
		return node;
	}

	public void insert(int data) {
		root = insert(root, data);
	}

	public void insert(int[] data) {
		for (int i : data) {
			insert(i);
		}
	}

	public void preOrder(Node node) {
		if (node == null) {
			return;
		}
		System.out.println(node.data);
		preOrder(node.left);
		preOrder(node.right);
	}

	public void inOrder(Node node) {
		if (node == null) {
			return;
		}
		preOrder(node.left);
		System.out.println(node.data);
		preOrder(node.right);
	}

	public void postOrder(Node node) {
		if (node == null) {
			return;
		}
		preOrder(node.left);
		preOrder(node.right);
		System.out.println(node.data);
	}

	public static void main(String[] args) {
		MyBinaryTree binaryTree = new MyBinaryTree();
		int[] data = { 4, 8, 7, 2, 9, 3, 1, 6, 5 };
		binaryTree.insert(data);
		System.out.println("preOrder:");
		binaryTree.preOrder(binaryTree.root);
		System.out.println("inOrder:");
		binaryTree.inOrder(binaryTree.root);
		System.out.println("postOrder:");
		binaryTree.postOrder(binaryTree.root);
	}
}

分享到:
评论

相关推荐

    Java超详细!Java实现数据结构PPT课件

    二叉树(BinaryTree)、二叉搜索树(BinarySearchTree、BST) 平衡二叉搜索树(BalancedBinarySearchTree、BBST) AVL树(AVLTree)、红黑树(RebBlackTree) B树(B-Tree) 集合(TreeSet)、映射(TreeMap) ...

    cpp-算法精粹

    Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary Search Tree Convert Sorted Array to Binary Search Tree ...

    数据结构常用算法c++实现

    Binary search tree Dynamic order statistics Red-black tree Interval tree Prefix Tree(Trie) *Suffix Tree(未实现)* B-Tree Hash by multiplication Hash table Universal hash function Perfect hash Java's ...

    普林斯顿算法课程 Robert Sedgewick

    insertion sort, shell sort, quick sort, merge sort, heap sort), searching algorithms (Binary search tree, Red-Black BST, hashing), 所以程序都用java实现,代码风格简洁,很值得学习。

    javalruleetcode-Algorithms:数据结构和算法

    java lru leetcode Algorithms 为什什么要学习算法与数据结构 编程的内功修炼 去国内一流互联网公司的必要条件 硅谷互联网公司面试更是要求当场写算法题目 算法和数据结构是有趣且实用的 Data Structure Array Stack...

    数据结构Advanced-Data-Structures

    Self-balancing binary search tree 180 Tree rotation 182 Weight-balanced tree 185 Threaded binary tree 186 AVL tree 191 Red-black tree 195 AA tree 210 Scapegoat tree 215 Splay tree 219 T-tree 234 Rope ...

    目前最火最热门的python经典编程题之1

    9.用两个栈实现队列 Stack,Queue 关注 9.两个队列实现栈 Stack,Queue 关注 10.斐波那契数列 Dynamic Programming, 10.跳台阶 Dynamic Programming, 10.变态跳台阶 Dynamic Programming,Math 10.矩形覆盖 Dynamic ...

    leetcode双人赛-js-structures:JS结构体问题及解决方法

    Queue - 使用双链表的队列实现 [] Stack - 自定义 JS 堆栈实现 [] Graph - JS Graph 实现 [] Binary Tree - JS 使用树节点的二叉树实现 [] Binary Search Tree - JS 二叉搜索树(继承)[] Min Heap - JS 最小堆,...

    Java 9 Data Structures and Algorithms

    Get a grasp on the basics of abstract data types—stack, queue, and double ended queue See how to use recursive functions and immutability while understanding and in terms of recursion Handle reactive...

    javalruleetcode-algorithm-java:leetcode刷题

    java lru leetcode ...Tree/BinaryTree BST HashTable Disjoint Set Trie BloomFilter LRU Cache Algorithm General Coding InOrder/PreOrder/PostOrder Traversal Greedy Recursion/Backtrace Breadth-fi

    javaarraylist源码-Data-Structures-Algorithms:使用Java编程语言的数据结构和算法源代码(包括Stac

    java arraylist源码数据结构算法 使用Java编程语言的数据结构和算法源代码(包括Stack,LinkedList,ArrayList,Queue和Binary Tree)

    Java中的数据结构:Java实现中的详细数据结构

    Levent Divilioglu-2017年夏季待办事项: 附录将在此自述文件中创建DS_011软件包:BTree实现将完成DS_011软件包:AVLTree的实现将完成DS_012套装:骑士之旅将被展示DS_013套件:图表将完成DS_013封装:实施广度优先...

    几种基本数据结构

    学数据结构的时候自己写的几种基本数据结构,包括binary tree,circular queueu,linked list,linked queue,linked stack,sequence list,sequence stack

    basic-java-data-structures:多种(基本)数据结构,以简单易读的Java实现

    基本的Java数据结构用简单易读的Java实现的各种(基本)数据结构。介面堆栈接口队列接口树木二进制搜索树AVL树最小堆基本/各种数据结构链表哈希表队列(ArrayQueue,ListQueue) 堆栈(ArrayListStack,ListStack)...

    leetcode卡-LeetCode:我的LeetCode解决方案

    leetcode卡 LeetCode 记录一下再LeetCode上刷的题,坚持每天刷一道...Tree/stack 2017.06.20 打卡[LeetCode 107. Binary Tree Level Order Traversal II], Tree/BFS 2017.06.20 打卡[LeetCode 324. Wiggle Sort II], S

    Learning.JavaScript.Data.Structures.and.Algorithms.1783554878

    This book begins by covering the basics of the JavaScript language and then moves on to discuss the most important data structures such as array, queue, stack, and linked list. You will also gain an ...

    javalruleetcode-leetcode:更多信息请访问Gitbook:https://wentao-shao.gitbook.io/

    java lru leetcode LeetCode 记录数据结构与算法/LeetCode练习过程,将持续更新 Best Time To Buy And Sell Stock [Binary Tree](Basic-Knowledge/Binary tree.md) Serialize And Deserialize Bit 1.position 2....

    Data.Structures.and.Algorithms.USING.C

    STACK & QUEUE 13. Stack 14. Expression Parsing 15. Queue SEARCHING TECHNIQUES 16. Linear Search 17. Binary Search 18. Interpolation Search 19. Hash Table SORTING TECHNIQUES 20. Sorting Algorithm 21....

    数据结构:数据结构-C ++实现

    数据结构数据结构-C ++实现C ++模板实现常用数据结构,包括向量,链表,栈,数量,二叉树,二叉搜索树,平衡二叉树,优先权(堆),图,排序...二叉树二叉树节点:BinaryTreeNode.h头文件:BinaryTree.h测试函数:binar

    JS-DataStructure:使用CodeSandbox创建

    :rocket: JS数据结构 用CodeSandbox创建 数据结构Icerik 链接列表(Bagli ... 3 Binary Tree * Binary Tree * BST * AVL, Red Black vs. vs.4堆 6.Graph * MST * Shortes Path * Max Flow 7.Hash 9,Matrisler

Global site tag (gtag.js) - Google Analytics