stack
1.ArrayStackDemo
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(4);
String key = "";
boolean loop = true;
Scanner scanner = new Scanner(System.in);
while (loop) {
System.out.println("show: 显示栈");
System.out.println("exit: 退出");
System.out.println("push: 入栈");
System.out.println("pop: 出栈");
key = scanner.next();
switch (key) {
case "show":
arrayStack.list();
break;
case "exit":
loop = false;
break;
case "push":
System.out.print("请输入一个数:");
int value = scanner.nextInt();
arrayStack.push(value);
break;
case "pop":
arrayStack.pop();
break;
default:
break;
}
System.out.println("程序退出~~");
}
}
}
class ArrayStack {
private int maxSize;
private int top = -1;
private int[] stack;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
public boolean isFull() {
return top == maxSize - 1;
}
public boolean isEmpty() {
return top == -1;
}
public int peek() {
return stack[top];
}
public void push(int value) {
if (isFull()) {
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
public void list() {
if (isEmpty()) {
System.out.println("栈空");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("栈[%d] = %d\n", i, stack[i]);
}
}
public int priority(int oper) {
if (oper == '*' || oper == '/') {
return 1;
} else if (oper == '+' || oper == '-') {
return 0;
} else {
return -1;
}
}
public boolean isOper(char value) {
return value == '+' || value == '-' || value == '*' || value == '/';
}
public int cal(int num1, int num2, int oper) {
int result = 0;
switch (oper) {
case '+':
result = num1 + num2;
break;
case '-':
result = num2 - num1;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num2 / num1;
break;
default:
break;
}
return result;
}
}
2.Calculator
public class Calculator {
public static void main(String[] args) {
String expression = "7+20*4+4";
ArrayStack numStack = new ArrayStack(10);
ArrayStack operStack = new ArrayStack(10);
int index = 0;
int num1 = 0;
int num2 = 0;
int oper = 0;
int result = 0;
char ch = ' ';
String keepNum = "";
while (true) {
ch = expression.substring(index, index + 1).charAt(0);
if (operStack.isOper(ch)) {
if (!operStack.isEmpty()) {
if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
result = numStack.cal(num1, num2, oper);
numStack.push(result);
operStack.push(ch);
} else {
operStack.push(ch);
}
} else {
operStack.push(ch);
}
} else {
keepNum += ch;
if (index == expression.length() - 1) {
numStack.push(Integer.parseInt(keepNum));
} else {
if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
index++;
if (index >= expression.length()) {
break;
}
}
while (true) {
if (operStack.isEmpty()) {
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = (char) operStack.pop();
result = numStack.cal(num1, num2, oper);
numStack.push(result);
}
int resFinally = numStack.pop();
System.out.printf("表达式:%s=%d", expression, resFinally);
}
}
3.PolandNotation
public class PolandNotation {
public static void main(String[] args) {
String expression = "1+((2+3)*4)-5";
List<String> list = toInfixExpressionList(expression);
System.out.println(list);
List<String> strings = pareSuffixExpressionList(list);
System.out.println(strings);
System.out.println(calculator(strings));
}
public static List<String> pareSuffixExpressionList(List<String> list) {
Stack<String> s1 = new Stack<>();
ArrayList<String> s2 = new ArrayList<>();
for (String item : list) {
if (item.matches("\\d+")) {
s2.add(item);
} else if (item.equals("(")) {
s1.push(item);
} else if (item.equals(")")) {
while (!s1.peek().equals("(")){
s2.add(s1.pop());
}
s1.pop();
}else {
while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)){
s2.add(s1.pop());
}
s1.push(item);
}
}
while (s1.size() != 0){
s2.add(s1.pop());
}
return s2;
}
public static List<String> toInfixExpressionList(String infixExpression) {
ArrayList<String> list = new ArrayList<>();
int i = 0;
String str;
char c;
do {
if ((c = infixExpression.charAt(i)) < 48 || (c = infixExpression.charAt(i)) > 57) {
list.add(c + "");
i++;
} else {
str = "";
while (i < infixExpression.length() && (c = infixExpression.charAt(i)) >= 48 && (c = infixExpression.charAt(i)) <= 57) {
str += c;
i++;
}
list.add(str);
}
} while (i < infixExpression.length());
return list;
}
public static List<String> getListString(String suffixExpression) {
String[] split = suffixExpression.split(" ");
List<String> list = new ArrayList<String>();
for (String item : split) {
list.add(item);
}
return list;
}
public static int calculator(List<String> list) {
Stack<String> stack = new Stack<String>();
for (String item : list) {
if (item.matches("\\d+")) {
stack.push(item);
} else {
int num1 = Integer.parseInt(stack.pop());
int num2 = Integer.parseInt(stack.pop());
int result = 0;
if (item.equals("+")) {
result = num1 + num2;
} else if (item.equals("-")) {
result = num2 - num1;
} else if (item.equals("*")) {
result = num1 * num2;
} else if (item.equals("/")) {
result = num2 / num1;
} else {
throw new RuntimeException("传入的运算符不合理");
}
stack.push(result + "");
}
}
return Integer.parseInt(stack.pop());
}
}
class Operation {
private static int ADD = 1;
private static int SUB = 1;
private static int MUL = 2;
private static int DIV = 2;
public static int getValue(String operation) {
int result = 0;
switch (operation) {
case "+":
result = ADD;
break;
case "-":
result = SUB;
break;
case "*":
result = MUL;
break;
case "/":
result = DIV;
break;
default:
System.out.println("不存在该运算符");
break;
}
return result;
}
}
linkedlist
1.LinkedList:单链表
public class LinkedList {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "吴用", "智多星");
HeroNode heroNode3 = new HeroNode(5, "林冲", "豹子头");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.addHeroNode(heroNode1);
singleLinkedList.addHeroNode(heroNode2);
singleLinkedList.addHeroNode(heroNode3);
singleLinkedList.list();
System.out.println("----------------------------------------------");
singleLinkedList.delete(2);
singleLinkedList.list();
}
}
class SingleLinkedList {
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return head;
}
public void addHeroNode(HeroNode heroNode) {
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
public void addHeroNodeOrder(HeroNode heroNode) {
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > heroNode.no) {
break;
} else if (temp.next.no == heroNode.no) {
flag = true;
}
temp = temp.next;
}
if (flag) {
System.out.printf("英雄%d已存在\n", heroNode.no);
} else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
public void update(HeroNode newHeroNode) {
if (head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.no == newHeroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = newHeroNode.name;
temp.nickName = newHeroNode.nickName;
} else {
System.out.printf("没有找到排名%d的英雄\n", newHeroNode.no);
}
}
public void delete(int no) {
HeroNode temp = head.next;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.printf("没有找到排名%d的英雄", no);
}
}
public void list() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
System.out.println(temp);
}
}
}
class HeroNode {
int no;
String name;
String nickName;
HeroNode next;
public HeroNode(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
2.DoubleLinkedList:双链表
public class DoubleLinkedList {
public static void main(String[] args) {
HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 heroNode2 = new HeroNode2(2, "吴用", "智多星");
HeroNode2 heroNode3 = new HeroNode2(5, "林冲", "豹子头");
DoubleLinkedListManager doubleLinkedListManager = new DoubleLinkedListManager();
doubleLinkedListManager.addHeroNode(heroNode1);
doubleLinkedListManager.addHeroNode(heroNode2);
doubleLinkedListManager.addHeroNode(heroNode3);
doubleLinkedListManager.list();
System.out.println("----------------------------------------------");
if (doubleLinkedListManager.addHeroNodeOrder(new HeroNode2(1, "笑笑", "帅帅"))){
doubleLinkedListManager.list();
}
}
}
class DoubleLinkedListManager {
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
public void addHeroNode(HeroNode2 heroNode) {
HeroNode2 temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
heroNode.pre = temp;
}
public boolean addHeroNodeOrder(HeroNode2 heroNode){
HeroNode2 temp = head;
boolean flag = false;
boolean flag1 = false;
while (true){
if (temp.next == null){
flag1 =true;
break;
}
if (temp.next.no > heroNode.no){
break;
}else if (temp.next.no == heroNode.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
System.out.printf("已经存在排名%d的英雄", heroNode.no);
return false;
}else if (flag1){
temp.next = heroNode;
heroNode.pre = temp;
return true;
}else{
heroNode.next = temp.next;
temp.next.pre = heroNode;
temp.next = heroNode;
heroNode.pre = temp;
}
return true;
}
public void update(HeroNode2 newHeroNode) {
if (head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode2 temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.no == newHeroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = newHeroNode.name;
temp.nickName = newHeroNode.nickName;
} else {
System.out.printf("没有找到排名%d的英雄\n", newHeroNode.no);
}
}
public void delete(int no) {
if (head.next == null){
return;
}
HeroNode2 temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.no == no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.pre.next = temp.next;
if (temp.next != null) {
temp.next.pre = temp.pre;
}
} else {
System.out.printf("没有找到排名%d的英雄", no);
}
}
public void list() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
HeroNode2 temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
System.out.println(temp);
}
}
}
class HeroNode2 {
int no;
String name;
String nickName;
HeroNode2 next;
HeroNode2 pre;
public HeroNode2(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode2{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
3.面试题
public class Interview {
public static int getLinkedLength(HeroNode head) {
if (head.next == null) {
System.out.println("该链表为空");
return 0;
}
HeroNode temp = head.next;
int length = 0;
while (temp != null) {
length++;
temp = temp.next;
}
return length;
}
public static HeroNode getHeroNode(HeroNode head, int index) {
if (head.next == null) {
System.out.println("链表为空");
return null;
}
int size = getLinkedLength(head);
if (index < 0 || size < index){
return null;
}
HeroNode current = head.next;
for (int i = 0;i < size - index;i++){
current = current.next;
}
return current;
}
public static void reverseLinkedList(HeroNode head){
if (head.next == null || head.next.next == null){
return;
}
HeroNode current = head.next;
HeroNode next = null;
HeroNode reverseHead = new HeroNode(0, "", "");
while (current != null){
next = current.next;
current.next = reverseHead.next;
reverseHead.next = current;
current = next;
}
head.next = reverseHead.next;
}
public static void reversePrint(HeroNode head){
if (head.next == null){
System.out.println("链表为空");
return;
}
Stack<HeroNode> stack = new Stack<HeroNode>();
HeroNode temp = head.next;
while (temp != null){
stack.push(temp);
temp = temp.next;
}
while (stack.size() > 0){
System.out.println(stack.pop());
}
}
}
recursion
1.RecursionTest
public class RecursionTest {
public static void main(String[] args) {
int result = factorial(3);
}
public static void test(int n) {
if (n > 1) {
test(n - 1);
}
System.out.println("n=" + n);
}
public static int factorial(int n) {
if (n == 1) {
return 1;
} else {
return factorial(n - 1) * n;
}
}
}
2.迷宫问题
public class MiGong {
public static void main(String[] args) {
int[][] map = new int[8][7];
for (int i = 0; i < 7; i++) {
map[0][i] = 1;
map[7][i] = 1;
}
for (int i = 0; i < 8; i++) {
map[i][0] = 1;
map[i][6] = 1;
}
map[3][1] = 1;
map[3][2] = 1;
System.out.println("地图------------");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
getWay(map, 1, 1);
System.out.println("小球额走过的地图------------");
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 7; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
public static boolean getWay(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
} else {
if (map[i][j] == 0) {
map[i][j] = 2;
if (getWay(map, i + 1, j)) {
return true;
} else if (getWay(map, i, j + 1)) {
return true;
} else if (getWay(map, i, j - 1)) {
return true;
} else if (getWay(map, i - 1, j)) {
return true;
} else {
map[i][j] = 3;
return false;
}
} else {
return false;
}
}
}
}
sort
1.冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {3, 9, -1, 20, 10};
}
public static int[] bubbleSortOrigin(int[] arr) {
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
System.out.println("第" + (i + 1) + "趟排序的数组");
System.out.println(Arrays.toString(arr));
}
return arr;
}
public static int[] bubbleSortOptimized(int[] arr) {
int temp = 0;
boolean flag = false;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
flag = true;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
System.out.println("第" + (i + 1) + "趟排序的数组");
System.out.println(Arrays.toString(arr));
if (!flag) {
break;
} else {
flag = false;
}
}
return arr;
}
}
2.选择排序从小到大
public class SelectSort {
public static void main(String[] args) {
int[] arr = new int[200000];
LocalDateTime time1 = LocalDateTime.now();
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 10000);
}
Date date1 = new Date();
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date1Str = simpleDateFormat1.format(date1);
System.out.println("排序前的时间:" + date1Str);
selectSort(arr);
Date date2 = new Date();
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date2Str = simpleDateFormat2.format(date2);
System.out.println("排序后的时间:" + date2Str);
}
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = arr[i];
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (min > arr[j]) {
min = arr[j];
minIndex = j;
}
}
if (minIndex != i) {
arr[minIndex] = arr[i];
arr[i] = min;
}
}
}
}
hashtable
1.HashtableDemo
public class HashtableDemo {
public static void main(String[] args) {
HashTab hashTab = new HashTab(7);
String key = "";
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("add: 添加雇员");
System.out.println("list: 显示雇员");
System.out.println("find: 查找雇员");
System.out.println("exit: 退出系统");
key = scanner.next();
switch (key) {
case "add":
System.out.print("输入id:");
int id = scanner.nextInt();
System.out.print("输入name:");
String name = scanner.next();
Employee employee = new Employee(id, name);
hashTab.add(employee);
break;
case "list":
hashTab.list();
break;
case "find":
System.out.print("请输入查找的id:");
id = scanner.nextInt();
hashTab.findEmpById(id);
break;
case "exit":
scanner.close();
System.exit(0);
break;
default:
break;
}
}
}
}
class HashTab {
private int size;
private EmployeeLinkedList[] empLinkedListArray;
public HashTab(int size) {
this.size = size;
empLinkedListArray = new EmployeeLinkedList[size];
for (int i = 0; i < size; i++) {
empLinkedListArray[i] = new EmployeeLinkedList();
}
}
public void add(Employee employee) {
int empLinkedListNo = hashFun(employee.getId());
empLinkedListArray[empLinkedListNo].add(employee);
}
public void list() {
for (int i = 0; i < size; i++) {
empLinkedListArray[i].list(i);
}
}
public void findEmpById(int id) {
int empLinkedListNo = hashFun(id);
Employee employee = empLinkedListArray[empLinkedListNo].findEmpById(id);
if (employee != null) {
System.out.printf("在第%d条链表找到雇员id=%d\n", empLinkedListNo, id);
} else {
System.out.println("没有找到雇员");
}
}
public int hashFun(int id) {
return id % size;
}
}
class Employee {
private int id;
private String name;
Employee next;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
class EmployeeLinkedList {
private Employee head;
public void add(Employee employee) {
if (head == null) {
head = employee;
return;
}
Employee curEmp = head;
while (true) {
if (curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
curEmp.next = employee;
}
public void list(int num) {
if (head == null) {
System.out.println("第" + num + "条链表无员工");
return;
}
System.out.print("第" + num + "条链表员工信息");
Employee curEmp = head;
while (true) {
System.out.printf("id=>%d,name=>%s\n", curEmp.getId(), curEmp.getName());
if (curEmp.next == null) {
break;
}
curEmp = curEmp.next;
}
}
public Employee findEmpById(int id) {
if (head == null) {
return null;
}
Employee curEmp = head;
while (true) {
if (curEmp.getId() == id) {
break;
}
if (curEmp.next == null) {
curEmp = null;
break;
}
curEmp = curEmp.next;
}
return curEmp;
}
}
tree
1.二叉树遍历
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
HeroNode node5 = new HeroNode(5, "鲁智深");
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
binaryTree.setRoot(root);
System.out.println("删除前=========");
binaryTree.preOrder();
System.out.println("删除后");
binaryTree.deleteHeroNode(5);
binaryTree.preOrder();
}
}
class BinaryTree {
private HeroNode root;
public void setRoot(HeroNode root) {
this.root = root;
}
public void deleteHeroNode(int no) {
if (this.root != null) {
if (this.root.getNo() == no) {
this.root = null;
return;
}
this.root.deleteHeroNode(no);
} else {
System.out.println("空树,无法删除!");
}
}
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("二叉树为空,无法遍历");
}
}
public void infixOrder() {
if (this.root != null) {
this.root.infixOrder();
} else {
System.out.println("二叉树为空,无法遍历");
}
}
public void postOrder() {
if (this.root != null) {
this.root.postOrder();
} else {
System.out.println("二叉树为空,无法遍历");
}
}
public HeroNode preOrderSearch(int no) {
if (this.root != null) {
return this.root.preOrderSearch(no);
} else {
return null;
}
}
public HeroNode infixOrderSearch(int no) {
if (this.root != null) {
return this.root.infixOrderSearch(no);
} else {
return null;
}
}
public HeroNode postOrderSearch(int no) {
if (this.root != null) {
return this.root.postOrderSearch(no);
} else {
return null;
}
}
}
class HeroNode {
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(int no, String name) {
this.no = no;
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public void deleteHeroNode(int no) {
if (this.left != null && this.left.no == no) {
this.left = null;
return;
}
if (this.right != null && this.right.no == no) {
this.right = null;
return;
}
if (this.left != null) {
this.left.deleteHeroNode(no);
}
if (this.right != null) {
this.right.deleteHeroNode(no);
}
}
public void preOrder() {
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
public void postOrder() {
if (this.left != null) {
this.left.postOrder();
}
if (this.right != null) {
this.right.postOrder();
}
System.out.println(this);
}
public HeroNode preOrderSearch(int no) {
if (this.no == no) {
return this;
}
HeroNode resultNode = null;
if (this.left != null) {
resultNode = this.left.preOrderSearch(no);
}
if (resultNode != null) {
return resultNode;
}
if (this.right != null) {
resultNode = this.right.preOrderSearch(no);
}
return resultNode;
}
public HeroNode infixOrderSearch(int no) {
HeroNode resultNode = null;
if (this.left != null) {
resultNode = this.left.infixOrderSearch(no);
}
if (resultNode != null) {
return resultNode;
}
if (this.no == no) {
return this;
}
if (this.right != null) {
resultNode = this.right.infixOrderSearch(no);
}
return resultNode;
}
public HeroNode postOrderSearch(int no) {
HeroNode resultNode = null;
if (this.left != null) {
resultNode = this.left.postOrderSearch(no);
}
if (resultNode != null) {
return resultNode;
}
if (this.right != null) {
resultNode = this.right.postOrderSearch(no);
}
if (resultNode != null) {
return resultNode;
}
if (this.no == no) {
return this;
}
return resultNode;
}
}
2.顺序存储二叉树
public class ArrayBinaryTreeDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7};
ArrayBinaryTree tree = new ArrayBinaryTree(arr);
tree.preOrder();
}
}
class ArrayBinaryTree {
int[] arr;
public ArrayBinaryTree(int[] arr) {
this.arr = arr;
}
public void preOrder() {
this.preOrder(0);
}
public void preOrder(int index) {
if (arr == null || arr.length == 0) {
System.out.println("数组为空,不能按照二叉树前序遍历!");
return;
}
System.out.println(arr[index]);
if ((index * 2 + 1) < arr.length) {
preOrder(index * 2 + 1);
}
if ((index * 2 + 2) < arr.length) {
preOrder(index * 2 + 2);
}
}
}