集合
接口
public interface Set<E> {
void add(E e);
void remove(E e);
boolean contaions(E e);
int getSize();
boolean isEmpty();
}
链表实现集合
/**
* 虚拟头节点
* @param <E>
*/
public class LinkedList<E> {
private class Node{
public E e;
public Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this(e,null);
}
public Node() {
this(null,null);
}
@Override
public String toString() {
return e.toString();
}
}
public Node dummyHead;
private int size;
public LinkedList() {
dummyHead = new Node(null,null);
size = 0;
}
/**
* 判断链表元素个数
* @return
*/
public int getSize(){
return size;
}
/**
* 判断链表是否为空
* @return
*/
public boolean isEmpty(){
return size==0;
}
/**
* 在链表的index(0-based)位置添加新的元素e
* @param e
*/
public void add(int index,E e){
if (index<0 || index > size){
throw new IllegalArgumentException("Add failed .Illegal index.");
}
Node prev = dummyHead;
for (int i = 0; i < index; i++) {
prev = prev.next;
}
// Node node = new Node(e);
// node.next = prev.next;
// prev.next = node;
prev.next = new Node(e,prev.next);
size++;
}
/**
* 在链表头添加元素
* @param e
*/
public void addFirst(E e){
add(0,e);
}
/**
* 在链表尾添加元素
* @param e
*/
public void addLast(E e){
add(size,e);
}
/**
* 根据索引查找
* @param index
* @return
*/
public E get(int index){
if (index<0 || index > size){
throw new IllegalArgumentException("Get failed .Illegal index.");
}
Node cur = dummyHead.next;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
return cur.e;
}
public E getFirst(){
return get(0);
}
public E getLast(){
return get(size-1);
}
/**
* 编辑index处元素
* @param index
* @param e
*/
public void set(int index,E e){
if (index<0 || index > size){
throw new IllegalArgumentException("Set failed .Illegal index.");
}
Node cur = dummyHead.next;
for (int i = 0; i < index; i++) {
cur = cur.next;
}
cur.e = e;
}
/**
* 查找链表中是否存在元素e
* @param e
* @return
*/
public boolean contains(E e){
Node cur = dummyHead.next;
while (cur!=null){
if (cur.e.equals(e) ){
return true;
}
cur = cur.next;
}
return false;
}
/**
* 删除索引为index的节点
* @param index
*/
public E remove(int index){
if (index<0 || index > size){
throw new IllegalArgumentException("Delete failed .Illegal index.");
}
Node prev = dummyHead;
for (int i = 0; i < index; i++) {
prev = prev.next;
}
Node delNode= prev.next;
prev.next = delNode.next;
delNode.next = null;
size--;
return delNode.e;
}
/**
* 删除第一个元素
* @return
*/
public E removeFirst(){
return remove(0);
}
/**
* 删除最后一个元素
* @return
*/
public E removeLast(){
return remove(size-1);
}
/**
* 从链表中删除元素
* @param e
*/
public void removeElement(E e){
Node prev = dummyHead;
while(prev.next != null){
if(prev.next.e.equals(e))
break;
prev = prev.next;
}
if(prev.next != null){
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size --;
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
Node cur = dummyHead.next;
while (cur!=null){
res.append(cur + "->");
cur = cur.next;
}
res.append("NULL");
return res.toString();
}
}
public class LinkedListSet<E> implements Set<E> {
private LinkedList<E> list;
public LinkedListSet() {
this.list = new LinkedList<E>();
}
@Override
public void add(E e) {
if (!list.contains(e)){
list.addFirst(e);
}
}
@Override
public void remove(E e) {
list.removeElement(e);
}
@Override
public boolean contaions(E e) {
return list.contains(e);
}
@Override
public int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
}
二分搜索树实现集合
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BST<E extends Comparable<E>> {
private class Node{
public E e;
public Node left,right;
public Node(E e) {
this.e = e;
this.left = null;
this.right = null;
}
}
private Node root;
private int size;
public BST() {
this.root = null;
this.size = 0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size==0;
}
public void add(E e){
// if (root == null){
// root = new Node(e);
// size++;
// }else{
// Node add = add(root, e);
// }
root = add(root,e);
}
/**
* 向以node为根的二分搜索树中插入元素e,递归算法
* 返回插入的新节点后二分搜索树的根
* @param node
* @param e
*/
private Node add(Node node,E e){
// if (node.e.equals(e)){
// return ;
// }else if(e.compareTo(node.e)<0 && node.left == null){
// node.left = new Node(e);
// size++;
// return;
// }else if (e.compareTo(node.e)>0 && node.right == null){
// node.right = new Node(e);
// size++;
// return;
// }
// if (e.compareTo(node.e)<0){//左节点
// add(node.left,e);
// }else {//右节点
// add(node.right,e);
// }
if (node == null){
size++;
return new Node(e);
}
//如果根节点左右不为空
if (e.compareTo(node.e)<0){//左节点
node.left = add(node.left,e);
}else if (e.compareTo(node.e)>0){//右节点
node.right = add(node.right,e);
}
return node;
}
/**
* 在二分搜索树查找元素
* @param e
* @return
*/
public boolean contains(E e){
return contains(root,e);
}
public boolean contains(Node node,E e){
if (node == null){
return false;
}
if (e.compareTo(node.e) == 0){
return true;
}else if (e.compareTo(node.e)<0){
return contains(node.left,e);
}else{
return contains(node.right,e);
}
}
/**
* 前序遍历二分搜索树
*/
public void preOrder(){
preOrder(root);
}
private void preOrder(Node node) {
if (node == null){
return ;
}
System.out.println(node.e);
preOrder(node.left);
preOrder(node.right);
}
/**
* 前序遍历二分搜索树(非递归)
*/
public void preOrderNR(){
Stack<Node> stack = new Stack<Node>();
stack.push(root);
while (!stack.isEmpty()){
Node cur = stack.pop();
System.out.println(cur.e);
if (cur.right!=null){
stack.push(cur.right);
}
if (cur.left!=null){
stack.push(cur.left);
}
}
}
/**
* 中序遍历
*/
public void inOrder(){
inOrder(root);
}
private void inOrder(Node node) {
if (node == null){
return ;
}
inOrder(node.left);
System.out.println(node.e);
inOrder(node.right);
}
/**
* 后序遍历
*/
public void postOrder(){
postOrder(root);
}
private void postOrder(Node node) {
if (node == null){
return ;
}
postOrder(node.left);
postOrder(node.right);
System.out.println(node.e);
}
/**
* 二分搜索树的层序遍历
*/
public void levelOrder(){
if (root == null){
return;
}
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (!q.isEmpty()){
Node cur = q.remove();
System.out.println(cur.e);
if (cur.left !=null){
q.add(cur.left);
}
if (cur.right !=null){
q.add(cur.right);
}
}
}
/**
* 查找二分搜索树最小的值
* @return
*/
public E mininum(){
if (size == 0){
throw new IllegalArgumentException("BST is empty");
}
Node mininum = mininum(root);
return mininum.e;
}
private Node mininum(Node node){
if (node.left ==null){
return node;
}
return mininum(node.left);
}
/**
* 在二分搜索树中查找最大的值
* @return
*/
public E maxnum(){
if (size == 0){
throw new IllegalArgumentException("BST is empty");
}
Node maxnum = maxnum(root);
return maxnum.e;
}
private Node maxnum(Node node){
if (node.right == null){
return node;
}
return maxnum(node.right);
}
/**
* 删除二分搜索树最小值
* @return
*/
public E removeMin(){
E ret = mininum();
root = removeMin(root);
return ret;
}
private Node removeMin(Node node) {
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
/**
* 删除二分搜索树最大值
* @return
*/
public E removeMax(){
E maxnum = maxnum();
root = removeMax(root);
return maxnum;
}
private Node removeMax(Node node) {
if (node.right == null){
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
node.right = removeMax(node.right);
return node;
}
/**
* 删除二分搜索树中任意元素
* @param e
*/
public void remove(E e){
root = remove(root,e);
}
/**
*
* @param node 根节点
* @param e 删除元素
* @return
*/
private Node remove(Node node,E e){
if (node == null){
return null;
}
if (e.compareTo(node.e) < 0){//比当前根节点元素小
node.left = remove(node.left,e);
return node;
}else if(e.compareTo(node.e)>0){//比当前根节点元素大
node.right = remove(node.right,e);
return node;
}else {//e == node.e,删除
//待删除节点左子树为空
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
//待删除节点右子树为空
if (node.right == null){
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
//方法1
//左右子树都不为空
//查找最接近待删除元素的元素,即待删除元素的右子树最小的值,替换
Node successor = mininum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
//方法二
//左右子树都不为空
//查找最接近待删除元素的元素,即待删除元素的左子树最大的值,替换
// Node successor = maxnum(node.left);
// successor.left = removeMax(node.left);
// successor.right = node.right;
node.left = node.right = null;
return successor;
}
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
generateBSTString(root,0,res);
return res.toString();
}
/**
* 生成以node为根节点,深度为depth的描述二叉树的字符串
* @param node
* @param depth
* @param res
*/
private void generateBSTString(Node node, int depth, StringBuilder res) {
if (node == null){
res.append(generateDepthString(depth) + "null\n");
return;
}
res.append(generateDepthString(depth) + node.e +"\n");
generateBSTString(node.left,depth+1,res);
generateBSTString(node.right,depth+1,res);
}
private String generateDepthString(int depth) {
StringBuilder res = new StringBuilder();
for (int i = 0; i < depth; i++) {
res.append("--");
}
return res.toString();
}
public static void main(String[] args) {
BST<Integer> bst = new BST<Integer>();
int[] nums ={5,3,6,8,4,2};
for (int num : nums) {
bst.add(num);
}
bst.preOrder();
System.out.println();
bst.remove(3);
bst.preOrder();
// System.out.println();
//
// bst.inOrder();
// System.out.println();
//
// bst.postOrder();
// System.out.println();
}
}
public class BSTSet<E extends Comparable<E>> implements Set<E>{
private BST<E> bst;
public BSTSet() {
this.bst = new BST<E>();
}
@Override
public void add(E e) {
bst.add(e);
}
@Override
public void remove(E e) {
bst.remove(e);
}
@Override
public boolean contaions(E e) {
return bst.contains(e);
}
@Override
public int getSize() {
return bst.size();
}
@Override
public boolean isEmpty() {
return bst.isEmpty();
}
}
Map实现
接口
public interface Map<K,V> {
void add(K key,V value);
V remove(K key);
boolean contains(K key);
V get(K key);
void set(K key,V value);
int getSize();
boolean isEmpty();
}
链表实现Map
public class LinkedListMap<K,V> implements Map<K,V> {
private class Node{
public K key;
public V value;
public Node next;
public Node(K key, V value,Node next) {
this.key = key;
this.value = value;
this.next = next;
}
public Node(K key) {
this(key,null,null);
}
public Node() {
this(null,null,null);
}
@Override
public String toString() {
return key.toString()+":"+ value.toString();
}
}
private Node dummyHead;
private int size;
@Override
public void add(K key, V value) {
Node node = getNode(key);
if (node == null){
dummyHead.next = new Node(key,value,dummyHead.next);
size++;
}else {
node.value = value;
}
}
@Override
public V remove(K key) {
Node prev = dummyHead;
while(prev.next != null){
if(prev.next.key.equals(key))
break;
prev = prev.next;
}
if(prev.next != null){
Node delNode = prev.next;
prev.next = delNode.next;
delNode.next = null;
size --;
return delNode.value;
}
return null;
}
@Override
public boolean contains(K key) {
return getNode(key)!=null;
}
@Override
public V get(K key) {
Node node = getNode(key);
return node == null ? null : node.value ;
}
@Override
public void set(K key, V newValue) {
Node node = getNode(key);
if(node == null)
throw new IllegalArgumentException(key + " doesn't exist!");
node.value = newValue;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size==0;
}
private Node getNode(K key){
Node cur = dummyHead;
while (cur!=null){
if (cur.key.equals(key))
return cur;
cur = cur.next;
}
return null;
}
public static void main(String[] args) {
}
}
二分搜索树实现Map
public class BSTMap<K extends Comparable<K>,V> implements Map<K,V> {
private class Node{
public K key;
public V value;
public Node left,right;
public Node(K key,V value) {
this.key = key;
this.value = value;
this.left = null;
this.right = null;
}
}
private Node root;
private int size;
public BSTMap() {
this.root = null;
this.size = 0;
}
@Override
public void add(K key, V value) {
root = add(root,key,value);
}
private Node add(Node node,K key,V value){
if (node == null){
size++;
return new Node(key, value);
}
//如果根节点左右不为空
if (key.compareTo(node.key)<0){//左节点
node.left = add(node.left,key,value);
}else if (key.compareTo(node.key)>0){//右节点
node.right = add(node.right,key,value);
}else {//key.compareTo(node.key)==0
node.value = value;
}
return node;
}
private Node getNode(Node node,K key){
if (node == null){
return null;
}
if (key.compareTo(node.key) == 0){
return node;
}else if (key.compareTo(node.key) < 0){
return getNode(node.left,key);
}else {
return getNode(node.right,key);
}
}
@Override
public V remove(K key) {
Node node = getNode(root, key);
if (node!=null){
root = remove(root,key);
return node.value;
}
return null;
}
private Node remove(Node node,K key){
if (node == null){
return null;
}
if (key.compareTo(node.key) < 0){//比当前根节点key小
node.left = remove(node.left,key);
return node;
}else if(key.compareTo(node.key)>0){//比当前根节点key大
node.right = remove(node.right,key);
return node;
}else {//key == node.key,删除
//待删除节点左子树为空
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
//待删除节点右子树为空
if (node.right == null){
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
//方法1
//左右子树都不为空
//查找最接近待删除元素的元素,即待删除元素的右子树最小的值,替换
Node successor = mininum(node.right);
successor.right = removeMin(node.right);
successor.left = node.left;
//方法二
//左右子树都不为空
//查找最接近待删除元素的元素,即待删除元素的左子树最大的值,替换
// Node successor = maxnum(node.left);
// successor.left = removeMax(node.left);
// successor.right = node.right;
node.left = node.right = null;
return successor;
}
}
private Node mininum(Node node){
if (node.left ==null){
return node;
}
return mininum(node.left);
}
private Node maxnum(Node node){
if (node.right == null){
return node;
}
return maxnum(node.right);
}
private Node removeMin(Node node) {
if (node.left == null){
Node rightNode = node.right;
node.right = null;
size--;
return rightNode;
}
node.left = removeMin(node.left);
return node;
}
private Node removeMax(Node node) {
if (node.right == null){
Node leftNode = node.left;
node.left = null;
size--;
return leftNode;
}
node.right = removeMax(node.right);
return node;
}
@Override
public boolean contains(K key) {
return getNode(root,key)!=null;
}
@Override
public V get(K key) {
Node node = getNode(root, key);
return node == null ? null :node.value;
}
@Override
public void set(K key, V newValue) {
Node node = getNode(root,key);
if(node == null)
throw new IllegalArgumentException(key + " doesn't exist!");
node.value = newValue;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
}