一、线性结构
线性表
1.线性表的顺序表示,如:数组
2.线性表的链式表示
- 单向链表
- 双向链表
- 循环链表
栈和队列
栈和队列也是线性表,但是是属于操作受限的线性表
1.单向链表举例
public class Node {
Node next =null;
int data;
public Node(int d){
data = d;
}
//添加节点
void appendToTail(int d){
Node end = new Node(d);
Node n =this;
while(n.next!=null){
n=n.next;
}
n.next= end;
}
//删除指定节点
Node deleteNode(Node head,int d){
Node n = head;
if(n.data==d){
return head.next;
}
while(n.next!=null){
if(n.next.data==d){
n.next= n.next.next;
return head;
}
n = n.next;
}
return head;
}
}
2.栈的数组表示
import java.util.Arrays;
public class StackInArray {
int top;
int base;
public static int STACKSIZE = 100;
public Object value[];
public StackInArray(){
top = 0;
base = 0;
value=new Object[STACKSIZE];
}
public void push(Object item){
if(item==null)
return ;
if(top-base>=value.length){
value= Arrays.copyOf(value, value.length*2);
}
value[top++]=item;
}
public Object pop(){
if(top==base){
return null;
}
return value[--top];
}
}
3.栈的链表表示
public class StackInList {
Node top;
void push(Object item){
Node n = new Node(item);
n.next = top;
top = n;
}
Object pop(){
if(top==null){
return null;
}
Object item = top.data;
top = top.next;
return item;
}
Object peek(){
return top.data;
}
}
4.队列的链表表示
public class Queue {
Node rear,front;
//入队列,添加到尾部
public void endequeue(Object item){
if(front==null){
rear = new Node(item);
front =rear;
}else{
rear.next =new Node(item);
rear = rear.next;
}
}
public Object dequeue(){
if(front!=null){
Object item = front.data;
front = front.next;
return item;
}
return null;
}
}
二、非线性结构
树
import java.util.LinkedList;
import java.util.Scanner;
public class BinaryTree {
private static Node root=null;
private class Node{
Node left;
Node right;
int value;
public boolean visited;
public Node(int value){
this.value=value;
left = null;
right = null;
}
}
//求树高
public static int getHeight(Node root){
if(root==null){
return 0;
}
return Math.max(getHeight(root.left), getHeight(root.right))+1;
}
//先序遍历构造二叉树
public Node createTree(Node node){
Scanner in = new Scanner(System.in);
int value = in.nextInt();
if(value==-1){
return null;
}else{
node = new Node(value);
node.left=createTree(node.left);
node.right=createTree(node.right);
return node;
}
}
//前序遍历
public void preOrder(Node node){
if(node==null)
return ;
System.out.print(node.value+" ");
preOrder(node.left);
preOrder(node.right);
}
//中序遍历
public void InOrder(Node node){
if(node==null)
return ;
InOrder(node.left);
System.out.print(node.value+" ");
InOrder(node.right);
}
public void PostOrder(Node node){
if(node == null){
return ;
}
PostOrder(node.left);
PostOrder(node.right);
System.out.print(node.value+" ");
}
//树的广度优先搜索
public void BFS(Node root){
LinkedList<Object> list= new LinkedList<>();
if(root==null){
return ;
}
root.visited = true;
print(root);
list.add(root);
while(!list.isEmpty()){
Node r = (Node) list.remove();
if(r.left!=null){
print(r.left);
list.add(r.left);
}
if(r.right!=null){
print(r.right);
list.add(r.right);
}
}
}
//树的深度优先搜索
public void DFS(Node root){
if(root == null){
return ;
}
root.visited=true;
print(root);
if(root.left!=null){
DFS(root.left);
}
if(root.right!=null){
DFS(root.right);
}
}
public void print(Node root) {
System.out.print(root.value);
}
public static void main(String[] args) {
root = new BinaryTree().createTree(root);
System.out.println("\n前序遍历为:");
new BinaryTree().preOrder( root);
System.out.println("\n中序遍历为:");
new BinaryTree().InOrder(root);
System.out.println("\n后序遍历为");
new BinaryTree().PostOrder(root);
System.out.println("\nDFS:");
new BinaryTree().DFS(root);
System.out.println("\nBFS:");
new BinaryTree().BFS(root);
System.out.println("\n树高"+getHeight(root));
}
}
图
//广度优先搜索
public void BFS(Node start){
Queue queue = new Queue();
if(start==null){
return ;
}
visited[start] = true;
print(start);
queue.enqueue(start);
while(!queue.isEmpty()){
Node r = queue.dequeue();
for(Node n:r.adj()){
if(visited[n]==false){
print(n);
visited[n] = true;
queue.enqueue(n);
}
}
}
}
//深度优先搜索
public void DFS(Node start){
if(start==null){
return ;
}
visited[start]=true;
print(start);
for(Node n:start.adj()){
if(visited[n]==false){
DFS(n);
}
}
}