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
  • 相关阅读:
    JavaScript事件处理
    JavaScript模拟"类"的三种方法
    非构造函数的继承和拷贝
    构造函数的继承
    vim开发环境
    socket之非阻塞
    网络编程
    多线程
    消息队列
    信号
  • 原文地址:https://www.cnblogs.com/waynewei/p/14923479.html
Copyright © 2011-2022 走看看