Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BST remove函数,尝试添加奇数和偶数以确定要进入哪个循环_Python_Methods_Counter - Fatal编程技术网

Python BST remove函数,尝试添加奇数和偶数以确定要进入哪个循环

Python BST remove函数,尝试添加奇数和偶数以确定要进入哪个循环,python,methods,counter,Python,Methods,Counter,我正在尝试做的,我还没有完成,但我现在有以下代码: def delete(self): t=1 if isinstance(self.root,odd): child=self.left grandchild=child.right if self.left == self.right==None: return None if self.left==None:

我正在尝试做的,我还没有完成,但我现在有以下代码:

def delete(self):
        t=1
        if isinstance(self.root,odd):
        child=self.left
        grandchild=child.right
        if self.left == self.right==None:
            return None
        if self.left==None:
            return self.right
        if self.right==None:
            return self.left
        if grandchild:
            while grandchild.right:
                child = grandchild
                grandchild = child.right
            self.data = grandchild.data
            child.right = grandchild.left
        else:
            self.left = child.left
            self.data = child.data
        return self
        if isinstance(self.root,even):

我没有完成代码,但是对于作业,它应该做两个选择(基本上是一样的,但是我不会先向左,然后尽量向右,我会先向右,然后尽量向左)。我的想法是在每个循环之后加上t+=1,如果这个数字是奇数,我希望它进入第一个循环,如果它是偶数,我希望它进入另一个循环(我还没有做那个循环)。我能很容易地做到这一点吗(因为我想做的,如果isinstance(t,偶数)和isinstance(t,奇数),但是python似乎没有这种语法?有什么想法吗?

根据我的理解,你想知道t是奇数还是偶数

你应该使用


我希望这能回答你的问题。

根据我的理解,你想知道t是奇数还是偶数

你应该使用


我希望这能回答您的问题。

t%2
是奇数,
非t%2
是偶数
t%2
是奇数,
非t%2
是偶数感谢您的帮助!
if t%2 == 0: # then it is even (divisible by 2)
if t%2 == 1: # it is odd (not divisible by 2)