二叉树-前中后序查找

public class BinaryTreeSearch {

    public static void main(String[] args) {
        //创建一颗二叉树
        BinarySearch binarySearch = new BinarySearch();
        //创建需要的节点
        Data root = new Data(0);
        Data one = new Data(1);
        Data two = new Data(2);
        Data three = new Data(3);
        Data four = new Data(4);
        Data five = new Data(5);
        Data six = new Data(6);
        Data seven = new Data(7);
        Data eight = new Data(8);

        //建立树结构
        /*       随意创建一颗二叉树
                        0
                      /   \
                     1     2
                    / \   / \
                   3   4  5  6
                    \    /
                     7   8
         */
        binarySearch.setRoot(root);
        root.left = one;
        root.right = two;
        one.left = three;
        one.right = four;
        two.left = five;
        two.right = six;
        three.right = seven;
        five.left = eight;

        System.out.println("-----先序查找-----");
        System.out.println("查到数据:" + binarySearch.preSearch(5));
        System.out.println("查找次数:" + binarySearch.count());
        System.out.println("-----中序查找-----");
        System.out.println("查到数据:" + binarySearch.infixSearch(0));
        System.out.println("查找次数:" + binarySearch.count());
        System.out.println("-----后序查找-----");
        System.out.println("查到数据:" + binarySearch.postSearch(0));
        System.out.println("查找次数:" + binarySearch.count());

    }

}

class BinarySearch {
    public Data root;

    public void setRoot(Data root) {
        this.root = root;
    }

    public int count() {
        int n = Data.count;
        Data.count = 0;
        return n;
    }

    public Data preSearch(int n) {
        if (root != null) {
            return root.preSearch(n);
        } else {
            System.out.println("二叉树为空,无法遍历~");
            return null;
        }
    }

    public Data infixSearch(int n) {
        if (root != null) {
            return root.infixSearch(n);
        } else {
            System.out.println("二叉树为空,无法遍历~");
            return null;
        }
    }

    public Data postSearch(int n) {
        if (root != null) {
            return root.postSearch(n);
        } else {
            System.out.println("二叉树为空,无法遍历~");
            return null;
        }
    }
}

class Data {
    public int num;
    public Data left;
    public Data right;

    public static int count;

    public Data(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return num + "";
    }

    /*
    前序查找
     */
    public Data preSearch(int n) {
        count++;
        if (this.num == n) {
            return this;
        }
        Data temp = null;
        if (this.left != null) {
            temp = this.left.preSearch(n);
        }
        if (temp != null) {
            return temp;
        }
        if (this.right != null) {
            temp = this.right.preSearch(n);
        }
        return temp;
    }

    /*
    中序查找
     */
    public Data infixSearch(int n) {
        Data temp = null;
        if (this.left != null) {
            temp = this.left.infixSearch(n);
        }
        if (temp != null) {
            return temp;
        }
        count++;
        if (this.num == n) {
            return this;
        }
        if (this.right != null) {
            temp = this.right.infixSearch(n);
        }
        return temp;
    }

    /*
    后序查找
     */
    public Data postSearch(int n) {
        Data temp = null;
        if (this.left != null) {
            temp = this.left.postSearch(n);
        }
        if (temp != null) {
            return temp;
        }
        if (this.right != null) {
            temp = this.right.postSearch(n);
        }
        if (temp != null) {
            return temp;
        }
        count++;
        if (this.num == n) {
            return this;
        }
        return temp;
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
叉树的遍历方式有三种:遍历、遍历和后遍历。 遍历:先遍历根节点,然后遍历左子树,最后遍历右子树。其遍历顺为:根节点 -> 左子树 -> 右子树。 遍历:先遍历左子树,然后遍历根节点,最后遍历右子树。其遍历顺为:左子树 -> 根节点 -> 右子树。 后遍历:先遍历左子树,然后遍历右子树,最后遍历根节点。其遍历顺为:左子树 -> 右子树 -> 根节点。 下面是二叉树、后遍历的递归实现代码: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def preorderTraversal(root: TreeNode) -> List[int]: res = [] def dfs(node): if not node: return res.append(node.val) dfs(node.left) dfs(node.right) dfs(root) return res def inorderTraversal(root: TreeNode) -> List[int]: res = [] def dfs(node): if not node: return dfs(node.left) res.append(node.val) dfs(node.right) dfs(root) return res def postorderTraversal(root: TreeNode) -> List[int]: res = [] def dfs(node): if not node: return dfs(node.left) dfs(node.right) res.append(node.val) dfs(root) return res ``` 以上代码,TreeNode 表示二叉树的节点,preorderTraversal、inorderTraversal 和 postorderTraversal 分别表示遍历、遍历和后遍历的函数。每个函数都是通过递归实现的,dfs 函数用来遍历节点,将节点的值加入 res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值