Data Structure --- 03. Find, binary tree

A. Find

  1. sequential search (unordered list)

Sequential search principle analysis:
  Starting with the first element in the list, we simply move in accordance with the basic order ordering from one element to another, 
  until you find the elements we are looking for or traverse the complete lists. If we traverse a complete list, the elements are searching for does not exist.
def search(alist,item):
    find = False
    length = len(alist)
    
    for i in range(length):
        if alist[i] == item:
            find = True
            
    return find
alist = [3,8,5,7,6,4]
print(search(alist,51))


#False

 

  2. Find the order (ordered list)

def search(alist,item):
    length = len(alist)
    find = False
    pos = 0
    stop = False
    while pos <= length and not stop:
        if alist[pos] == item:
            find = True
            break
        elif alist[pos] > item:
            stop = True
        else:
            pos += 1
    return find
alist = [1,3,5,7,9,11]
print(search(alist,5))

#True

 

  3. binary search (important)

  For us to achieve an ordered list of search is very useful. In order to find, when we compare with the first element, 
if the first element is not what we're looking for, then there are at most N- 1 elements need to be compared. Binary search is
to start from the middle element, rather than find a list in order. If the element is the element we're looking for, we will complete
the look. If it is not, we can eliminate half the remaining elements using an ordered list of properties. If we are
the elements look larger than the middle element, you can eliminate the intermediate element and the middle element is smaller than half elements. If the element in the
list, certainly a large part of that half. We can then repeat the process with half of the large, continue to start from the middle element,
it is compared with what we are looking for content

 

def search(alist,item):
    last = len(alist)-1
    first = 0
    find = False
    
    while first<=last and not find:
        mid = (last+first) // 2
        if alist[mid] == item:
            find = True
        else:
            if alist[mid] > item:
                last = mid - 1
            else:
                first = mid + 1
    return find
alist = [1,3,5,7,9]
print(search(alist,31))

# False

 

II. Binary Tree

Binary Tree
   - with nodes
   - left leaf node
   - Right leaf nodes
   - subtree
Traversing Binary Tree
  Breadth traversal: hierarchy traversal
  Depth traversal
    Preamble: about Root
    In order: left and right root
    After the sequence: about Root
Sorting binary tree

 

 

 

  1. Create a binary tree traversal and breadth

class Node():
    def __init__(self,item):
        self.item = item
        self.left = None
        self.right = None
class Tree():
    # Constructor can construct an empty tree
    def __init__(self):
        self.root = None
    def add(self,item):
        node = Node(item)
        # Decision tree is empty
        if self.root == None:
            self.root = node
            return
        Tree # nonempty insertion operation, is operable to create a list of
        queue = [self.root]
        
        while queue:
            cur = queue.pop(0)
            if cur.left == None:
                cur.left = node
                return
            else:
                queue.append(cur.left)
            if cur.right == None:
                cur.right = node
                return
            else:
                queue.append(cur.right)
    # Breadth traversal
    def travel(self):
        queue = [self.root]
        while queue:
            cur = queue.pop(0)
            print(cur.item)
            if cur.left is not None:
                queue.append(cur.left)
            if cur.right is not None:
                queue.append(cur.right)
tree = Tree()
tree.add(1)
tree.add(2)
tree.add(3)
tree.add(4)
tree.add(5)
tree.add(6)
tree.add(7)
tree.add(8)
tree.add(9)
tree.add(10)
tree.travel()

# 1 2 3 4 5 6 7 8 9 10

 

  2. depth traversal

class Node():
    def __init__(self,item):
        self.item = item
        self.left = None
        self.right = None
class Tree():
    # Constructor can construct an empty tree
    def __init__(self):
        self.root = None
    def add(self,item):
        node = Node(item)
        # Decision tree is empty
        if self.root == None:
            self.root = node
            return
        Tree # nonempty inserting operation
        queue = [self.root]
        
        while queue:
            cur = queue.pop(0)
            if cur.left == None:
                cur.left = node
                return
            else:
                queue.append(cur.left)
            if cur.right == None:
                cur.right = node
                return
            else:
                queue.append(cur.right)
    # Depth traversal
    def forward (self, root): root around #
        if root == None:
            return
        print(root.item,end=' ')
        self.forward(root.left)
        self.forward(root.right)
        
    def mid (self, root): # root left and right
        if root == None:
            return
        self.mid(root.left)
        print(root.item,end=' ')
        self.mid(root.right)
        
    def back (self, root): # root around
        if root == None:
            return
        self.back(root.left)
        self.back(root.right)
        print(root.item,end=' ')
tree = Tree()
tree.add(1)
tree.add(2)
tree.add(3)
tree.add(4)
tree.add(5)
tree.add(6)
tree.add(7)
tree.add(8)
tree.add(9)
tree.add(10)

tree.forward(tree.root)
print('\n')
tree.mid(tree.root)
print('\n')
tree.back(tree.root)
print('\n')

 

Results: 

. 1
2 . 4 . 8 . 9 . 5 10 . 3 . 6 . 7 8 4 9 2 10 5 1 6 3 7 8 9 4 10 5 2 6 7 3 1

 

  3. Sort binary tree

class Node():
    def __init__(self,item):
        self.item = item
        self.left = None
        self.right = None
class Tree():
    # Constructor can construct an empty tree
    def __init__(self):
        self.root = None
    # nodes need to be inserted, according to a criterion criterion: the root node is inserted into the left smaller than the data number of the root node is greater than the insertion to the right
    def insert(self,item):
        node = Node(item)
        if self.root == None:
            self.root = node
            return
        cur = self.root
        #right
        while True:
            if item > cur.item:
                if cur.right == None:
                    cur.right = node
                    return
                else:
                    cur = cur.right

            else:
                if cur.left == None:
                    cur.left = node
                    return
                else:
                    cur = cur.left
        
     # Depth traversal
    def forward (self, root): root around #
        if root == None:
            return
        print(root.item,end=' ')
        self.forward(root.left)
        self.forward(root.right)
        
    def mid (self, root): # root left and right
        if root == None:
            return
        self.mid(root.left)
        print(root.item,end=' ')
        self.mid(root.right)
        
    def back (self, root): # root around
        if root == None:
            return
        self.back(root.left)
        self.back(root.right)
        print(root.item,end=' ')
tree = Tree()
tree.insert(3)
tree.insert(0)
tree.insert(2)
tree.insert(9)
tree.insert(1)
tree.insert(6)
tree.insert(4)
tree.forward(tree.root)
print('\n')
tree.mid(tree.root)
print('\n')
tree.back(tree.root)
print('\n')

 

Guess you like

Origin www.cnblogs.com/sc-1067178406/p/11028545.html