Create and delete a clue binary tree and create a clue Binary Tree

Source: Internet
Author: User

Create and delete a clue binary tree and create a clue Binary Tree

Description

Clue binary tree concept
1. Definition
The binary linked list of n nodes contains n + 1 null pointer fields. Use the NULL pointer field in the binary linked list to store the pointer pointing to the forward and next nodes in a certain traversal order of the node (this additional pointer is called a "clue ").

The binary linked list with clues is called the chain list, and the corresponding binary tree is called the Threaded BinaryTree ). Based on the different nature of the clue, the clue binary tree can be divided into three types: pre-order clue Binary Tree, middle-order clue Binary Tree, and post-order clue binary tree.
Note:
The clue linked list solves the problem of finding left and right children in a binary linked list, and the problem of failing to directly locate the node in a traversal sequence and the problem of the previous node and the next node.

2. node Structure of the linked list of clues
The node Structure in the linked list of clues is as follows:

Where:
The ltag and rtag are two additional flag domains used to distinguish whether the Left and Right pointer fields of a node point to its left and right child pointers, or to its forward or subsequent clues.

The following is your task: first, create a binary tree based on the input sequence, then make a clue to the binary tree, and finally traverse the binary tree in the middle order and output the result.

Input requirements

The first line of the input contains a separate number T, indicating the number of test sequences;
Each of the following rows is a test sequence. The test sequence is a sequence of first-order input characters. If the node does not have left or right children, the input is represented by a space, end the input of a row with a space.

Output requirements

For each test sequence, traverse the binary clue tree in the middle order and output a row
Assume that

2ABC  DE G  F    -+a  *b  -c  d  /e  f   

Output

CBEGDFA
A + B * c-d-e/f

I wrote a clue binary tree.
Code:

# Include <bits/stdc ++. h> using namespace std; typedef struct BTree {char data; struct BTree * left, * right; int ltag, rtag;} BTree; BTree * CreatBTree () {char ch = getchar (); if (ch = '') return NULL; BTree * temp = (BTree *) malloc (sizeof (BTree )); temp-> data = ch; temp-> ltag = temp-> rtag = 0; temp-> left = CreatBTree (); temp-> right = CreatBTree (); return temp;} void inThread (BTree * p, BTree * & pre) {if (p) {inThread (p-> left, pr E); if (! P-> left) {p-> left = pre; p-> ltag = 1;} if (pre &&! Pre-> right) {pre-> right = p; pre-> rtag = 1;} pre = p; inThread (p-> right, pre );}} void CreateInThread (BTree * T) {BTree * pre = NULL; if (T) {inThread (T, pre); pre-> right = NULL; pre-> rtag = 1 ;}} BTree * first (BTree * p) {while (p-> ltag = 0) p = p-> left; return p ;} BTree * next (BTree * p) {if (p-> rtag = 0) return first (p-> right); else return p-> right ;} void inOrder (BTree * T) {for (BTree * p = first (T); p! = NULL; p = next (p) cout <p-> data;} void delBTree (BTree * T) {BTree * pre = first (T ); BTree * p = next (pre); for (; p! = NULL; pre = p, p = next (p) free (pre);} int main () {int t; cin> t; while (t --) {getchar (); // eat the carriage return BTree * root = CreatBTree (); getchar (); // eat the space CreateInThread (root) at the end ); inOrder (root); cout <endl; delBTree (root);} return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.