Edited, memorised or added to reading list

on 01-Nov-2018 (Thu)

Do you want BuboFlash to help you learning these things? Click here to log in or create user.

Flashcard 3169730039052

Question
In python, you have string, s, how do you get the index of first occurance of substring 'abc' in s (-1 is returned if 'abc' substring is NOT found)?
Answer
s.find('abc')

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3317001751820

Question
In python, the proper way to split a string like "a bunch of words" into a list of words is: [...]
Answer
str.split("a bunch of words")
OR: "a buch of words".split()

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill






Flashcard 3511538814220

Question
In algorithms, for BinarySearchTree class' insert method (def insert(self, data): ), you 1) [...] 2) check to see if root is empty (if so make the new Node root), 3) if root is not empty, create current pointer to root, and parent pointer (to None initially) 4) go into "while True:" loop and in loop make parent the current, 5) in loop, check value of current (compared to new Node data) to see if current is to shift to right or left subtree and if current is None, you found right place to insert (so short circuit loop):
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None

    def insert(self, data):
        # 1. ???????????????????????????????????????????????????????
        # ??????????????????????????????????????????????????????????
        # 2. if root is None, make this new node the root
        if self.root_node == None:            
            self.root_node = node        
        else:
            # 3. if root exists, then create a current pointer initialized to root, and a parent pointer (None initially)
            current = self.root_node        
            parent = None 
            # 4. start a loop to keep going down until you find place to insert new Node, loop will short circuit when place found  
            while True:            
                parent = current
                # 5. in loop compare the new node data to current node data and either go left side
                #    or right side of tree (and if you hit current of None, use the parent pointer to insert new node and
                     short circuit loop, other wise the loop will continue down the tree
                if node.data < current.data:
                    current = current.left_child
                    if current is None:
                        parent.left_child = node
                        return
                else:
                    current = current.right_child
                    if current is None:
                        parent.right_child = node
                        return
Answer
create the Node object from the data
    def insert(self, data):
        # 1. create the Node object with the data
        node = Node(data)

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511542746380

Question
In algorithms, for BinarySearchTree class' insert method (def insert(self, data): ), you 1) create the Node object from the data 2) [...] 3) if root is not empty, create current pointer to root, and parent pointer (to None initially) 4) go into "while True:" loop and in loop make parent the current, 5) in loop, check value of current (compared to new Node data) to see if current is to shift to right or left subtree and if current is None, you found right place to insert (so short circuit loop):
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None


    def insert(self, data):
        # 1. create the Node object with the data
        node = Node(data)
        # 2. ??????????????????????????????????????????????
        ???????????????????????????????????????????????????
        ???????????????????????????????????????????????????       
        else:
            # 3. create a current pointer initialized to root, and a parent pointer (None initially)
            current = self.root_node        
            parent = None 
            # 4. start a loop to keep going down until you find place to insert new Node, loop will short circuit when place found  
            while True:            
                parent = current
                # 5. in loop compare the new node data to current node data and either go left side
                #    or right side of tree (and if you hit current of None, use the parent pointer to insert new node and
                     short circuit loop, other wise the loop will continue down the tree
                if node.data <= current.data:
                    current = current.left_child
                    if current is None:
                        parent.left_child = node
                        return
                else:
                    current = current.right_child
                    if current is None:
                        parent.right_child = node
                        return
Answer
check to see if root is empty (if so make the new Node root):
        # 2. if root is None, make this new node the root
        if self.root_node == None:            
            self.root_node = node        
        else:

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511547464972

