Create a binary tree, generate clues to the tree, and traverse the tree clues

Source: Internet
Author: User

// First create a binary tree to generate clues for the tree and traverse the tree.
# Include <stdio. h>
# Include <malloc. h>
# Define elemtype char
Typedef Enum tag {link, thread };
 
Typedef struct threadtree
{
Elemtype data;
Struct threadtree * lchild, * rchild;
Tag ltag, rtag;
} Threadtree, * sthreadtree;

// Global variable
Sthreadtree pre;

// Create a binary tree, similar to sequential Traversal
Void creat (sthreadtree & T)
{
Char ch;
Scanf ("% C", & Ch );
If (CH = '#')
T = NULL;
Else
{
T = (sthreadtree) malloc (sizeof (threadtree ));
T-> DATA = CH;
T-> ltag = link; t-> rtag = link;
Creat (t-> lchild );
Creat (t-> rchild );
}
}

// Sequential Traversal
Void mid (sthreadtree T)
{
If (t)
{
Mid (t-> lchild );
Printf ("% C", T-> data );
Mid (t-> rchild );
}
}

// Clue-Based Binary Tree, similar to sequential Traversal
Void inthread (sthreadtree T)
{

If (t)
{
Inthread (t-> lchild );
If (! T-> lchild) {T-> ltag = thread; t-> lchild = pre ;}
If (! Pre-> rchild) {pre-> rtag = thread; pre-> rchild = T ;}
Pre = T;
Inthread (t-> rchild );
}
}

// Add a header Node
Void inorderthread (sthreadtree & head, sthreadtree T)
{
// Sthreadtree pre;
Head = (sthreadtree) malloc (sizeof (threadtree ));
Head-> ltag = link; head-> rtag = thread;
Head-> rchild = head;
If (! T) Head-> lchild = head;
Else
{
Head-> lchild = T; Pre = head;
Inthread (t );
Pre-> rchild = head; pre-> rtag = thread;
Head-> rchild = pre;
}
}

// Link the Binary Tree
Void inordertraverse (sthreadtree T)
{
Sthreadtree P;
P = T-> lchild;
While (P! = T)
{
While (p-> ltag = link) P = p-> lchild;
Printf ("% C", p-> data );
While (p-> rtag = thread & P-> rchild! = T)
{
P = p-> rchild;
Printf ("% C", p-> data );
}
P = p-> rchild;
}
}

Void main ()
{
Sthreadtree T = NULL;
Sthreadtree T = NULL; // head
Printf ("Enter the sequence of the tree to be created:/N ");
Creat (t); // mid (t); printf ("/N ");
Inorderthread (t, t );
Inordertraverse (t );
Printf ("/N ");
}
/*
Enter the sequence of the tree to be created:
Hda ## C # B ## GF # E ###
Adcbhfeg
Press any key to continue
*/

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.