找到 34423 篇文章,关于编程

Python程序:仅打印左子树中的节点

AmitDiwan
更新于 2021年4月16日 12:41:26

214 次浏览

如果需要打印左子树中的节点,可以创建一个包含方法的类,这些方法可以定义为设置根节点、执行顺序遍历、将元素插入到根节点的右侧、左侧等等。创建一个类的实例,然后可以使用这些方法执行所需的操作。下面是演示:示例 在线演示class BinaryTree_struct:    def __init__(self, data=None):       self.key = data       self.left = None       self.right = ... 阅读更多

Python程序:按字典序打印字符串的所有排列(无递归)

AmitDiwan
更新于 2021年4月16日 12:38:58

384 次浏览

如果需要在不使用递归的情况下按字典序打印字符串的所有排列,则定义一个方法,该方法将字符串作为参数。它使用简单的“for”循环迭代字符串元素,并使用“while”条件检查某些约束。以下是演示:示例 在线演示from math import factorial def lex_permutation(my_string): for i in range(factorial(len(my_string))):    print(''.join(my_string))    i = len(my_string) - 1    while i > 0 and my_string[i-1] > my_string[i]:       i -= 1    my_string[i:] = reversed(my_string[i:])    if i > 0: ... 阅读更多

Python程序:从给定字符串中创建由前两个和后两个字符组成的新的字符串

AmitDiwan
更新于 2021年4月16日 12:38:18

634 次浏览

如果需要创建一个新的字符串,该字符串由给定字符串的前两个字符和后两个字符组成,则可以定义一个计数器,并使用索引访问特定范围的元素。以下是演示:示例 在线演示my_string = "Hi there how are you" my_counter = 0 for i in my_string:    my_counter = my_counter + 1 new_string = my_string[0:2] + my_string [my_counter - 2: my_counter ] print("The string is ") print(my_string) print("The new string is ") print(new_string)输出The string is Hi there how are you The new ... 阅读更多

Python程序:创建树的镜像副本并使用BFS遍历显示

AmitDiwan
更新于 2021年4月16日 12:34:24

553 次浏览

如果需要创建树的镜像副本并使用广度优先搜索 (BFS) 显示它,则创建一个二叉树类,其中包含设置根元素、将元素插入左侧、将元素插入右侧、搜索特定元素以及执行后序遍历等方法。创建一个类的实例,可以使用它来访问这些方法。以下是演示:示例 在线演示class BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None ... 阅读更多

Python程序:使用后序遍历实现深度优先搜索遍历

AmitDiwan
更新于 2021年4月16日 12:31:03

399 次浏览

如果需要使用后序遍历实现深度优先搜索,则创建一个树类,其中包含添加元素、搜索特定元素以及执行后序遍历等方法。创建一个类的实例,可以使用它来访问这些方法。以下是演示:示例 在线演示class Tree_Struct:    def __init__(self, key=None):       self.key = key       self.children = []    def add_elem(self, node):       self.children.append(node)      def search_elem(self, key):       if self.key == key:     ... 阅读更多

Python程序:使用中序遍历查找树中的最大值

AmitDiwan
更新于 2021年4月16日 12:28:03

141 次浏览

如果需要使用中序遍历查找树中的最大值,则创建一个二叉树类,其中包含设置根元素、使用递归执行中序遍历等方法。创建一个类的实例,可以使用它来访问这些方法。以下是演示:示例 在线演示class BinaryTree_Struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key    def inorder_traversal_largest(self):   ... 阅读更多

Python程序:计算字符串中单词和字符的个数

AmitDiwan
更新于 2021年4月16日 12:24:38

1K+ 次浏览

如果需要计算字符串中单词和字符的个数,以下是演示:示例 在线演示my_string = "Hi there, how are you Will ? " print("The string is :") print(my_string) my_chars=0 my_words=1 for i in my_string:    my_chars=my_chars+1    if(i==' '):       my_words=my_words+1 print("The number of words in the string are :") print(my_words) print("The number of characters in the string are :") print(my_chars)输出The string is : Hi there, how are you Will ? The number of words in the string are : 8 The number of characters in the string ... 阅读更多

Python程序:删除字符串中奇数索引值的字符

AmitDiwan
更新于 2021年4月16日 12:22:23

572 次浏览

如果需要删除字符串中奇数索引的字符,则定义一个方法,该方法将字符串作为参数。以下是演示:示例 在线演示def remove_odd_index_characters(my_str):    new_string = ""    i = 0    while i < len(my_str):       if (i % 2 == 1):          i+= 1          continue       new_string += my_str[i]       i+= 1    return new_string if __name__ == '__main__':    my_string = "Hi there Will"    my_string = remove_odd_index_characters(my_string)    print("Characters from odd ... 阅读更多

Python程序:使用递归进行深度优先二叉树搜索

AmitDiwan
更新于 2021年4月16日 12:21:22

344 次浏览

如果需要使用递归对树执行深度优先搜索,则定义一个类,并在其中定义有助于执行广度优先搜索的方法。以下是演示:示例 在线演示class BinaryTree_struct:    def __init__(self, key=None):       self.key = key       self.left = None       self.right = None    def set_root(self, key):       self.key = key    def insert_at_left(self, new_node):       self.left = new_node    def insert_at_right(self, new_node):       self.right = new_node    def search_elem(self, key):   ... 阅读更多

Python程序:使用二叉搜索树排序

AmitDiwan
更新于 2021年4月16日 12:19:41

693 次浏览

如果需要对二叉搜索树进行排序,则创建一个类,并在其中定义执行插入元素和执行中序遍历等操作的方法。以下是演示:示例class BinSearchTreeNode:    def __init__(self, key):       self.key = key       self.left = None       self.right = None       self.parent = None    def insert_elem(self, node):       if self.key > node.key:          if self.left is None:             self.left = node             node.parent = self          else:             self.left.insert_elem(node)       elif self.key

广告
© . All rights reserved.