Sequential storage binary tree - threaded binary tree

Sequential storage binary tree - threaded binary tree

Issues into

Not every binary tree can completely pointer can be used on both hands leaf nodes are not referenced, it is empty. Want to take advantage of all of its pointers in a binary tree structure by sequential storage remaining pointer which points to a certain cutting predecessor node traversal order of the successor node and
illustrated:Here Insert Picture Description

  • In all orange lead figure above is the object does not point to any null pointer
  • Null reference number n + 1, n is the number of nodes common
  • The total number of references 2 * n,
  • Number of non-null reference to n - 1, has one between each two.

Asked Questions

Here Insert Picture Description

  • Obtained according to the output sequence preorder fashion, and with an empty reference, inorder traversal order will be continuous to each other together with each other. This is the clue of the order of stored binary tree
  • References in the following case each node:
    • left pointing to the left child node, it may point to its precursor nodes in the corresponding sequence
    • right point to the right child node, it may point to its successor node in the corresponding order.

basic introduction

  • Pointer precursors : a fixed traversal order, prior to a node of a node, called the pre pointer
  • Successor pointers : a fixed traversal order, after one node of a node, called a successor pointers.
  • Clues : it contains the n binary nodes in the linked list of n + 1 null pointer field using binary null pointer field in the list, pointing to the storage nodeUnder certain traversal orderThe predecessor and successor pointers node called leads.
  • Trail list : adding a list called a binary clue clue list, called a binary tree corresponding threaded binary. Depending on the sequence of cues, cue into binary: the preamble of Threaded binary tree, the binary sequence leads, the threaded binary sequence.

Code

public HeroArrangement pre = null;
public void threadedNotes(HeroArragement node){
        //为什么要传入对应的节点,线索化不应该是所有的结点都进行线索化吗
        if (node == null){
            return;
        }
        //先线索化左子树
        threadedNotes(node.getLeft());
        //线索化当前节点
        //先处理当前节点的前驱节点
        if(node.getLeft() == null){
            //让当前节点的左指针指向前驱节点
            node.setLeft(pre);
            //修改左指针的数据类型
            node.setLeftType(1);
        }
        //处理后继节点,在递归的时候由下一次处理
        if(pre != null && pre.getRight() == null){
            //让前驱指针的右指针位当前指针,因为递归的单向性,
            // 所以必须单向来改变他的值,不可以及改变左边的指针,又改变右边的指针
            pre.setRight(node);
            pre.setRightType(1);
        }

        //每处理一个节点,让当前节点是下一个节点的前驱节点
        //迭代的条件
        pre = node;

        //再线索化右子树
        threadedNotes(node.getRight());
    }
class HeroArragement{
    private int heroNo;
    private String heroName;
    HeroArragement left;
    HeroArragement right;
    //因为指针有不同的情况,所以设置变量
    //说明:
    //LeftType == 0,表示指向左子树,如果是1,标志指向前驱节点
    //RightType == 0,表示指向右子树,如果是1,表示指向后继节点
    private int leftType;
    private int RightType;
    ...
    }

Analysis and summary

  1. It is in accordance with the order recursive traversal sequence, whether that end node to the leaf, the leaf nodes before and after the null pointer then connected together.
  2. Unidirectional traversal, a node can only use the left pointer of the current pointer link. Therefore provided a secondary node, pre, which always precedes the current node. By the right pointer of the current node pre links.
Published 38 original articles · won praise 15 · views 5573

Guess you like

Origin blog.csdn.net/Blackoutdragon/article/details/104052636