java二叉搜索树

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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值