Question
In algorithms, for BinarySearchTree class' insert method (def insert(self, data): ), you 1) create the Node object from the data 2) check to see if root is empty (if so make the new Node root), 3) [...] 4) go into "while True:" loop and in loop make parent the current, 5) in loop, check value of current (compared to new Node data) to see if current is to shift to right or left subtree and if current is None, you found right place to insert (so short circuit loop):
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None


    def insert(self, data):
        # 1. create the Node object with the data
        node = Node(data)
        # 2. if root is None, make this new node the root
        if self.root_node == None:            
            self.root_node = node        
        else:
            # 3. ????????????????????????????????????????????????????????????
            ?????????????????????????????????????????????????????????????????
            ????????????????????????????????????????????????????????????????? 
            # 4. start a loop to keep going down until you find place to insert new Node, loop will short circuit when place found  
            while True:            
                parent = current
                # 5. in loop compare the new node data to current node data and either go left side
                #    or right side of tree (and if you hit current of None, use the parent pointer to insert new node and
                     short circuit loop, other wise the loop will continue down the tree
                if node.data <= current.data:
                    current = current.left_child
                    if current is None:
                        parent.left_child = node
                        return
                else:
                    current = current.right_child
                    if current is None:
                        parent.right_child = node
                        return
Answer
if root is not empty, create current pointer to root, and parent pointer (to None initially)
        else:
            # 3. if root exists, then create a current pointer initialized to root, and a parent pointer (None initially)
            current = self.root_node        
            parent = None 

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511551397132

Question
In algorithms, for BinarySearchTree class' insert method (def insert(self, data): ), you 1) create the Node object from the data 2) check to see if root is empty (if so make the new Node root), 3) if root is not empty, create current pointer to root, and parent pointer (to None initially) 4) [...] 5) in loop, check value of current (compared to new Node data) to see if current is to shift to right or left subtree and if current is None, you found right place to insert (so short circuit loop)
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None


    def insert(self, data):
        # 1. create the Node object with the data
        node = Node(data)
        # 2. if root is None, make this new node the root
        if self.root_node == None:            
            self.root_node = node        
        else:
            # 3. if root exists, then create a current pointer initialized to root, and a parent pointer (None initially)
            current = self.root_node        
            parent = None 
            # 4. ???????????????????????????????????????????????????????????
            ????????????????????????????????????????????????????????????????            
                ????????????????????????????????????????????????????????????
                # 5. in loop compare the new node data to current node data and either go left side
                #    or right side of tree (and if you hit current of None, use the parent pointer to insert new node and
                     short circuit loop, other wise the loop will continue down the tree
                if node.data <= current.data:
                    current = current.left_child
                    if current is None:
                        parent.left_child = node
                        return
                else:
                    current = current.right_child
                    if current is None:
                        parent.right_child = node
                        return
Answer
go into "while True:" loop and in loop make parent the current
            # 4. go into "while True:" loop and in loop make parent the current  
            while True:            
                parent = current

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511555329292

Question
In algorithms, for BinarySearchTree class' insert method (def insert(self, data): ), you 1) create the Node object from the data 2) check to see if root is empty (if so make the new Node root), 3) if root is not empty, create current pointer to root, and parent pointer (to None initially) 4) go into "while True:" loop and in loop make parent the current, 5) in loop, [...] and if current is None, you found right place to insert (so short circuit loop)
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None


    def insert(self, data):
        # 1. create the Node object with the data
        node = Node(data)
        # 2. if root is None, make this new node the root
        if self.root_node == None:            
            self.root_node = node        
        else:
            # 3. if root exists, then create a current pointer initialized to root, and a parent pointer (None initially)
            current = self.root_node        
            parent = None 
            # 4. go into "while True:" loop and in loop make parent the current  
            while True:            
                parent = current
                # 5. ?????????????????????????????????????????????????????????????????????
                ??????????????????????????????????????????????????????????????????????????
                    ??????????????????????????????????????????????????????????????????????
                    if current is None:
                        parent.left_child = node
                        return
                ??????????????????????????????????????????????????????????????????????????
                    ??????????????????????????????????????????????????????????????????????
                    if current is None:
                        parent.right_child = node
                        return
Answer
check value of current (compared to new Node data) to see if current is to shift to right or left subtree:
            while True:            
                parent = current
                # 5. in loop compare the new node data to current node data and either go left side
                #    or right side of tree.....
                if node.data <= current.data:
                    current = current.left_child
                    # ...(and if you hit current of None, use the parent pointer to insert new node and
                    #    short circuit loop, other wise the loop will continue down the tree
                    if current is None:
                        parent.left_child = node
                        return
                else:
                    current = current.right_child
                    if current is None:
                        parent.right_child = node
                        return

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511559261452

