字节跳动面试题-算法篇(一)输入一个SQL语句,将where后面的查询条件解析为一棵规则树

题目

输入一个SQL语句,将 where 后面的查询条件解析为一棵规则树。

例如:where (a == b and c == d) or (e == f or (g == h)),需要生成一棵树,结构为

or
├── and
│   ├── a == b
│   └── c == d
└── or
    ├── e == f
    └── g == h

解答

本题考查了树和递归,首先要自定义一颗树,其次利用递归进行解析。

下面是 Java 实现。

import java.util.ArrayList;
import java.util.List;

class TreeNode {
    String value;
    List<TreeNode> children;

    public TreeNode(String value) {
        this.value = value;
        this.children = new ArrayList<>();
    }
}

public class SQLConditionParser {

    public static void main(String[] args) {
        String sqlCondition = "where (a == b and c == d) or (e == f or (g == h))";
        TreeNode ruleTree = parseSQLCondition(sqlCondition);
        printTree(ruleTree);
    }

    public static TreeNode parseSQLCondition(String condition) {
        condition = condition.trim().toLowerCase();
        if (condition.startsWith("where ")) {
            condition = condition.substring(6); // Remove "where" keyword if present
        }

        return parseLogicalExpression(condition);
    }

    public static TreeNode parseLogicalExpression(String expression) {
        expression = expression.trim();
        TreeNode root = null;
        TreeNode currentNode = null;
        List<TreeNode> stack = new ArrayList<>();

        for (String token : expression.split("\\s+")) {
            if (token.equals("(")) {
                TreeNode newNode = new TreeNode(null);
                if (currentNode != null) {
                    currentNode.children.add(newNode);
                }
                stack.add(currentNode);
                currentNode = newNode;
            } else if (token.equals(")")) {
                if (!stack.isEmpty()) {
                    currentNode = stack.remove(stack.size() - 1);
                }
            } else if (token.equals("and") || token.equals("or")) {
                if (currentNode != null) {
                    currentNode.value = token;
                    TreeNode newNode = new TreeNode(null);
                    currentNode.children.add(newNode);
                    currentNode = newNode;
                }
            } else {
                TreeNode conditionNode = new TreeNode(token);
                if (currentNode != null) {
                    currentNode.children.add(conditionNode);
                }
            }
        }

        return root;
    }

    public static void printTree(TreeNode node, int level) {
        if (node != null) {
            for (int i = 0; i < level; i++) {
                System.out.print("\t");
            }
            System.out.println(node.value);
            for (TreeNode child : node.children) {
                printTree(child, level + 1);
            }
        }
    }

    public static void printTree(TreeNode node) {
        printTree(node, 0);
    }
}

力扣习题

536. 从字符串生成二叉树

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

腐烂的橘子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值