Data Structure-implement a clue Binary Tree in C Language

Source: Internet
Author: User

Data Structure-implement a clue Binary Tree in C Language

// Clue binary tree, which is added to the binary tree. // Yang Xin # include
 
  
# Include
  
   
Typedef char ElemType; typedef enum {Link, Thread} childTag; // Link indicates the node, and Thread indicates the clue typedef struct bitNode {ElemType data; struct bitNode * lchild, * rchild; int ltag, rtag;} bitNode, * bitTree; bitTree pre; // create a global variable, indicating that the accessed node/* creates a binary tree. The input must be in the order of traversal in the forward order. T: arr of the binary tree root node: values of each node in the descending order. When there is no child node, use a space to replace */void create_tree (bitTree * T, char ** arr) {char c; sscanf (* arr, "% c", & c ); // read a node value (* arr) ++; if (''= c) // if it is a space, it indicates an empty node {* T = NULL ;} else {* T = (bitTree) malloc (sizeof (bitNode); // construct a new node (* T)-> data = c; (* T)-> ltag = Link; (* T)-> rtag = Link; create_tree (& (* T)-> lchild, arr); // construct the left child create_tree (& (* T) of the new node) -> rchild, arr); // construct the right child of the new node}/* Access Node information */void visit (bitTree T) {printf ("| % d | % c | % d | \ n", T-> ltag, T-> data, T-> rtag);}/* preordered traversal of the Access Binary Tree */void pre_order_traverse (bitTree T, int level) {if (T) {visit (T); pre_order_traverse (T-> lchild, level + 1); pre_order_traverse (T-> rchild, level + 1);}/* traversing a binary tree in the central order, define it as a clue */void in_order_threading (bitTree T) {if (T) {in_order_threading (T-> lchild); // turn left child into a clue if (! T-> lchild) // if the left child is empty, direct it to the front {T-> lchild = pre; T-> ltag = Thread;} if (! Pre-> rchild) // if the right child of the previous node is empty, direct the child to the next node. (Note: only access to the next node will know who is next to the node.) {pre-> rchild = T; pre-> rtag = Thread;} pre = T; in_order_threading (T-> rchild); // right-Child clue}/* adds a header node to form a closed loop P: a binary tree with a header node. The left child of the header node points to the binary tree T; the right child points to the last leaf node T in the T tree: a binary tree without the header node. */Void in_thread (bitTree * P, bitTree T) {(* P) = (bitTree) malloc (sizeof (bitNode )); // construct the newly added header node (* P)-> ltag = Link; (* P)-> rtag = Thread; (* P)-> rchild = * P; if (! T) // If the binary tree is empty, P's child points to himself. {(* P)-> lchild = * P;} else {(* P)-> lchild = T; pre = * P; in_order_threading (T ); // generate clues for Binary Trees (* P)-> rchild = pre; // point the right child of the header node to the last leaf node pre-> rtag = Thread; // point the right child of the last leaf node to the header node. In this way, the ring is formed. Pre-> rchild = * P ;}/ * Non-recursive method: traverse a binary tree in the middle order (the tree must have a header node and have been hashed) P: binary Tree with header nodes */void in_order_traverse (bitTree P) {bitTree T; T = P-> lchild; while (T! = P) // determine whether the tree is empty {while (T-> ltag = Link) // starts from the left child until the leaf node {T = T-> lchild ;} visit (T); while (T-> rtag = Thread & T-> rchild! = P) // access the subsequent Node Based on the clue. And the successor node is not the {T = T-> rchild; visit (T) ;}t = T-> rchild ;}} int main () {bitTree P, t; int level = 1; // indicates the depth of the node char * arr = "AB d ce"; // construct the node required for the binary tree (input in the forward traversal Mode) create_tree (& T, & arr); // construct a binary tree printf ("pre_order_traverse: sequential traversal: \ n"); pre_order_traverse (T, level ); // print the output binary tree printf ("in_order_traverse: middle-order traversal: \ n"); in_thread (& P, T); // bin_order_traverse (P ); // return 0 for the binary tree after clue output ;}
  
 

 

 

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.