Question
In algorithms, for BinarySearchTree class' insert method (def insert(self, data): ), you 1) create the Node object from the data 2) check to see if root is empty (if so make the new Node root), 3) if root is not empty, create current pointer to root, and parent pointer (to None initially) 4) go into "while True:" loop and in loop make parent the current, 5) in loop, check value of current (compared to new Node data) to see if current is to shift to right or left subtree and [...] (so short circuit loop):
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None


    def insert(self, data):
        # 1. create the Node object with the data
        node = Node(data)
        # 2. if root is None, make this new node the root
        if self.root_node == None:            
            self.root_node = node        
        else:
            # 3. if root exists, then create a current pointer initialized to root, and a parent pointer (None initially)
            current = self.root_node        
            parent = None 
            # 4. go into "while True:" loop and in loop make parent the current  
            while True:            
                parent = current
                # 5. in loop compare the new node data to current node data and either go left side
                #    or right side of tree (and....
                if node.data <= current.data:
                    current = current.left_child
                    ?????????????????????????????????????????????????????????????????????
                        ?????????????????????????????????????????????????????????????????
                        ?????????????????????????????????????????????????????????????????
                else:
                    current = current.right_child
                    ?????????????????????????????????????????????????????????????????????
                        ?????????????????????????????????????????????????????????????????
                        ?????????????????????????????????????????????????????????????????
Answer
if current is None, you found right place to insert, so insert using parent pointer:
                # 5. in loop compare the new node data to current node data and either go left side
                #    or right side of tree and ...
                if node.data <= current.data:
                    current = current.left_child
                    #...and if you hit current of None, use the parent pointer to insert new node and
                    # short circuit loop, other wise the loop will continue down the tree
                    if current is None:
                        parent.left_child = node
                        return
                else:
                    current = current.right_child
                    #...and if you hit current of None, use the parent pointer to insert new node and
                    # short circuit loop, other wise the loop will continue down the tree
                    if current is None:
                        parent.right_child = node
                        return

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511595175180

Question
In algorithms, for Binary Search Tree insert, one helpful thing to remember is you need to use two pointers and drill down to the right place to insert the node, the two pointers are current and [...]
Answer
parent
^^ you need the parent pointer, because by the time you drill down to current, current will be None (so addition of node has to be done via parent).

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511600155916

Question
In python, the difference between == and is, is that == compares [...] while is compares [...] (NOTE: looking for two different words here)
Answer
==, equality (i.e. two objects are pointing not to necessarly the same object but what they do point to is of equal value)
is, identity (i.e. two object have same value and are same/point to same thing

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill
The Difference Between “is” and “==” in Python – dbader.org
in meaning between equal and identical. And this difference is important when you want to understand how Python’s is and == comparison operators behave. The == operator compares by checking for <span>equality: If these cats were Python objects and we’d compare them with the == operator, we’d get “both cats are equal” as an answer. The is operator, however, compares identities: If we compared







Flashcard 3511605923084

Question
In algorithms, for BinarySearchTree delete method (def delete(self, data): ), once you get the pointer to node to delete and its
parent, name (broadly) the main cases to handle?
Answer
1) node to delete has 0 children,
2) node to delete has 1 child,
3) node to delete has 2 children

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511609068812

Question
In algorithms, for BinarySearchTree delete method (def delete(self, data): ), first thing you need to do is [...multi word description...] , then there is three cases to handle (node to delete has 0 children, node to delete has 1 child, node to delete has 2 children).
Answer

get the pointer to node to delete and its parent (using helper method: get_node_with_parent(self, data), which returns (parent, node) ):

Here is the helper function implementation (as a bonus):

   def __get_node_with_parent(self, data):
       parent = None
       current = self.root_node
       while current:
           if data == current.data:
               return (parent, current)
           parent = current
           if data < current.data:
               current = current.left_child
           else:
               current = current.right_child
       return (None, None)

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511613525260

