Data Structure-clue binary tree and clue Binary Tree

Source: Internet
Author: User

Data Structure-clue binary tree and clue Binary Tree
1. What is a clue binary tree?

There must be n + 1 null pointer fields in the binary linked list with n nodes, therefore, we can use these NULL pointer fields to store the pointers of the forward and next nodes in a certain traversal order pointing to the node. The pointers of the forward and next nodes are called "clues ", the binary linked list with clues is called a chain list, and the corresponding binary tree is called a chain tree.

2. Why do we need to establish a clue binary tree?

Isn't Binary Tree enough? So why do we need to create a clue binary tree?

In the original binary linked list, you can find the Left and Right nodes of the node, but what if you want to find the front and next nodes of the node? This becomes very difficult. To meet this common requirement, we need to add two pointer domains in each node to store the previous and subsequent nodes, in this way, the cursor can be used to directly or indirectly access the frontend and successor nodes.

3. How to break Binary Tree clues

Traverse Binary Trees in a certain order and replace null pointers with clues during traversal.

The following is the clue Binary Tree and the clue binary chain table, which can help us better understand the clue binary tree.

 

The Code is as follows:

/** Clue Binary Tree **/# include <stdio. h> # include <stdlib. h> typedef char ElemType; // data type typedef enum {Link, Thread} childTag; // Link indicates the node, Thread indicates the clue typedef struct threadBiTree {ElemType data; struct threadBiTree * lchild, * rchild; int ltag, rtag; // flag, which distinguishes nodes or clues in Enumeration type.} threadBiTree, * ptrThreadBiTree; /*** create a common binary tree */void create_BiTree (ptrThreadBiTree & TNode, char * & str) {char ch; sscanf (str, "% c ", & ch); str ++; if (c H = '#') {// empty node TNode = NULL;} else {TNode = (ptrThreadBiTree) malloc (sizeof (threadBiTree); TNode-> data = ch; TNode-> ltag = Link; TNode-> rtag = Link; create_BiTree (TNode-> lchild, str); create_BiTree (TNode-> rchild, str );}} /*** Access Node information */void visit (ptrThreadBiTree & TNode) {printf ("% d-% c-% d \ n", TNode-> ltag, TNode-> data, TNode-> rtag);}/*** preorder traversal of the Access Binary Tree */void pre_order_traverse (ptrThreadBiTree & TNode, in T level) {if (TNode) {visit (TNode); pre_order_traverse (TNode-> lchild, level + 1); pre_order_traverse (TNode-> rchild, level + 1 );}} /*** subordered binary tree */void in_order_threading (ptrThreadBiTree & preNode, ptrThreadBiTree & root) {if (root) {in_order_threading (preNode, root-> lchild); if (! Root-> lchild) {// left child is empty, so that the clues direct to the frontend root-> lchild = preNode; root-> ltag = Thread;} if (! PreNode-> rchild) {// if the right child of the last node is empty, direct the child to the next node (Note: only when the next node is accessed, to know who the node is next to.) preNode-> rchild = root; preNode-> rtag = Thread;} preNode = root; in_order_threading (preNode, root-> rchild) ;}}/*** Add a header node to make the binary tree into a closed ring * binary tree with a header node, and the left child of the header node points to the binary tree T; the right child points to the last leaf node in the T tree * a binary tree without a header node */void in_thread (ptrThreadBiTree & head, ptrThreadBiTree & root) {head = (ptrThreadBiTree) malloc (sizeof (threadBiTree); // Add a header node head-> lt Ag = Link; head-> rtag = Thread; head-> rchild = head; // set the right clue to itself if (! Root) {// The binary tree is empty, so that the left child Pointer Points to its own head-> lchild = head;} else {head-> lchild = root; ptrThreadBiTree preNode = head; // record the last accessed node in_order_threading (preNode, root); head-> rchild = preNode; // point the right child of the header node to the last leaf node preNode-> rtag = Thread; preNode-> rchild = head; // point the right child of the last leaf node to the header node, the loop forms the}/*** middle-order clue binary tree */void mid_order_traverse (ptrThreadBiTree & head) {ptrThreadBiTree root = head-> lchild; while (root! = Head) {// determine whether to empty the tree while (root-> ltag = Link) {root = root-> lchild;} visit (root ); while (root-> rtag = Thread & root-> rchild! = Head) {// access the successor Node Based on the clue, and the successor node is not the root = root-> rchild; visit (root );} root = root-> rchild;} int main () {ptrThreadBiTree head, biTree = NULL; char * ch = "AB # d # ce ###"; create_BiTree (biTree, ch); // printf ("Forward traversal outputs common Binary Tree: \ n"); // pre_order_traverse (biTree, 1 ); // printf ("=================================\ n"); in_thread (head, biTree ); printf ("middle-order clue Binary Tree: \ n"); mid_order_traverse (head); return 0 ;}

Running result:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.