二叉排序树删除并插入某结点是否与原树相同?(简答)

二叉排序树删除并插入某结点是否与原树相同?

答:二叉排序树删除并插入某结点,得到的二叉排序树与原来有可能相同,也可能不同。

解释:排序二叉树可以删除树中任意结点,但插入的新结点一定是某个叶结点。所以删除并插入某结点的二叉排序树可能与原树相同,也可能不同。

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
/* * AVL树 * 基于BSTree的扩充 */ package dsa; public class AVLTree extends BSTree implements Dictionary { /**************************** 构造方法 ****************************/ public AVLTree() { super(); } public AVLTree(BinTreePosition r) { super(r); } public AVLTree(BinTreePosition r, Comparator c) { super(r, c); } /**************************** 词典方法(覆盖父类BSTree) ****************************/ //插入条目(key, value),并返回该条目 public Entry insert(Object key, Object value) { Entry e = super.insert(key, value);//调用父类方法完成插入 root = rebalance(lastV.getParent(), root);//从插入节点的父亲开始重新平衡化 return e; } //若词典存在以key为关键码的条目,则将摘除其的一个并返回;否则,返回null public Entry remove(Object key) { Entry e = super.remove(key);//调用父类方法完成删除 if (null != e) root = rebalance(lastV, root);//从删除节点的父亲开始重新平衡化 return e; } /**************************** 辅助方法 ****************************/ //从节点z开始,自上而下重新平衡化 //返回后,root仍为平衡后的(整棵)树的根节点 protected static BinTreePosition rebalance(BinTreePosition z, BinTreePosition r) { if (null == z) return r; while (true) {//从z开始,向上逐一检查z的祖先 if (!isBalanced(z)) rotate(z);//若z节点失去平衡,则通过旋转使之重新平衡 if (!z.hasParent()) return z; z = z.getParent();//继续检查其父亲 }//while } //判断节点v是否平衡 protected static boolean isBalanced(BinTreePosition v) { if (null == v) return true; int lH = (v.hasLChild()) ? (v.getLChild().getHeight()) : -1; int rH = (v.hasRChild()) ? (v.getRChild().getHeight()) : -1; int deltaH = lH - rH; return (-1 <= deltaH) && (deltaH <= 1); } //通过旋转,使节点z的平衡因子的绝对值不超过1(支持AVL树) //返回新的子树根 public static BinTreePosition rotate(BinTreePosition z) { BinTreePosition y = tallerChild(z);//取y为z更高的孩子 BinTreePosition x = tallerChild(y);//取x为y更高的孩子 boolean cType = z.isLChild();//记录:z是否左孩子 BinTreePosition p = z.getParent();//p为z的父亲 BinTreePosition a, b, c;//自左向右,三个节点 BinTreePosition t0, t1, t2, t3;//自左向右,四棵子树 /******** 以下分四种情况 ********/ if (y.isLChild()) {//若y是左孩子,则 c = z; t3 = z.getRChild(); if (x.isLChild()) {//若x是左孩子 b = y; t2 = y.getRChild(); a = x; t1 = x.getRChild(); t0 = (BSTreeNode)x.getLChild(); } else {//若x是右孩子 a = y; t0 = y.getLChild(); b = x; t1 = x.getLChild(); t2 = (BSTreeNode)x.getRChild(); } } else {//若y是右孩子,则 a = z; t0 = z.getLChild(); if (x.isRChild()) {//若x是右孩子 b = y; t1 = y.getLChild(); c = x; t2 = x.getLChild(); t3 = (BSTreeNode)x.getRChild(); } else {//若x是左孩子 c = y; t3 = y.getRChild(); b = x; t1 = x.getLChild(); t2 = (BSTreeNode)x.getRChild(); } } //摘下三个节点 z.secede(); y.secede(); x.secede(); //摘下四棵子树 if (null != t0) t0.secede(); if (null != t1) t1.secede(); if (null != t2) t2.secede(); if (null != t3) t3.secede(); //重新链接 a.attachL(t0); a.attachR(t1); b.attachL(a); c.attachL(t2); c.attachR(t3); b.attachR(c); //子树重新接入原树 if (null != p) if (cType) p.attachL(b); else p.attachR(b); return b;//返回新的子树根 }//rotate //返回节点p的孩子的更高者 protected static BinTreePosition tallerChild(BinTreePosition v) { int lH = v.hasLChild() ? v.getLChild().getHeight() : -1; int rH = v.hasRChild() ? v.getRChild().getHeight() : -1; if (lH > rH) return v.getLChild(); if (lH < rH) return v.getRChild(); if (v.isLChild()) return v.getLChild(); else return v.getRChild(); } //返回节点p的孩子的更矮者 protected static BinTreePosition shorterChild(BinTreePosition v) { int lH = v.hasLChild() ? v.getLChild().getHeight() : -1; int rH = v.hasRChild() ? v.getRChild().getHeight() : -1; if (lH > rH) return v.getRChild(); if (lH < rH) return v.getLChild(); if (v.isLChild()) return v.getRChild(); else return v.getLChild(); } }
假设树的节点定义如下: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right ``` 可以考虑使用深度优先搜索(DFS)的方式进行遍历和删除。 具体而言,我们可以从根节点开始进行深度优先搜索,并记录下访问的节点。当我们遍历到一个叶子节点时,就将其删除,并返回其父节点。如果父节点的左右子节点都被删除了,那么我们也将其删除,并返回其父节点。如此往复,直到整棵树都被删除完。 下面是具体的实现: ```python def del_leaf_nodes(root): if not root: return [] res = [] stack = [root] while stack: node = stack[-1] if not node.left and not node.right: res.append(node.val) stack.pop() if stack: parent = stack[-1] if parent.left == node: parent.left = None else: parent.right = None else: if node.right: stack.append(node.right) node.right = None if node.left: stack.append(node.left) node.left = None return res ``` 其,`res` 用于存放删除的顺序,`stack` 用于存放待访问的节点。我们从根节点开始入栈,然后不断弹出栈顶元素进行处理。 如果当前节点是叶子节点,我们将其删除,并记录下其值,并将其父节点弹出栈。如果其父节点的左右子节点都被删除了,那么我们也将其删除,并返回其父节点。如果当前节点不是叶子节点,我们则按照右子节点先入栈的顺序,依次将其右子节点和左子节点入栈。 最后,我们返回记录下来的删除顺序即可,同时原树已经被完全删除。如果需要输出删除顺序,可以将其转换为字符串形式,如下所示: ```python root = TreeNode(1, TreeNode(2, TreeNode(4), TreeNode(5)), TreeNode(3, TreeNode(6), TreeNode(7))) res = del_leaf_nodes(root) print("->".join(map(str, res))) # 输出:4->5->2->6->7->3->1 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一北_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值