Question
In algorithms, for BinarySearchTree, the delete method (def delete(self, data): ) has these 5 steps: 1) use helper function to get pointers to node to delete and its parent 2) get children count of node to delete 3) If node to delete has no children, [...] 4) If node to delete has one child, get pointer to that child (next_node), and if parent exists (i.e. not root case!), make parent point correctly (via left/right pointer) to that child, then cover root case (i.e. make root the next_node) 5) If node to delete has 2 children, create two new pointers, parent_of_leftmost_node (inititialized to node), and leftmost_node (initialized to node.right_child), and iterate left on the leftmost_node till you find left most, then swap the current node.data with the leftmost_node.data, and delete the leftmost_node via its parent by pointing its left child to leftmost_node right child (which could be None, if no children), making sure to cover the basecase where the parent_of_leftmost_node is the root node (which causes right_child pointer of parent to point to leftmost_node right_child)
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None

    def get_node_with_parent(self, data):        
        parent = None        
        current = self.root_node        
        if current == None:            
            return (parent, None)
        while current:            
            if data == current.data:                
                return (parent, current)           
            elif data <= current.data:                
                parent = current                
                current = current.left_child            
            else:                
                parent = current                
                current = current.right_child        
        return (parent, current)


    def remove(self, data): 
        # 1. User helper function to get pointers to node to delete and its parent       
        parent, node = self.get_node_with_parent(data)

        if node == None:            
            return False        

        # 2. Get children count of node to delete       
        children_count = 0        
        if node.left_child and node.right_child:            
            children_count = 2        
        elif (node.left_child is None) and (node.right_child is None):            
            children_count = 0        
        else:            
            children_count = 1

        # ????????????????????????????????????????????????????????????????????????????????????????   
        # ????????????????????????????????????????????????????????????????????????????????????????
        ??????????????????????????????????????????????????????????????????????????????????????????
            ??????????????????????????????????????????????????????????????????????????????????????                
                ??????????????????????????????????????????????????????????????????????????????????                   
                    ??????????????????????????????????????????????????????????????????????????????
                ??????????????????????????????????????????????????????????????????????????????????
                    ??????????????????????????????????????????????????????????????????????????????
            ??????????????????????????????????????????????????????????????????????????????????????
                ??????????????????????????????????????????????????????????????????????????????????
        # 4. If node to delete has one child, get pointer to that child (next_node), and if parent exists (i.e. not root case!), 
        #    make parent p
...
Answer
see if its right or left child of parent and point the left or right pointer of parent to None accordintly, and don't forget to cover root case where node is root so set root to None!

        if children_count == 0:
            if parent:                
                if parent.right_child is node:                    
                    parent.right_child = None
                else:
                    parent.left_child = None
            else:
                self.root_node = None

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511617457420

Question
In algorithms, for BinarySearchTree, the delete method (def delete(self, data): ) has these 5 steps: 1) use helper function to get pointers to node to delete and its parent 2) get children count of node to delete 3) If node to delete has no children, see if its right or left child of parent and act accordingly (cover root case where node is root so set root to None!) 4) If node to delete has one child, [...] 5) If node to delete has 2 children, create two new pointers, parent_of_leftmost_node (inititialized to node), and leftmost_node (initialized to node.right_child), and iterate left on the leftmost_node till you find left most, then swap the current node.data with the leftmost_node.data, and delete the leftmost_node via its parent by pointing its left child to leftmost_node right child (which could be None, if no children), making sure to cover the basecase where the parent_of_leftmost_node is the root node (which causes right_child pointer of parent to point to leftmost_node right_child):
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None

    def get_node_with_parent(self, data):        
        parent = None        
        current = self.root_node        
        if current == None:            
            return (parent, None)
        while current:            
            if data == current.data:                
                return (parent, current)           
            elif data <= current.data:                
                parent = current                
                current = current.left_child            
            else:                
                parent = current                
                current = current.right_child        
        return (parent, current)


    def remove(self, data): 
        # 1. User helper function to get pointers to node to delete and its parent       
        parent, node = self.get_node_with_parent(data)

        if node == None:            
            return False        

        # 2. Get children count of node to delete       
        children_count = 0        
        if node.left_child and node.right_child:            
            children_count = 2        
        elif (node.left_child is None) and (node.right_child is None):            
            children_count = 0        
        else:            
            children_count = 1

        # 3. If node to delete has no children, see if its right or left child of parent and act accordingly (cover case of root/   
        #   no parent!)
        if children_count == 0:
            if parent:                
                if parent.right_child is node:                    
                    parent.right_child = None
                else:
                    parent.left_child = None
            else:
                self.root_node = None
        ???????????????????????????????????????????????????????????????????????????????????????
        ???????????????????????????????????????????????????????????????????????????????????????
            ???????????????????????????????????????????????????????????????????????????????????
            ???????????????????????????????????????????????????????????????????????????????????
                ???????????????????????????????????????????????????????????????????????????????
            ???????????????????????????????????????????????????????????????????????????????????
                ???????????????????????????????????????????????????????????????????????????????
            ???????????????????????????????????????????????????????????????????????????????????
                ???????????????????
