import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
class Node {
public int val;
public Node left;//左指针
public Node right;//右指针
public Node(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
//二叉搜索树
class BinaryTree {
public Node root;//根结点
//增加函数
public void append(int val) {
Node node = new Node(val);//新增的结点
Node cur = root;//记录当前结点
Node parent = null;//记录父结点的位置
if (cur == null) {
root = node;
return;
}
while (cur != null) {
if (cur.val > val) {
parent = cur;
cur = cur.left;
} else if (cur.val < val) {
parent = cur;
cur = cur.right;
} else {
return;
}
}
if (parent.val > val) {
parent.left = node;
} else {
parent.right = node;
}
}
//递归先序遍历
public void preOrder(Node root) {
if (root == null) {
return;
}
System.out.println(root.val);
preOrder(root.left);
preOrder(root.right);
}
//递归中序遍历
public void inOrder(Node root) {
if (root == null) {
return;
}
inOrder(root.left);
System.out.println(root.val);
inOrder(root.right);
}
//递归后序遍历
public void postOrder(Node root) {
if (root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.println(root.val);
}
//迭代法先序遍历
public void preOrderIterator() {
if (this.root == null) {
return;
}
Stack<Node> stack = new Stack<>();//申明一个栈
stack.push(root);//把根结点放入栈中
while (!stack.empty()) {
Node node = stack.peek();//获取栈顶元素
if (node != null) {
stack.pop();
//把右左中依次压入栈,因为栈的结构所以逆序入栈
if (node.right != null) {
stack.push(node.right);//空结点不入栈
}
if (node.left != null) {
stack.push(node.left);//空结点不入栈
}
stack.push(node);//压入父结点
stack.push(null);//再压入null标记,方便else识别
} else {
node = stack.peek();//弹掉null后再重新获取栈顶的值
System.out.println(node.val);
stack.pop();//获取值后弹掉栈顶元素
}
}
}
//迭代法中序遍历
public void inOrderIterator() {
if (this.root == null) {
return;
}
Stack<Node> stack = new Stack<>();
stack.push(this.root);
while (!stack.empty()) {
Node node = (Node) stack.peek();
stack.pop();
if (node != null) {
if (node.right != null) {
stack.push(node.right);
}
stack.push(node);
stack.push(null);
if (node.right != null) {
stack.push(node.left);
}
} else {
node = (Node) stack.peek();
System.out.println(node.val);
stack.pop();
}
}
}
//迭代法后序遍历
public void postOrderIterator() {
if (this.root == null) {
return;
}
Stack<Node> stack = new Stack<>();
stack.push(this.root);
while (!stack.empty()) {
Node node = stack.peek();
stack.pop();
if (node != null) {
stack.push(node);
stack.push(null);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
} else {
node = stack.peek();
System.out.println(node.val);
stack.pop();
}
}
}
//层序遍历
public void levelTraversal() {
if (this.root == null) {
return;
}
Queue<Node> queue = new LinkedList();//使用队列先先进先出的思想
queue.offer(this.root);//添加根结点
while (!queue.isEmpty()) {
Node node = queue.peek();//获取队列第一个元素
System.out.println(node.val);
queue.remove();//移除队列第一个元素
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
//翻转二叉树
public void invertTree() {
if (this.root == null) {
return;
}
Queue<Node> queue = new LinkedList();
queue.offer(this.root);
while (!queue.isEmpty()) {
Node node = queue.peek();
queue.remove();
//交换左右孩子
Node temp = node.left;
node.left = node.right;
node.right = temp;
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
}
public class Main {
public static void main(String[] args) {
//测试代码
BinaryTree binaryTree = new BinaryTree();
//10,6,16,3,9,14,19,13
binaryTree.append(10);
binaryTree.append(6);
binaryTree.append(16);
binaryTree.append(3);
binaryTree.append(9);
binaryTree.append(14);
binaryTree.append(19);
binaryTree.append(13);
binaryTree.inOrder(binaryTree.root);
System.out.println("-----");
binaryTree.levelTraversal();
System.out.println("-----");
binaryTree.invertTree();
binaryTree.levelTraversal();
//binaryTree.postOrderIterator();
}
}
java二叉搜索树
于 2023-05-23 21:01:57 首次发布