数据结构 之 二叉搜索树 加 二叉树 前中后序遍历 伪代码以及 递归实现和迭代实现 层序遍历定义 代码 以及 深度优先查询题

二叉树

前序遍历

一直遍历左子树, 路中经过也记录

Function order(Node n){
if(n==null){
return;
}
Syso(n.date)
order(n.left)
order(n.right)
}

中序遍历

一直遍历左子树, 如果没有左子树了 输出 当前 然同理遍历后右子树

Function order(Node n){
if(n==null){
return;
}
order(n.left)
Syso(n.date)
order(n.right)
}

后序遍历

一直遍历左子树, 如果没有左子树了 然同理遍历后右子树 最后 输出当前

Function order(Node n){
if(n==null){
return;
}
order(n.left)
order(n.right)
Syso(n.date)
}

层序遍历

不断入栈 当前出栈则入 栈他的儿子子树

深度遍历

从深度遍历树
在这里插入图片描述

代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
import java.util.ArrayList;
import java.util.List;
class Solution {
       static List<Integer>  list=new ArrayList();
    public List<Integer> rightSideView(TreeNode root) {
         list=new ArrayList();
        inout(root,0);
        return  list;
    }

    private void inout(TreeNode root, int i) {
        if (root==null){
            return;
        }
        if (i==list.size()){
            list.add(root.val);
        }
        inout(root.right, i+1);
        inout(root.left, i+1);
       
    }
}

二分搜索树

将数字按大小存入树里面

话不多说 看代码

package 树形结构;

import java.util.Iterator;
import java.util.LinkedList;