...
Answer
get pointer to that child (next_node), and if parent exists (i.e. not root case!), make parent point correctly (via left/right pointer) to that child, then cover root case (i.e. make root the next_node):
        elif children_count == 1:
            next_node = None
            if node.left_child:
                next_node = node.left_child
            else:
                next_node = node.right_child
            if parent:
                if parent.left_child is node:
                    parent.left_child = next_node
                else:
                    parent.right_child = next_node
            else:
                self.root_node = next_node

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511621389580

Question
In algorithms, for BinarySearchTree, the delete method (def delete(self, data): ) has these 5 steps: 1) use helper function to get pointers to node to delete and its parent 2) get children count of node to delete 3) If node to delete has no children, see if its right or left child of parent and act accordingly (cover root case where node is root so set root to None!) 4) If node to delete has one child, get pointer to that child (next_node), and if parent exists (i.e. not root case!), make parent point correctly (via left/right pointer) to that child, then cover root case (i.e. make root the next_node) 5) If node to delete has 2 children, create [...] , and iterate left on the leftmost_node till you find left most, then swap the current node.data with the leftmost_node.data, and delete the leftmost_node via its parent by pointing its left child to leftmost_node right child (which could be None, if no child), making sure to cover the basecase where the parent_of_leftmost_node is the root node (which causes right_child pointer of parent to point to leftmost_node right_child):
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None

    def get_node_with_parent(self, data):        
        parent = None        
        current = self.root_node        
        if current == None:            
            return (parent, None)
        while current:            
            if data == current.data:                
                return (parent, current)           
            elif data <= current.data:                
                parent = current                
                current = current.left_child            
            else:                
                parent = current                
                current = current.right_child        
        return (parent, current)


    def remove(self, data): 
        # 1. User helper function to get pointers to node to delete and its parent       
        parent, node = self.get_node_with_parent(data)

        if node == None:            
            return False        

        # 2. Get children count of node to delete       
        children_count = 0        
        if node.left_child and node.right_child:            
            children_count = 2        
        elif (node.left_child is None) and (node.right_child is None):            
            children_count = 0        
        else:            
            children_count = 1

        # 3. If node to delete has no children, see if its right or left child of parent and act accordingly (cover case of root/   

        #   no parent!)
        if children_count == 0:
            if parent:                
                if parent.right_child is node:                    
                    parent.right_child = None
                else:
                    parent.left_child = None
            else:
                self.root_node = None
        # 4. If node to delete has one child, get pointer to that child (next_node), and if parent exists (i.e. not root case!), 
        #    make parent point correctly (via left/right pointer) to that child, then cover root case (i.e. make root the           

        #    next_node)
        elif children_count == 1:
            next_node = None
            if node.left_child:
                next_node = node.left_child
            else:
                next_node = node.right_child
            if parent:
                if parent.left_child is node:
                    parent.left_child = next_node
                else:
                    parent.right_child = next_node
            else:
           
