Data structure_cue binary tree_python implementation

Clue binary tree

The clue binary tree is based on the ordinary binary tree. The null pointer field on the root node is changed. The left pointer points to the precursor node of the current node, and the right pointer points to the successor node of the current node. This kind of binary list with clues is called a thread list, and the corresponding binary tree is called a threaded binary tree (Threaded BinaryTree). According to the different nature of clues, the clue binary tree can be divided into three types: pre-order clue binary tree, mid-order clue binary tree and post-order clue binary tree.

Clue binary tree application case
Insert picture description here
description: After clue binary tree, the attributes of Node node left and right are as follows:
(1) left refers to the left subtree, and may also be the precursor node that points to. For example, ① node left points to the left The subtree, and the left of the ⑩ node points to the predecessor node.
(2) The right points to the right subtree, and may also point to the successor nodes, for example, ① node right points to the right subtree, and ⑩ node right points to Is the successor node.

This time, the code mainly uses python to complete the transformation of the ordinary binary tree into a mid-order clue binary tree. Construct a binary tree based on the photo above. After the code is completed, the correctness of the code is tested by calling the left and right of the leaf node.

Let's analyze node 10 first, according to our definition of the clue binary tree. We need to get the mid-order traversal sequence of the binary tree first: {8, 3, 10, 1, 14, 6}. So the predecessor node of node 10 is 3, and the successor node is 1. So if the clue is correct, we call the left and right pointers of node 10, and the two nodes we get are 3 and 1.

#线索化二叉树
#二叉树

#先创建结点
class HeroNode(object):
    def __init__(self,no,name):
        self.no = no
        self.name= name
        self.left=None
        self.right=None
        '''
        1、如果leftType==0,表示指向左子树;
            leftType==1,表示指向前驱节点
        
        2、如果rightType==0,表示指向右子树;
            rightType==1,表示指向后继节点
        '''
        self.leftType=None
        self.rightType=None
    
    def setleft(self,node):
        self.left = node
    def setright(self,node):
        self.right = node
        
   
#中序遍历线索化
class ThreadBinaryTree(object):
    def __init__(self):
        self.root=None
        #为了实现线索化,需要增加这个属性,保留前一个节点
        self.pre=None
    def setRoot(self,root):
        self.root = root
    
    #编写二叉树进行线索的方法
    def threadedNode(self,node):
        if node == None:
            return
        
        self.threadedNode(node.left)
        
        #处理当前节点的的前驱节点
        if node.left==None:
            node.left=self.pre
            node.leftType=1
            
        if self.pre!=None and self.pre.right == None:
            #前驱节点的右指针指向当前节点
            self.pre.right=node
            self.pre.rightType=1
            
        self.pre = node
        
        self.threadedNode(node.right)
    
    def threadedList(self):
        node = self.root
        while(node!=None):
            
            while(node.leftType==0):
                node=node.left
                
            print("no = {}; name = {}".format(node.no,node.name))
            
            while(node.rightType==1):
                node=node.right
                print("no = {}; name = {}".format(node.no,node.name))
            
            node = node.right

            
node1 = HeroNode(1,'关胜')
node2 = HeroNode(3,'刘备')
node3 = HeroNode(6,'张飞')
node4 = HeroNode(8,'曹操')
node5 = HeroNode(10,'李逵')
node6 = HeroNode(14,'大力')

print('测试中序线索二叉树的功能')
tree = ThreadBinaryTree()
node1.setleft(node2)
node1.setright(node3)
node2.setleft(node4)
node2.setright(node5)
node3.setleft(node6)

tree.setRoot(node1)

tree.threadedNode(node1)

leftnode = node5.left
rightnode= node5.right
print('id={};name={}'.format(leftnode.no,leftnode.name))
print('id={};name={}'.format(rightnode.no,rightnode.name))


print('list:')
tree.threadedList()




The result of running the code is:
Insert picture description here

Published 27 original articles · praised 2 · visits 680

Guess you like

Origin blog.csdn.net/qq_44273739/article/details/105498527