C语言角色互换指针,树移动指针中两个随机选择节点的角色交换算法

我创建了一个算法,它的目的应该是,给定BST中的两个节点A和B,它通过简单地移动指针来切换这两个节点的角色(或树中的位置)。在我对BST的表示中,我使用了一个双链接连接(即a.parent==B和(B.left==a)或(B.right==a))。我不确定这是否完全正确。我把算法分为两种情况。在A和B直接相连(A是B的父项,B是A的父项)

所有其他案例

对于前面的每个例子,我都创建了一个嵌套函数。我想先听听你对算法正确性的看法,如果我能改进的话。代码如下:def switch(self, x: BSTNode, y: BSTNode, search_first=False):

if not x:

raise ValueError("x cannot be None.")

if not y:

raise ValueError("y cannot be None.")

if x == y:

raise ValueError("x cannot be equal to y")

if search_first:

if not self.search(x.key) or not self.search(y.key):

raise LookupError("x or y not found.")

def switch_1(p, s):

"""Switches the roles of p and s,

where p (parent) is the direct parent of s (son)."""

assert s.parent == p

if s.is_left_child():

p.left = s.left

if s.left:

s.left.parent = p

s.left = p

s.right, p.right = p.right, s.right

if s.right:

s.right.parent = s

if p.right:

p.right.parent = p

else:

p.right = s.right

if s.right:

s.right.parent = p

s.right = p

s.left, p.left = p.left, s.left

if s.left:

s.left.parent = s

if p.left:

p.left.parent = p

if p.parent:

if p.is_left_child():

p.parent.left = s

else:

p.parent.right = s

else: # p is the root

self.root = s

s.parent = p.parent

p.parent = s

def switch_2(u, v):

"""u and v are nodes in the tree

that are not related by a parent-son

or a grandparent-son relantionships."""

if not u.parent:

self.root = v

if v.is_left_child():

v.parent.left = u

else:

v.parent.right = u

elif not v.parent:

self.root = u

if u.is_left_child():

u.parent.left = v

else:

u.parent.right = v

else: # neither u nor v is the root

if u.is_left_child():

if v.is_left_child():

v.parent.left, u.parent.left = u, v

else:

v.parent.right, u.parent.left = u, v

else:

if v.is_left_child():

v.parent.left, u.parent.right = u, v

else:

v.parent.right, u.parent.right = u, v

v.parent, u.parent = u.parent, v.parent

u.left, v.left = v.left, u.left

u.right, v.right = v.right, u.right

if u.left:

u.left.parent = u

if u.right:

u.right.parent = u

if v.left:

v.left.parent = v

if v.right:

v.right.parent = v

if x.parent == y:

switch_1(y, x)

elif y.parent == x:

switch_1(x, y)

else:

switch_2(x, y)

我真的需要switch在任何情况下都能工作,不管我们选择哪个节点x还是{}。我已经做了一些测试,似乎有用,但我还是不确定。在

编辑

最后,如果有帮助的话,这里有我的BST的完整实现(和我正在进行的测试):

编辑2(只是好奇)

@Rishav评论道:I do not understand the intention behind this function.. if it is to swap two nodes in the BST, is it not sufficient to swap their data instead of manipulating pointers?

我回答说:Ok, maybe I should have added a little bit more about the reason behind all this "monster" function. I can insert BSTNode objects or any comparable objects in my BST. When the user decides to insert any comparable object, the responsibility of creating the BSTNode is mine, therefore the user has no access to a initial BSTNode reference, unless they search for the key. But a BSTNode would only be returned after the insertion of the key, or there's already another BSTNode object in the tree with the same key (or value), but this latter case is irrelevant.

The user can also insert a BSTNode object in the tree which has an initial (and should remain constant) key (or value). Nevertheless, if I just exchanged the values or keys of the nodes, the user would have a reference to a node with a different key then the key of the node he inserted. Of course, I want to avoid this.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值