zoukankan      html  css  js  c++  java
  • Algorithm

    Algorithm implementation Binary search:

    # Binary tree
    
    class Node:
        def __init__(self, data):
            self.data = data
            self.left = None
            self.right = None
    
        def insert(self, data):
            if data < self.data:
                if self.left is None:
                    self.left = Node(data)
                else:
                    self.left.insert(data)
    
            if data > self.data:
                if self.right is None:
                    self.right = Node(data)
                else:
                    self.right.insert(data)
    
        def find(self, data):
            founded = '%s is founded in binary tree' % data
            if data < self.data:
                if self.left is None:
                    founded = '%s is not found' % data
                else:
                    founded = self.left.find(data)
            elif data > self.data:
                if self.right is None:
                    founded = '%s is not found' % data
                else:
                    founded = self.right.find(data)
    
            return founded
    
    root = Node(7)
    root.insert(6)
    root.insert(9)
    print(root.find(2))
    print(root.find(6))

    Testing output:

    D:	estvenvScriptspython.exe D:/test/algorithm.py
    2 is not found
    6 founded in binary tree
    
    Process finished with exit code 0
  • 相关阅读:
    服务管理器
    自动启动管理器
    进程管理器
    进程模块查看器
    无DLL远程注入
    远程DLL注入
    U盘免疫
    WSAAsyncSelect Demo
    select Demo
    校正系统时间
  • 原文地址:https://www.cnblogs.com/waynewei/p/14923479.html
Copyright © 2011-2022 走看看