Python programming to sort keyboard input elements binary tree sorting

Posted by Bogart on Sun, 26 Dec 2021 08:35:55 +0100

Today, let's take a different sort method - binary tree sort. This sorting method is more interesting and easy to operate.

This paper refers to Python data analysis from introduction to mastery, Zhang Xiaoyu and Li Jing, electronic industry press, 5.3 2 sorting

Binary tree sorting

The process of binary tree sorting is mainly the construction and traversal of binary tree. The binary tree traversal method used in this article is the middle order traversal of binary tree.

Medium order traversal: if the binary tree is not empty, access the left subtree first. Then access the root node, and finally access the right subtree; Otherwise, the program exits.

The main codes are as follows:

def inorder(node):                                  #Medium order traversal
    if node.data:
        if node.left:
            inorder(node.left)
        node.show()
        if node.right:
            inorder(node.right)

Building binary tree (key part)

Binary tree sorting is mainly from scratch and then to sorting. Therefore, building a binary tree has become a key step.

From scratch:

 For example, from a set of data 3, 5, 7, 20, 43, 2, 15, 30, the construction process of binary tree is as follows:

1) First, put the first data 3 into the root node:

2) Compare data 5 with data 3 in the root node. Since 5 is greater than 3, put 5 into the right subtree of 3.

3) Compare data 7 with data 3 in the root node. Since 7 is greater than 3, put 7 into the right subtree of 3; Since 3 already has a right subtree 5, compare 7 with 5. If 7 is greater than 5, 7 should be placed in the right subtree of 5.

4) Compare the data 20 with the root node data 3. Since 20 is greater than 3, put 20 into the right subtree of 3; Repeat the comparison, and finally put 20 into the right subtree of 7.

5) Compare the data 43 with the node values in the tree and finally put it into the right subtree of 20.
6) Compare data 2 with root node data 3. If 2 is less than 3, put it into the left subtree.
7) Similarly, the data 15 and 30 are processed to finally form a binary tree as shown in the figure below.

The main codes are as follows:

def insert(node,value):                             #Constructing binary tree
    if value > node.data:
        if node.right:
            insert(node.right,value)
        else:
            node.insertRight(value)
    else:
        if node.left:
            insert(node.left, value)
        else:
            node.insertLeft(value)

Sort:

When the tree is built, the tree is traversed in middle order, and the traversal result is the result of sorting the data from small to large. If you want to sort from large to small; You can start the middle order traversal from the right subtree.

The binary tree sorting method is adopted to sort the data, and the code is as follows:
#
class BTree:                                        #Binary tree node
    def __init__(self,value):                       #Initialization function
        self.left = None                            #Left son
        self.data = value                           #Node value
        self.right = None                           #Right son
    def insertLeft(self,value):                     #Left subtree add
        self.left = BTree(value)
        return self.left
    def insertRight(self,value):                    #Right subtree add
        self.right = BTree(value)
        return self.right
    def show(self):                                 #Output node data
        print(self.data)
    
def inorder(node):                                  #Medium order traversal, from small to large
    if node.data:
        if node.left:
            inorder(node.left)
        node.show()
        if node.right:
            inorder(node.right)
def rinorder(node):                                 #From large to small, medium order traversal
    if node.data:
        if node.right:
            rinorder(node.right)
        node.show()
        if node.left:
            rinorder(node.left)
            
def insert(node,value):                             #Constructing binary tree
    if value > node.data:
        if node.right:
            insert(node.right,value)
        else:
            node.insertRight(value)
    else:
        if node.left:
            insert(node.left, value)
        else:
            node.insertLeft(value)

if __name__ == '__main__':
    l1 = input("Input list elements,Use“,"(english)separate:").split(',')##keyboard entry
    l = [int(l1[i]) for i in range(len(l1))] #Generate initial list
    Root = BTree(l[0])
    node = Root
    for i in range(1,len(l)):
        insert(Root, l[i])
        
    print('*'*30)
    print('**from small to large**')
    print('*'*30)
    inorder(Root)
    print('*'*30)
    print('**From big to small**')
    print('*'*30)
    rinorder(Root)
The final results are as follows:
Input list elements,Use“,"(english)separate:3,5,7,20,43,2,15,30
******************************
**from small to large**
******************************
2
3
5
7
15
20
30
43
******************************
**From big to small**
******************************
43
30
20
15
7
5
3
2

epilogue
There is no unique programming world. You are welcome to supplement and optimize the code.

Topics: Python Algorithm data structure