...
Answer
two new pointers, parent_of_leftmost_node (inititialized to node), and leftmost_node (initialized to node.right_child):
        else:
            parent_of_leftmost_node = node
            leftmost_node = node.right_child
            while leftmost_node.left_child:
                parent_of_leftmost_node = leftmost_node
                leftmost_node = leftmost_node.left_child
            node.data = leftmost_node.data

            if parent_of_leftmost_node.left_child == leftmost_node:
                parent_of_leftmost_node.left_child = leftmost_node.right_child
            else:  ############ parent_of_leftmost_node is root!!                                  
                parent_of_leftmost_node.right_child = leftmost_node.right_child   

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs







Flashcard 3511625321740

Question
In algorithms, for BinarySearchTree, the delete method (def delete(self, data): ) has these 5 steps: 1) use helper function to get pointers to node to delete and its parent 2) get children count of node to delete 3) If node to delete has no children, see if its right or left child of parent and act accordingly (cover root case where node is root so set root to None!) 4) If node to delete has one child, get pointer to that child (next_node), and if parent exists (i.e. not root case!), make parent point correctly (via left/right pointer) to that child, then cover root case (i.e. make root the next_node) 5) If node to delete has 2 children, create two new pointers, parent_of_leftmost_node (inititialized to node), and leftmost_node (initialized to node.right_child), [...] , and delete the leftmost_node via its parent by pointing its left child to leftmost_node right child (which could be None, if no right child), making sure to cover the basecase where the parent_of_leftmost_node is the root node (which causes right_child pointer of parent to point to leftmost_node right_child)
class Node:        
    def __init__(self, data):            
        self.data = data
        self.left_child = None
        self.right_child = None


class Tree:        
    def __init__(self):            
        self.root_node = None

    def get_node_with_parent(self, data):        
        parent = None        
        current = self.root_node        
        if current == None:            
            return (parent, None)
        while current:            
            if data == current.data:                
                return (parent, current)           
            elif data <= current.data:                
                parent = current                
                current = current.left_child            
            else:                
                parent = current                
                current = current.right_child        
        return (parent, current)


    def remove(self, data): 
        # 1. User helper function to get pointers to node to delete and its parent       
        parent, node = self.get_node_with_parent(data)

        if node == None:            
            return False        

        # 2. Get children count of node to delete       
        children_count = 0        
        if node.left_child and node.right_child:            
            children_count = 2        
        elif (node.left_child is None) and (node.right_child is None):            
            children_count = 0        
        else:            
            children_count = 1

        # 3. If node to delete has no children, see if its right or left child of parent and act accordingly (cover case of root/   

        #   no parent!)
        if children_count == 0:
            if parent:                
                if parent.right_child is node:                    
                    parent.right_child = None
                else:
                    parent.left_child = None
            else:
                self.root_node = None
        # 4. If node to delete has one child, get pointer to that child (next_node), and if parent exists (i.e. not root case!), 
        #    make parent point correctly (via left/right pointer) to that child, then cover root case (i.e. make root the           

        #    next_node)
        elif children_count == 1:
            next_node = None
            if node.left_child:
                next_node = node.left_child
            else:
                next_node = node.right_child
            if parent:
                if parent.left_child is node:
                    parent.left_child = next_node
                else:
                    parent.right_child = next_node
            else:
           
...
Answer
and iterate left on the leftmost_node till you find left most, then swap the current node.data with the leftmost_node.data:
        else:
            parent_of_leftmost_node = node
            leftmost_node = node.right_child
            while leftmost_node.left_child:                ##<<<<<<<<<<<<<<<<<<<<<<<<
                parent_of_leftmost_node = leftmost_node    ##<<<<<<<<<<<<<<<<<<<<<<<<
                leftmost_node = leftmost_node.left_child   ##<<<<<<<<<<<<<<<<<<<<<<<<
            node.data = leftmost_node.data   ##<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

            if parent_of_leftmost_node.left_child == leftmost_node:
                parent_of_leftmost_node.left_child = leftmost_node.right_child
            else:  ############ parent_of_leftmost_node is root!!                                  
                parent_of_leftmost_node.right_child = leftmost_node.right_child  

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

pdf

cannot see any pdfs