一、链表
1、Node类中需要保存自己的数据信息和下一个节点的信息 代码如下:
public class Node {
private Integer data;//节点中的数据
private Node next;//存储的下一个节点
public Node(Integer data) {
this.data = data;
}
public void setData(Integer data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public Integer getData() {
return data;
}
public Node getNext() {
return next;
}
}
2、实体类中添加、删除和遍历分别如下:
import com.work2.singly.Node;
public class SinglyLinkedList {
private Node head;
private int size;
public void setHead(Node head) {
this.head = head;
}
public Node getHead() {
return head;
}
public int getSize() {
return size;
}
/**
* 添加一个数据到下一个节点
* @param data
* @return
*/
public boolean add(Integer data){
Node node = new Node(data);
if (head == null){
head = node;
} else{
Node t = head;
while (t.getNext() != null){
t = t.getNext();
}
t.setNext(node);
}
size++;
return true;
}
/**
* 在指定位置添加一个节点
*/
public boolean add(Integer data,int index){
if (index < 0 || index >= size){
throw new RuntimeException("越界异常");
}else{
Node node = new Node(data);
if (index == 0){
node.setNext(head);
head = node;
}else{
Node temp = head;
for (int i = 0;i < index-1;i++){
temp = temp.getNext();
}
node.setNext(temp.getNext());
temp.setNext(node);
}
}
size++;
return true;
}
/**
* 删除最后一个节点
*/
public Integer remove(){
if (head == null) {
throw new RuntimeException("无法删除");
}
if (head.getNext() == null){
Integer data = head.getData();
head = null;
return data;
}
Node tlow = null;
Node tfast = head;
while (tfast.getNext() != null){
tlow = tfast;
tfast = tfast.getNext();
}
tlow.setNext(null);
return tfast.getData();
}
/**
* 删除指定位置的节点
*/
public Integer remove(int index){
if (index < 0 || index >= size){
throw new RuntimeException("越界异常");
}
if (index == 0){
Integer data = head.getData();
head = null;
return data;
}
Node tlow = null;
Node tfast = head;
for (int i = 0;i < index;i++){
tlow = tfast;
tfast = tfast.getNext();
}
tlow.setNext(tfast.getNext());
tfast.setNext(null);
return tfast.getData();
}
public Integer get(int index){
if (index < 0 || index >= size){
throw new RuntimeException("越界异常");
}
Node temp = head;
for (int i = 0; i < index; i++) {
temp = temp.getNext();
}
return temp.getData();
}
/**
* 遍历数组
*/
public void print(){
Node temp = head;
while (temp != null){
System.out.println(temp.getData());
temp = temp.getNext();
}
}
}
二、二叉树
1、Node类型中需要自身的数据、左右两个节点的信息
public class TreeNode {
private Integer data;
private TreeNode left;
private TreeNode right;
public TreeNode(Integer data) {
this.data = data;
}
public Integer getData() {
return data;
}
public TreeNode getLeft() {
return left;
}
public TreeNode getRight() {
return right;
}
public void setData(Integer data) {
this.data = data;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public void setRight(TreeNode right) {
this.right = right;
}
}
2、二叉树的遍历 涉及对下一个连接点非空的判断和递归
/**
* 按广度遍历数组
* @param root
*/
public static void printByLayer(TreeNode root){
if (root == null){
return;
}
Queue<TreeNode> queue = new LinkedList<>();//创建一个队列存储节点,先进先出
queue.add(root);
while (!queue.isEmpty()){
TreeNode temp = queue.poll();
System.out.println(temp.getData());
if (temp.getLeft() != null)
queue.add(temp.getLeft());
if (temp.getRight() != null)
queue.add(temp.getRight());
}
}
/**
* 前序遍历 根左右
*/
public static void pre_print(TreeNode root){
if (root != null){
System.out.println(root.getData());
}
if (root.getLeft() != null)
pre_print(root.getLeft());
if (root.getRight() != null)
pre_print(root.getRight());
}
/**
* 中序遍历 左根右
*/
public static void mid_print(TreeNode root){
if (root != null){
//左
if (root.getLeft() != null)
mid_print(root.getLeft());
//根
System.out.println(root.getData());
//右
if (root.getRight() != null)
mid_print(root.getRight());
}
}
/**
* 后序遍历 左右根
* @param root
*/
public static void last_print(TreeNode root){
if (root != null){
if (root.getLeft() != null)
last_print(root.getLeft());
if (root.getRight() != null)
last_print(root.getRight());
System.out.println(root.getData());
}
}
3、有序二叉树
/**
* 查找二叉树指定节点
* @param data
* @return
*/
public TreeNode get(Integer key){
TreeNode temp = root;
while (temp != null){
if (key < temp.getData()){
temp = temp.getLeft();
}else if (key > temp.getData()){
temp = temp.getRight();
}else{
return temp;
}
}
return null;
}
/**
* 添加有序二叉树
* @param data
* @return
*/
public boolean add(Integer data){
if (root == null){
this.root = new TreeNode(data);
}
else {
TreeNode current = root;
TreeNode parentNode = null;
while (current != null){
parentNode = current;
if (data < current.getData()){
current = current.getLeft();
if (current == null) {
parentNode.setLeft(new TreeNode(data));
return true;
}
}else {
current = current.getRight();
if (current == null) {
parentNode.setRight(new TreeNode(data));
return true;
}
}
}
}
return false;
}
}