因为让代码足够健壮和足够合理,并且结果有附图,所以这篇博客代码很长很多。不习惯的话,可以直接点击目录查看。
1 顺序表
package DataStructure;
/**
* @author RuiMing Lin
* @date 2021-01-27 15:28
* @description 泛型为T的顺序表
*/
public class SequenceList<T> {
private final int maxSize = 100; // 顺序表的最大长度
private T[] listArray; // T类型的数组
private int length; // 存储当前顺序表的长度
/**
* 构造函数
*/
public SequenceList() {
this.length = 0;
this.listArray = (T[]) new Object[this.maxSize + 1];
}
/**
* 构造函数
* @param n
*/
public SequenceList(int n) {
if (n <= 0) {
System.out.println("error");
System.exit(1);
}
this.length = 0;
this.listArray = (T[]) new Object[n + 1];
}
/**
* 添加新的元素
* @param t
* @return
*/
public boolean add(T t) {
if (this.length >= this.maxSize) {
System.out.println("顺序表已满,不能再添加结点了!");
return false;
}
this.listArray[++this.length] = t;
return true;
}
/**
* 添加新的元素
*
* @param t
* @param pos
* @return
*/
public boolean add(T t, int pos) {
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return false;
}
if (this.length >= this.maxSize) {
System.out.println("顺序表已满,不能再添加结点了!");
return false;
}
for (int i = this.length + 1; i > pos; i--) {
this.listArray[i] = this.listArray[i - 1];
}
this.listArray[pos] = t;
this.length++;
return true;
}
/**
* 删除元素
*
* @param pos
* @return
*/
public T remove(int pos) {
if (isEmpty()) {
System.out.println("顺序表为空,不能再删除节点了!");
return null;
}
if (pos < 1 || pos >= this.length + 1) {
System.out.println("pos值不合法");
return null;
}
T t = this.listArray[pos];
for (int i = pos; i <= this.length; i++) {
listArray[i] = listArray[i + 1];
}
this.length--;
return t;
}
/**
* 查找一个元素
*
* @param obj
* @return
*/
public int find(T obj) {
if (isEmpty()) {
System.out.println("顺序表为空,不能再删除节点了!");
return -1;
}
for (int i = 0; i < this.length; i++) {
if (obj.equals(this.listArray[i])) {
return i;
}
}
return -1;
}
/**
* 根据位置获得元素
*
* @param pos
* @return
*/
public T get(int pos) {
if (isEmpty()) {
System.out.println("顺序表为空,不能再删除节点了!");
return null;
}
if (pos < 1 || pos >= length + 1) {
System.out.println("pos值不合法");
return null;
}
return this.listArray[pos];
}
/**
* 根据位置更新元素
*
* @param t
* @param pos
* @return
*/
public boolean update(T t, int pos) {
if (isEmpty()) {
System.out.println("顺序表为空,不能再删除节点了!");
return false;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return false;
}
this.listArray[pos] = t;
return true;
}
/**
* 判断线性表是否为空
*
* @return
*/
public boolean isEmpty() {
return this.length == 0;
}
/**
* 当前线性表存储的元素数量
* @return
*/
public int size() {
return this.length;
}
/**
* 输出顺序表
*/
public void print() {
for (int i = 1; i <= this.length; i++) {
System.out.println(this.listArray[i]);
}
}
/**
* 清空顺序表
*/
public void clear() {
this.length = 0;
}
public static void main(String[] args) {
SequenceList<String> sl = new SequenceList<>();
// 增
sl.add("bbb");
sl.add("ccc");
sl.add("ddd");
sl.add("aaa", 1);
sl.print();
System.out.println("---------------------------");
// 删
sl.remove(4);
sl.print();
System.out.println("---------------------------");
// 查
int index = sl.find("aaa");
System.out.println("index = " + index);
String s = sl.get(1);
System.out.println("s = " + s);
System.out.println("---------------------------");
// 改
boolean b = sl.update("eee", 3);
System.out.println("b = " + b);
sl.print();
System.out.println("---------------------------");
// 属性
System.out.println(sl.size());
System.out.println(sl.isEmpty());
sl.clear();
sl.print();
}
}
结果如下:
2 单链表
package DataStructure;
/**
* @author RuiMing Lin
* @date 2021-01-27 20:05
* @description 泛型为T的链表
*/
public class LinkList<T> {
private Node<T> head; // 头结点,不存储数据,指针指向的是第一个存储数据的节点
private int length; // 当前链表的节点数量
/**
* 构造函数
*/
public LinkList() {
this.length = 0;
this.head = new Node<T>(null);
}
/**
* 获得头结点
*
* @return
*/
public Node<T> getHead() {
return this.head;
}
/**
* 根据位置添加节点
*
* @param t
* @param pos
* @return
*/
public boolean add(T t, int pos) {
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return false;
}
int num = 1;
Node<T> p = head;
Node<T> q = head.next;
while (num < pos) {
// 把p定位到pos-1的位置上
p = q;
q = q.next;
num++;
}
p.next = new Node<T>(t, q);
this.length++;
return true;
}
/**
* 根据位置删除元素
*
* @param pos
* @return
*/
public T remove(int pos) {
if (isEmpty()) {
System.out.println("链表为空");
return null;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return null;
}
int num = 1;
Node<T> p = head;
Node<T> q = head.next;
while (num < pos) {
// 把p定位到pos-1的位置上
p = q;
q = q.next;
num++;
}
p.next = q.next;
this.length--;
return q.data;
}
/**
* 根据位置查询元素
*
* @param pos
* @return
*/
public T get(int pos) {
if (isEmpty()) {
System.out.println("链表为空");
return null;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return null;
}
int num = 1;
Node<T> q = head.next;
while (num < pos) {
// 把p定位到pos的位置上
q = q.next;
num++;
}
return q.data;
}
/**
* 根据元素返回索引
*
* @param t
* @return
*/
public int find(T t) {
if (isEmpty()) {
System.out.println("链表为空");
return -1;
}
int num = 1;
Node<T> p = head.next;
while (p != null) {
// 把p定位到pos的位置上
if (t.equals(p.data) == false) {
p = p.next;
num++;
} else break;
}
if (p == null)
return -1;
return num;
}
/**
* 更新元素
*
* @param t
* @param pos
* @return
*/
public boolean update(T t, int pos) {
if (isEmpty()) {
System.out.println("链表为空");
return false;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return false;
}
int num = 1;
Node<T> q = head.next;
while (num < pos) {
q = q.next;
num++;
}
q.data = t;
return true;
}
/**
* 判空
*
* @return
*/
public boolean isEmpty() {
return this.length == 0;
}
/**
* 返回当前链表存储节点的数量
*
* @return
*/
public int size() {
return this.length;
}
/**
* 输出节点信息
*/
public void print() {
Node<T> p = head.next;
while (p != null) {
System.out.println(p.data);
p = p.next;
}
}
/**
* 清空节点
*/
public void clear() {
this.length = 0;
this.head.next = null;
}
public static void main(String[] args) {
LinkList<String> linkList = new LinkList<>();
// 增
linkList.add("aaa", 1);
linkList.add("bbb", 2);
linkList.add("ccc", 3);
linkList.add("ddd", 4);
linkList.add("eee", 5);
linkList.print();
System.out.println("------------------------------");
// 删
linkList.remove(2);
linkList.print();
System.out.println("------------------------------");
// 查
String s = linkList.get(2);
System.out.println("s = " + s);
int index = linkList.find("eee");
System.out.println("index = " + index);
System.out.println("------------------------------");
// 改
boolean b = linkList.update("fff", 4);
linkList.print();
}
}
/**
* 节点类
*
* @param <T>
*/
class Node<T> {
T data;
Node<T> next;
public Node(Node<T> n) {
this.next = n;
}
public Node(T data, Node<T> n) {
this.data = data;
this.next = n;
}
public T getData() {
return this.data;
}
public Node<T> getNext() {
return this.next;
}
}
结果如下:
3 循环列表
package DataStructure;
/**
* @author RuiMing Lin
* @date 2021-01-28 15:41
* @description 循环链表·
*/
public class CircuLinkList<T> {
private Node<T> head; // 头结点,不存储数据,指针指向的是第一个存储数据的节点
private int length; // 当前链表的节点数量
/**
* 构造函数
*/
public CircuLinkList() {
this.length = 0;
Node<T> node = new Node<T>();
this.head = node;
node.data = null;
node.next = this.head; // 初始化时,头节点的指针指向头结点,构成循环链表
}
/**
* 获得头结点
*
* @return
*/
public Node<T> getHead() {
return this.head;
}
/**
* 根据位置添加节点
*
* @param t
* @param pos
* @return
*/
public boolean add(T t, int pos) {
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值 不合法");
return false;
}
int num = 1;
Node<T> p = head;
Node<T> q = head.next;
while (num < pos) {
// 把p定位到pos-1的位置上
p = q;
q = q.next;
num++;
}
p.next = new Node<T>(t, q); // 将q赋值给新节点的指针
this.length++;
return true;
}
/**
* 根据位置删除元素
*
* @param pos
* @return
*/
public T remove(int pos) {
if (isEmpty()) {
System.out.println("循环链表为空");
return null;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return null;
}
int num = 1;
Node<T> p = head;
Node<T> q = head.next;
while (num < pos) {
// 把p定位到pos的位置上
p = q;
q = q.next;
num++;
}
p.next = q.next;
this.length--;
return q.data;
}
/**
* 根据位置查询元素
*
* @param pos
* @return
*/
public Node<T> get(int pos) {
if (isEmpty()) {
System.out.println("循环链表为空");
return null;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return null;
}
int num = 1;
Node<T> q = head.next;
while (num < pos) {
// 把p定位到pos的位置上
q = q.next;
num++;
}
return q;
}
/**
* 根据元素返回索引
*
* @param t
* @return
*/
public int find(T t) {
if (isEmpty()) {
System.out.println("循环链表为空");
return -1;
}
int num = 1;
Node<T> p = head.next;
while (p != null) {
// 把p定位到pos-1的位置上
if (!t.equals(p.data)) {
p = p.next;
num++;
} else break;
}
if (p == null)
return -1;
return num;
}
/**
* 更新元素
*
* @param t
* @param pos
* @return
*/
public boolean update(T t, int pos) {
if (isEmpty()) {
System.out.println("循环链表为空");
return false;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return false;
}
int num = 1;
Node<T> q = head.next;
while (num < pos) {
q = q.next;
num++;
}
q.data = t;
return true;
}
/**
* 判空
*
* @return
*/
public boolean isEmpty() {
return this.length == 0;
}
/**
* 返回当前链表存储节点的数量
*
* @return
*/
public int size() {
return this.length;
}
/**
* 输出节点信息
*/
public void print() {
/**
* 循环列表不适合用这种方式输出每一个节点
Node<T> p = head.next;
while (p != null) {
System.out.println(p.data);
p = p.next;
}
*/
int num = 1;
Node<T> p = head.next;
while (num++ <= this.size()) {
System.out.println(p.data);
p = p.next;
}
}
/**
* 清空节点
*/
public void clear() {
this.length = 0;
this.head.next = null;
}
public static void main(String[] args) {
CircuLinkList<String> circuLinkList = new CircuLinkList<>();
// 增
circuLinkList.add("aaa", 1);
circuLinkList.add("bbb", 2);
circuLinkList.add("ccc", 3);
circuLinkList.add("ddd", 4);
circuLinkList.add("eee", 5);
circuLinkList.print();
System.out.println("------------------------------");
// 删
circuLinkList.remove(2);
circuLinkList.print();
System.out.println("------------------------------");
// 查
Node<String> node = circuLinkList.get(2);
System.out.println("node.data = " + node.data);
int index = circuLinkList.find("eee");
System.out.println("index = " + index);
System.out.println("------------------------------");
// 改
boolean b = circuLinkList.update("fff", 4);
circuLinkList.print();
// 判断是否循环
Node<String> node1 = circuLinkList.get(circuLinkList.size()); // 获得最后一个节点
System.out.println(node1.next == circuLinkList.getHead()); // 判断最后一个节点的指针是否指向头结点
}
}
/**
* 节点类
*
* @param <T>
*/
public class Node<T> {
T data;
Node<T> next;
public Node() {
}
public Node(Node<T> n) {
this.next = n;
}
public Node(T data, Node<T> n) {
this.data = data;
this.next = n;
}
public T getData() {
return this.data;
}
public Node<T> getNext() {
return this.next;
}
}
结果如下:
4 双向链表
package DataStructure;
/**
* @author RuiMing Lin
* @date 2021-01-28 20:09
* @description
*/
public class BiLinkList<T> {
private BiNode<T> head; // 头结点,不存储数据,指针指向的是第一个存储数据的节点
private int length; // 当前链表的节点数量
/**
* 构造函数
*/
public BiLinkList() {
this.length = 0;
this.head = new BiNode<>(null, null, null);
}
/**
* 获得头结点
*
* @return
*/
public BiNode<T> getHead() {
return this.head;
}
/**
* 根据位置添加节点
*
* @param t
* @param pos
* @return
*/
public boolean add(T t, int pos) {
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return false;
}
int num = 1;
BiNode<T> p = this.head;
BiNode<T> q = p.next;
while (num < pos) {
// 把p定位到pos-1的位置上
p = q;
q = q.next;
num++;
}
BiNode<T> newNode = new BiNode<>(t, p, q);
if (q != null) {
newNode.setPrevious(p);
newNode.setNext(q);
p.next = newNode;
q.previous = newNode;
}else {
p.next = newNode;
newNode.setPrevious(p);
}
this.length++;
return true;
}
/**
* 根据位置删除元素
*
* @param pos
* @return
*/
public T remove(int pos) {
if (isEmpty()) {
System.out.println("双向链表为空");
return null;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return null;
}
int num = 1;
BiNode<T> p = this.head;
BiNode<T> q = p.next;
while (num < pos) {
// 把p定位到pos-1的位置上
p = q;
q = q.next;
num++;
}
BiNode<T> tmp = q;
p.next = tmp.next;
tmp.next.previous = p;
this.length--;
return q.data;
}
/**
* 根据位置查询元素
*
* @param pos
* @return
*/
public BiNode get(int pos) {
if (isEmpty()) {
System.out.println("双向链表为空");
return null;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return null;
}
int num = 1;
BiNode<T> q = head.next;
while (num < pos) {
q = q.next;
num++;
}
return q;
}
/**
* 根据元素返回索引
*
* @param t
* @return
*/
public int find(T t) {
if (isEmpty()) {
System.out.println("双向链表为空");
return -1;
}
int num = 1;
BiNode<T> p = head.next;
while (p != null) {
if (t.equals(p.data) == false) {
p = p.next;
num++;
} else break;
}
if (p == null)
return -1;
return num;
}
/**
* 更新元素
*
* @param t
* @param pos
* @return
*/
public boolean update(T t, int pos) {
if (isEmpty()) {
System.out.println("双向链表为空");
return false;
}
if (pos < 1 || pos > this.length + 1) {
System.out.println("pos值不合法");
return false;
}
int num = 1;
BiNode<T> q = head.next;
while (num < pos) {
q = q.next;
num++;
}
q.data = t;
return true;
}
/**
* 判空
*
* @return
*/
public boolean isEmpty() {
return this.length == 0;
}
/**
* 返回当前链表存储节点的数量
*
* @return
*/
public int size() {
return this.length;
}
/**
* 输出节点信息
*/
public void print() {
BiNode<T> p = head.next;
while (p != null) {
System.out.println(p.data);
p = p.next;
}
}
/**
* 清空节点
*/
public void clear() {
this.length = 0;
this.head.previous = null;
this.head.next = null;
}
public static void main(String[] args) {
BiLinkList<String> biLinkList = new BiLinkList<>();
biLinkList.add("aaa", 1);
biLinkList.add("bbb", 2);
biLinkList.add("ccc", 3);
biLinkList.add("ddd", 4);
biLinkList.add("eee", 5);
biLinkList.print();
System.out.println("------------------------------");
// 删
biLinkList.remove(2);
biLinkList.print();
System.out.println("------------------------------");
// 查
BiNode<String> node = biLinkList.get(2);
System.out.println("node.data = " + node.getData());
int index = biLinkList.find("eee");
System.out.println("index = " + index);
System.out.println("------------------------------");
// 改
boolean b = biLinkList.update("fff", 4);
biLinkList.print();
System.out.println("------------------------------");
// 判断是否双向
biLinkList.add("bbb", 2);
BiNode biNode = biLinkList.get(2);
System.out.println(biNode.getData());
System.out.println(biNode.getPrevious().getData());
System.out.println(biNode.getNext().getData());
}
}
class BiNode<T> {
T data;
BiNode<T> previous;
BiNode<T> next;
/**
* 构造函数
*/
public BiNode() {
}
public BiNode(T data) {
this.data = data;
this.previous = null;
this.next = null;
}
public BiNode(T data, BiNode<T> previous, BiNode<T> next) {
this.data = data;
this.previous = previous;
this.next = next;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BiNode<T> getPrevious() {
return previous;
}
public void setPrevious(BiNode<T> previous) {
this.previous = previous;
}
public BiNode<T> getNext() {
return next;
}
public void setNext(BiNode<T> next) {
this.next = next;
}
}
结果如下:
5 栈
package DataStructure;
/**
* @author RuiMing Lin
* @date 2021-01-28 22:29
* @description
*/
public class Stack<T> {
final int MaxSize = 16;
private T[] stack; // 存储数据节点
private int top; // 栈顶指针
/**
* 构造函数
*/
public Stack() {
this.top = -1;
this.stack = (T[]) new Object[MaxSize];
}
public Stack(int n) {
if (n <= 0) {
System.out.println("初始化长度应该大于0");
System.exit(1);
}
this.top = -1;
this.stack = (T[]) new Object[n];
}
/**
* 入栈
*
* @param t
*/
public void push(T t) {
if (top == this.MaxSize - 1) {
System.out.println("栈满,不能再插入节点了!");
}
this.stack[++top] = t;
}
/**
* 出栈
*
* @return
*/
public T pop() {
if (top == -1) {
System.out.println("栈空,不能再删除节点了!");
return null;
}
T tmp = this.stack[top];
top--;
return tmp;
}
/**
* 查看栈顶元素
*
* @return
*/
public T peak() {
if (top == -1) {
System.out.println("栈空!");
return null;
}
return this.stack[top];
}
/**
* 查看栈的大小
*
* @return
*/
public int size() {
return this.top + 1;
}
/**
* 判断栈是否为空
*
* @return
*/
public boolean isEmpty() {
return top == -1;
}
/**
* 清空栈
*/
public void clear() {
this.top = -1;
this.stack = null;
}
/**
* 输出栈信息
*/
public void print() {
for (int i = top; i >= 0; i--) {
System.out.println(this.stack[i]);
}
}
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("aaa");
stack.push("bbb");
stack.push("ccc");
stack.print();
System.out.println("----------------------------");
System.out.println(stack.pop());
System.out.println(stack.peak());
System.out.println("----------------------------");
System.out.println(stack.isEmpty());
System.out.println(stack.size());
}
}
结果如下:
6 队列
package DataStructure;
/**
* @author RuiMing Lin
* @date 2021-01-29 14:38
* @description 队列
*/
public class Queue<T> {
final int MaxSize = 16;
private T[] queue; // 存储数据节点
private int front; // 队首指针
private int rear; // 队尾指针
/**
* 构造函数
*/
public Queue() {
this.front = 0;
this.rear = -1;
this.queue = (T[]) new Object[MaxSize];
}
public Queue(int n) {
if (n <= 0) {
System.out.println("初始化长度应该大于0");
System.exit(1);
}
this.front = 0;
this.rear = -1;
this.queue = (T[]) new Object[n];
}
/**
* 入队
*
* @param t
*/
public void EnQueue(T t) {
if (this.rear + 1 == this.queue.length) {
T[] tmp = (T[]) new Object[this.queue.length * 2]; // 扩容
for (int i = this.front; i <= this.rear; i++) {
// 复制,必须从this.front开始,减少不必要的空间浪费
tmp[i] = this.queue[i];
}
this.queue = tmp;
System.out.println("扩容完成,当前队列最大长度为:" + this.queue.length);
}
this.queue[++rear] = t; // 入队时,头指针不变,尾指针+1
}
/**
* 出队
*
* @return
*/
public T DeQueue() {
if (this.rear < this.front) {
// 当队列中只有一个节点时,rear等于front;当没有节点时,rear = front - 1
System.out.println("队空,不能再删除节点了!");
return null;
}
T t = this.queue[front++]; // 出队时,尾指针不变,头指针+1
if (this.rear - this.front + 1 < this.queue.length / 2) {
T[] tmp = (T[]) new Object[this.queue.length / 2];
int j = 0;
for (int i = this.front; i <= this.rear; i++, j++) {
// 复制,必须从this.front开始,减少不必要的空间浪费
tmp[j] = this.queue[i];
}
this.front = 0;
this.rear = j - 1;
this.queue = tmp;
System.out.println("缩容完成,当前队列最大长度为:" + this.queue.length);
}
return t;
}
/**
* 查看队首元素
*
* @return
*/
public T getFront() {
if (this.isEmpty()) {
System.out.println("队空!");
return null;
}
return this.queue[front];
}
/**
* 查看栈的大小
*
* @return
*/
public int size() {
return this.rear - this.front + 1;
}
/**
* 判断队列是否为空
*
* @return
*/
public boolean isEmpty() {
return this.rear < this.front;
}
/**
* 清空栈
*/
public void clear() {
this.front = 0;
this.rear = -1;
this.queue = (T[]) new Object[MaxSize];
}
/**
* 输出栈信息
*/
public void print() {
for (int i = this.front; i <= this.rear; i++) {
System.out.println(this.queue[i]);
}
}
public static void main(String[] args) {
Queue<String> queue = new Queue<>(4);
queue.EnQueue("aaa");
queue.EnQueue("bbb");
queue.EnQueue("ccc");
queue.EnQueue("ddd");
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
queue.print();
System.out.println("---------------------------");
queue.EnQueue("eee");
queue.print();
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
System.out.println("---------------------------");
String deQueue = queue.DeQueue();
System.out.println("deQueue = " + deQueue);
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
queue.print();
System.out.println("---------------------------");
queue.DeQueue();
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
queue.print();
System.out.println("---------------------------");
queue.DeQueue();
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
queue.print();
System.out.println("---------------------------");
queue.DeQueue();
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
queue.print();
System.out.println("---------------------------");
System.out.println(queue.getFront());
System.out.println(queue.size());
System.out.println("---------------------------");
queue.DeQueue();
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
queue.print();
System.out.println("---------------------------");
queue.DeQueue();
System.out.println("头指针位置:" + queue.front);
System.out.println("尾指针位置:" + queue.rear);
queue.print();
System.out.println("---------------------------");
queue.clear();
queue.print();
}
}
结果如下:
7 二叉树
package DataStructure;
/**
* @author RuiMing Lin
* @date 2021-02-03 13:55
* @description
*/
public class BiTree<T> {
private TreeNode<T> root; // 根节点
/**
* 构造函数
*/
public BiTree() {
}
public BiTree(T data) {
this.root = new TreeNode<T>(data);
}
public void setRoot(TreeNode<T> root) {
this.root = root;
}
public TreeNode<T> getRoot() {
return root;
}
/**
* 在当前parent节点插入一个左子节点,若该parent已存在左子节点,则将该左子节点变为新节点的左子节点
*
* @param data
* @param parent
* @return
*/
public boolean insertLeft(T data, TreeNode<T> parent) {
if (parent == null) return false;
TreeNode<T> newNode = new TreeNode<>(data);
if (parent.getlChild() == null) parent.setlChild(newNode);
else {
newNode.setlChild(parent.getlChild());
parent.setlChild(newNode);
}
return true;
}
/**
* 在当前parent节点插入一个右子节点,若该parent已存在右子节点,则将该右子节点变为新节点的右子节点
*
* @param data
* @param parent
* @return
*/
public boolean insertRight(T data, TreeNode<T> parent) {
if (parent == null) return false;
TreeNode<T> newNode = new TreeNode<>(data);
if (parent.getrChild() == null) parent.setrChild(newNode);
else {
newNode.setrChild(parent.getrChild());
parent.setrChild(newNode);
}
return true;
}
/**
* 删除parent节点的左子节点
*
* @param parent
* @return
*/
public boolean deleteLeft(TreeNode<T> parent) {
if (parent == null) return false;
parent.setlChild(null);
return true;
}
/**
* 删除parent节点的右子节点
*
* @param parent
* @return
*/
public boolean deleteRight(TreeNode<T> parent) {
if (parent == null) return false;
parent.setrChild(null);
return true;
}
public boolean search(TreeNode<T> node, T data) {
if (node == null) {
return false;
}
if (node.getData() == data) {
return true;
} else {
return search(node.getlChild(), data) || search(node.getrChild(), data);
}
}
/**
* preorder: 先序遍历
* inorder: 中序遍历
* postorder: 后序遍历
*
* @param node
* @param order
*/
public void print(TreeNode<T> node, String order) {
if (order == "preorder") {
if (node == null) return;
System.out.println(node.getData());
print(node.getlChild(), order);
print(node.getrChild(), order);
}
if (order == "inorder") {
if (node == null) return;
print(node.getlChild(), order);
System.out.println(node.getData());
print(node.getrChild(), order);
}
if (order == "postorder") {
if (node == null) return;
print(node.getlChild(), order);
print(node.getrChild(), order);
System.out.println(node.getData());
}
}
/**
* 获得二叉树的高度
*
* @param parent
* @return
*/
public int getHeight(TreeNode<T> parent) {
int lh, rh, max;
if (parent != null) {
lh = getHeight(parent.getlChild());
rh = getHeight(parent.getrChild());
max = lh > rh ? lh : rh;
return max + 1;
}
return 0;
}
public static void main(String[] args) {
// 插入节点
BiTree<Integer> biTree = new BiTree<>(1);
biTree.insertLeft(2, biTree.getRoot());
biTree.insertRight(3, biTree.getRoot());
biTree.insertLeft(4, biTree.getRoot().getlChild());
biTree.insertRight(5, biTree.getRoot().getlChild());
biTree.insertLeft(6, biTree.getRoot().getrChild());
biTree.insertRight(7, biTree.getRoot().getrChild());
System.out.println(biTree.getRoot());
// search()
boolean search = biTree.search(biTree.getRoot(), 1);
System.out.println("search = " + search);
// 先序遍历
biTree.print(biTree.getRoot(), "preorder");
System.out.println("--------------------------------");
// 中序遍历
biTree.print(biTree.getRoot(), "inorder");
System.out.println("--------------------------------");
// 后序遍历
biTree.print(biTree.getRoot(), "postorder");
System.out.println("--------------------------------");
System.out.println(biTree.getHeight(biTree.getRoot()));
}
}
class TreeNode<T> {
private TreeNode<T> lChild;
private T data;
private TreeNode<T> rChild;
public TreeNode() {
data = null;
lChild = null;
rChild = null;
}
public TreeNode(T data) {
this.data = data;
}
public TreeNode(T data, TreeNode<T> lChild, TreeNode<T> rChild) {
this.data = data;
this.lChild = lChild;
this.rChild = rChild;
}
public TreeNode<T> getlChild() {
return lChild;
}
public void setlChild(TreeNode<T> lChild) {
this.lChild = lChild;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public TreeNode<T> getrChild() {
return rChild;
}
public void setrChild(TreeNode<T> rChild) {
this.rChild = rChild;
}
@Override
public String toString() {
return "TreeNode{" +
"lChild=" + lChild +
", data=" + data +
", rChild=" + rChild +
'}';
}
}
8 图
package DataStructure;
import java.util.Scanner;
/**
* @author RuiMing Lin
* @date 2021-02-03 17:24
* @description
*/
public class Graph<T> {
protected final int MAXSIZE = 10; //邻接矩阵可以表示的最大顶点数
protected final int MAX = 999; //在网中,表示没有联系(权值无穷大)
protected T[] V; //顶点信息
protected int[][] arcs; //邻接矩阵
protected int e; //边数
protected int n; //顶点数
public Graph() {
V = (T[]) new Object[MAXSIZE];
arcs = new int[MAXSIZE][MAXSIZE];
}
/**
* 创建无向图的邻接矩阵
*/
public void CreateAdj() {
int i, j, k;
T v1, v2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入图的顶点数及边数");
System.out.print("顶点数n = ");
this.n = sc.nextInt();
System.out.print("边数e = ");
this.e = sc.nextInt();
System.out.print("请输入图的顶点信息∶");
String str = sc.next();
for (i = 0; i < this.n; i++) {
V[i] = (T) (Object) str.charAt(i);//构造顶点信息
}
for (i = 0; i < this.n; i++){
for (j = 0; j < n; j++)
arcs[i][j] = 0;//初始化邻接矩阵
}
System.out.println("请输入图的边的信息∶");
for (k = 0; k < this.e; k++){
System.out.print("请输入第" + (k + 1) + "条边的两个端点∶");
str = sc.next(); //输入一条边的两个顶点
v1 = (T) (Object) str.charAt(0);
v2 = (T) (Object) str.charAt(1); //确定两个顶点在图G中的位置
i = LocateVex(v1);
j = LocateVex(v2);
if (i >= 0 && j >= 0) {
arcs[i][j] = 1;
arcs[j][i] = 1;
}
}
}
/**
* 图中查找顶点v,找到后返回其在顶点数组中的索引号。若不存在,返回-1
*
* @param v
* @return
*/
public int LocateVex(T v) {
int i;
for (i = 0; i < n; i++) {
if (V[i] == v)
return i;
}
return -1;
}
/**
* 输出邻接矩阵
*/
public void DisplayAdjMatrix() {
int i, j;
System.out.println("图的邻接矩阵表示∶");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
System.out.print(" " + arcs[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Graph<Character> graph = new Graph<>();
graph.CreateAdj();
graph.DisplayAdjMatrix();
}
}
结果如下: