Image tree __c of binary tree

Source: Internet
Author: User

The so-called binary tree is the mirror tree is a node under the left and right children to exchange, for example:


This tree below is a complete binary tree with a depth of 3.

Then its mirror image should be


This problem is not difficult, the main thing is to build up the tree, and then the left and right node Exchange can be

Because the format of the data entered is

(A (B (D) (E)) (C (F)) in this form, (that is, the structure of the first binary tree above) we need to parse the string and build a tree (it's a bit of a hassle, for I've just touched it)


My idea is to use the stack to do the problem.

Step one: Initialize the pointer stack of a tree structure, read the first bracket, initialize a root node, and push the stack

Step Two: Start reading the string (the string is absolutely correct), and when you encounter ' (') initialize a child node, press into the stack,

The third step: The judge should add this node to that position, in fact, is to get the stack inside the top of the stack element, judge it's left and right children empty, priority left, next to the right-hand.

Fourth step: Encounter ') ' directly to the current node pointer from the stack inside the bounce off.

The condition of the loop is that the string is not '


Specific paste code:

#include <stdio.h>
#include <stack>
#include <stdlib.h>
#include <iostream>
using namespace Std;


Node structure of binary tree
typedef struct node{
int data;
struct Node *lchild;
struct Node *rchild;
}node,*tree;



Turn data into a tree
Tree Inittree (char *str) {
Stack<tree> s;
Stack<char> C;
Tree t,t,temp;
To create a root node
C.push (*STR);
str++;
t= (node *) malloc (sizeof (node));
t->data=*str;
t->lchild=null;
t->rchild=null;
S.push (T);
str++;
To create a relationship between nodes
while (*STR) {
if (*str== ' (') {
C.push (' C ');
temp= (node *) malloc (sizeof (node));
temp->data=* (++STR);
temp->lchild=null;
temp->rchild=null;
T=s.top ()//Get parent node
if (t->lchild==null)
t->lchild=temp;
Else
t->rchild=temp;
S.push (temp);
}
else if (*str== ') ') {
S.pop ();
C.pop ();
}
str++;
}
return T;
}

First-order traversal, the basic operation of a binary tree
void preorder (tree T) {
if (T) {
printf ("%c", t->data);
Preorder (T->lchild);
Preorder (T->rchild);
}
}

/* This is the binary tree mirroring conversion function, in fact, very simple

The same way as swapping the value of a variable

*/
void convert (tree T) {
if (null==t)
Return
Tree temp=t->lchild;//Temporary Node
t->lchild=t->rchild;
t->rchild=temp;

Recursive operations
Convert (T->lchild);
Convert (T->rchild);
}

int main () {
Freopen ("Test.in", "R", stdin);
Char str[200];
Char *s=str;
Tree T;
while (scanf ("%s", str)!=eof) {
T=inittree (s);
Convert (T);
Preorder (T);
cout<<endl;
}
return 0;
}


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.