//二分搜索树
public class BinarySearchTree<E extends Comparable<E>> implements Iterable<E> {
    private class Node {
        public E e;
        public Node left, right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }
    }

    //二份搜索树的根节点的指针
    private Node root;
    //二分搜索树的元素的个数
    private int size;

    public BinarySearchTree() {
        root = null;
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0 && root == null;
    }

    public void add(E e) {
        root = add(root, e);
    }

    //向以node为根的二分搜索树中添加ys e
    //并且 返回新结点插入之后的二分搜索树新的根
    private Node add(Node node, E e) {
        if (node == null) {
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        } else if (e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        }
        return node;
    }

    //查看二分搜索树中是否包含元素e
    public boolean contains(E e) {
        return contains(root, e);
    }

    private boolean contains(Node node, E e) {
        if (node == null) {
            return false;
        }
        if (e.compareTo(node.e) == 0) {
            return true;
        }
        if (e.compareTo(node.e) < 0) {
            return contains(node.left, e);
        } else {
            return contains(node.right, e);
        }
    }

    //前序遍历
    public void preOrder() {
        preOrder(root);
        System.out.println();//换行
    }

    private void preOrder(Node node) {
        if (node == null) {
            return;
        }
        System.out.print(node.e + " ");
        preOrder(node.left);
        preOrder(node.right);
    }

    //前序遍历非递归
    public void preOrderNr() {
        LinkedList<Node> stack = new LinkedList<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            Node cur = stack.pop();
            System.out.print(cur.e + " ");
            if (cur.right != null) {
                stack.push(cur.right);
            }
            if (cur.left != null) {
                stack.push(cur.left);
            }
        }
        System.out.println();//换行
    }

    //中序遍历
    public void inOrder() {
        inOrder(root);
        System.out.println();//换行
    }

    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.print(node.e + " ");
        inOrder(node.right);
    }

    //中序遍历非递归
    public void inOrderNr() {
        LinkedList<Node> stack = new LinkedList<>();
        Node p = root;
        while (p != null) {
            stack.push(p);
            p = p.left;
        }
        while (!stack.isEmpty()) {
            Node cur = stack.pop();
            System.out.print(cur.e + " ");
            if (cur.right != null) {
                p = cur.right;
                while (p != null) {
                    stack.push(p);
                    p = p.left;
                }
            }
        }
        System.out.println();//换行
    }

    //后序遍历
    public void postOrder() {
        postOrder(root);
        System.out.println();//换行
    }

    private void postOrder(Node node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.print(node.e + " ");
    }

    //后序遍历非递归
    public void postOrderNr() {
        if (root != null) {
            LinkedList<Node> stack1 = new LinkedList<>();
            LinkedList<Node> stack2 = new LinkedList<>();     // 辅助栈,存储 根 -> 右 -> 左 的结果
            stack1.push(root);
            while (!stack1.isEmpty()) {
                Node head = stack1.pop();
                stack2.push(head);
                // 有左孩子就先压入左孩子
                if (head.left != null)
                    stack1.push(head.left);
                // 有右孩子就后压入右孩子
                if (head.right != null)
                    stack1.push(head.right);
            }
            // 逆序打印 根 -> 右 -> 左 的结果,就是后序遍历的结果
            while (!stack2.isEmpty())
                System.out.print(stack2.pop().e + " ");
        }
        System.out.println();//换行
    }

    //层序遍历
    public void levelOrder() {
        LinkedList<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            Node cur = queue.poll();
            System.out.print(cur.e + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();//换行
    }

    //返回二分搜索树的最小值
    public E minnum() {
        if (isEmpty()) {
            throw new IllegalArgumentException("BST is empty");
        }
        return minnum(root).e;
    }

    private Node minnum(Node node) {
        if (node.left != null) {
            return node;
        } else {
            return minnum(node.left);
        }
    }

    //返回二分搜索树的最小值迭代
    public E minnumNr() {
        if (isEmpty()) {
            throw new IllegalArgumentException("BST is empty");
        }
        Node p = root;
        while (p.left != null) {
            p = p.left;
        }
        return p.e;
    }

    //返回二分搜索树的最大值
    public E maxnum() {
        if (isEmpty()) {
            throw new IllegalArgumentException("BST is empty");
        }
        return maxnum(root).e;
    }

    private Node maxnum(Node node) {
        if (node.right != null) {
            return node;
        } else {
            return maxnum(node.right);
        }
    }

    //返回二分搜索树的最大值迭代
    public E maxnumNr() {
        if (isEmpty()) {
            throw new IllegalArgumentException("BST is empty");
        }
        Node p = root;
        while (p.right != null) {
            p = p.right;
        }
        return p.e;
    }

    public E removeMin() {
        E ret = minnum();
        root = removeMin(root);
        return ret;
    }

    //以node为根结点的二分搜索树中删除最小值,并返回新树的根
    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    public E removeMax() {
        E ret = maxnum();
        root = removeMax(root);
        return ret;
    }

    //以node为根结点的二分搜索树中删除最大值,并返回新树的根
    private Node removeMax(Node node) {
        if (node.right == null) {
            Node leftNode = node.left;
            node.left = null;
            size--;
            return leftNode;
        }
        node.right = removeMin(node.right);
        return node;
    }

    //删除二分搜索树中的任意元素
    public void remove(E e) {
        root = remove(root, e);
    }

    //删除二分搜索树中以node为根结点的bst中的元素e的结点,并返回新树的根
    private Node remove(Node node, E e) {
        if (node == null) {
            return null;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = remove(node.left, e);
            return node;
        } else if (e.compareTo(node.e) > 0) {
            node.right = remove(node.right, e);
            return node;
        } else {
            //如果左子树为空
            if (node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }
            if (node.right == null) {
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }
            //如果左右子树都不为空
            Node successor = minnum(node.right);
            successor.right = removeMin(node.right);
            successor.left = removeMin(node.left);
            node.left = node.right = null;
            return successor;
        }
    }

    public String toString() {
        if (isEmpty()) {
            return "[]";
        }
        StringBuffer sb = new StringBuffer();

        inOrderByString(root, sb);

        return sb.toString();
    }

    private void inOrderByString(Node node, StringBuffer sb) {
        if (node == null) {
            return;
        }
        inOrderByString(node.left, sb);
        sb.append(node.e + " ");
        inOrderByString(node.right, sb);

    }


    @Override
    public Iterator<E> iterator() {
        return new BsTInterayor();
    }

    private class BsTInterayor implements Iterator<E> {
        private LinkedList<E> list = new LinkedList<>();

        public BsTInterayor() {
            LinkedList<Node> stack = new LinkedList<>();
            Node p = root;
            while (p != null) {
                stack.push(p);
                p = p.left;
            }
            while (!stack.isEmpty()) {
                Node cur = stack.pop();
                list.add(cur.e);
                if (cur.right != null) {
                    p = cur.right;
                    while (p != null) {
                        stack.push(p);
                        p = p.left;
                    }
                }
            }
        }

        @Override
        public boolean hasNext() {
            return !list.isEmpty();
        }

        @Override
        public E next() {
            return list.removeFirst();
        }
    }
}


测试类

package text;

import 树形结构.BinarySearchTree;

public class textBst {
    public static void main(String[] args) {
        BinarySearchTree<Integer> tree=new BinarySearchTree();
        tree.add(11);
        tree.add(10);
        tree.add(12);
        tree.postOrderNr();
        System.out.println(tree.removeMin());
        System.out.println(tree.removeMax());
        tree.postOrderNr();
        System.out.println(tree.toString());
        for (Integer i:tree){
            System.out.println(i);
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzsaixuexi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值