qq_41380554 2019-10-24 14:11 采纳率: 0%
浏览 246

输入字符串构建两个二叉搜索树

A tempting approach to maintaining a balanced binary search tree is to maintain two binary search trees and to insert each new key into the tree that will be more balanced. More specifically, do the following: The first key is the root of the left tree. The second key is the root of the right tree. To add a new key, insert it into the tree where it would have a smaller depth. If the depths of both trees are the same, then add it to the first tree.The input will be 5 strings (ignore everything but the letters A through Z and a through z; uppercase and lowercase are the same). For each input string, build the two trees with each string as described above. Print contents of the first tree in preorder (root, then the left child, then the right child).
Sample Input:
Line #1: AMERICAN COMPUTER SCIENCE
Line #2: I must say that I find television very educational
Line #3: INTERMEDIATE DIVISION
Sample Output:
Output #1: A A E C C E E N I P S
Output #2: I A A A A I D D C E E I U T T S R U Y V Y
Output #3: I E E A I I I T T V
维护一个平衡的二叉搜索树的一个诱人的方法是维护两个二叉搜索树,并将每个新键插入树中,这样会更加平衡。更具体地说,执行以下操作:第一个键是左树的根。第二个键是右树的根。若要添加新键,请将其插入到树中其深度较小的位置。如果两棵树的深度相同,则将其添加到第一棵树上。

输入将是5个字符串(忽略除了字母A到Z和A到Z;大写和小写是一样的)。对于每个输入字符串,使用上面描述的每个字符串构建两个树。按顺序打印第一个树的内容(根,然后左子树,然后右子树)。
样例输入:
第一行:美国计算机科学
我得说我觉得电视很有教育意义
第3行:中间部分
样例输出:
输出#1:A A E C C E E N I P S
输出#2:I A A A I D D C E E I U T S R U Y V Y
输出#3:ieia I I T T V
基本要求:算法合理,结果准确

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 19:26
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决