Rotate the 90-degree output binary tree counterclockwise (Data Structure Test 2) and the 90-degree Binary Tree

Source: Internet
Author: User

Rotate the 90-degree output binary tree counterclockwise (Data Structure Test 2) and the 90-degree Binary Tree





Rotating a 90-degree print binary tree in a counter-clockwise manner is a special medium-order traversal algorithm.

Rotate 90 degrees counter-clockwise


The implementation is also very simple, similar to the middle-order traversal algorithm. Before the output node value, use a special mark to record the layers and output appropriate spaces.

Code:

Void prtbtree (BiTNode * p, int cur) // rotate the 90-degree output binary tree counterclockwise {if (p) {prtbtree (p-> rch, cur + 1 ); for (int I = 0; I <= cur; I ++) printf (""); printf ("% 3c", p-> data ); printf ("\ n"); prtbtree (p-> lch, cur + 1 );}}



Binary Tree, Data Structure

What I just wrote

# Include <iostream. h>
# Include <stdlib. h>
# Include <stdio. h>

# Define OVERFLOW 0

// Binary Tree binary linked list storage Definition
Struct Node
{
Char data;
Struct Node * lchild, * rchild;
};
Typedef struct Node BiTNode;
Typedef BiTNode * BiTree;

/*************************************** **********************************\
*
* Input the node values (one character) in the binary tree in the first order to construct the Binary Tree represented by the binary linked list,
* The character '#' indicates the empty tree.
*
* For example, the three traversal orders of a binary tree are:
* Preface:-+ a * B-cd/ef preface: a + B * c-d-e/f Suffix: abcd-* + ef/
* In the program, enter-+ a ### * B ##-c ## d ##/ e ## f ##
*
* For example, the three traversal orders of a binary tree are:
* Preface: ABDFGCEH middle direction: BFDGACHE; suffix: FGDBHECA
* Enter AB # DF # G # C # EH ### in the program ###
*
\*************************************** **********************************/
Void CreateBiTree (BiTree & T)
{
Char ch;
Scanf ("% c", & ch); // * input the node values in the binary tree in the First Order (one character) to construct the Binary Tree represented by the binary linked list,
If (ch = '#') T = NULL; // * character '#' indicates an empty tree
Else {
If (! (T = (BiTNode *) malloc (sizeof (BiTNode) exit (OVERFLOW );
T-> data = ch;
CreateBiTree (T-> lchild );
CreateBiTree (T-> rchild );
}
Return;
} // CreateBiTree

Void PreOrderTraverse (BiTree T)
{
// The Node value (one character) in the binary tree T is output in the order of first traversal, And the binary tree T is stored in a binary linked list.

If (T)
{
Cout <T-> data;
PreOrderTraverse (T-> lchild );
PreOrderTraverse (T-> rchild );
}
Return;
} // PreOrderTraverse

Void InOrderTraverse (BiTree T)
{
// The Node value (one character) in the binary tree T is output in the ascending order, and the binary tree T is stored in the binary linked list.

If (T)
{
InOrderTraverse (T-> lchild );
Cout <T-> data;
InOrderTraverse (T-> rchild) ...... remaining full text>

Binary Tree Data Structure

# Include <iostream>
# Include <cmath> // provides the pow () function.
Using namespace std;

# Define OK 1
# Define ERROR 0

Typedef int Status;
Typedef struct BiTNode
{
Int data;
Struct BiTNode * lChild, * rChild;
} BiTNode, * BiTree;

// Output a binary tree in ascending order
Void PreOrderTraverse (BiTree T)
{
If (T)
{
Cout <T-> data <'';
PreOrderTraverse (T-> lChild );
PreOrderTraverse (T-> rChild );
}
}

// Output a binary tree in the middle order
Void InOrderTraverse (BiTree T)
{
If (T)
{
InOrderTraverse (T-> lChild );
Cout <T-> data <'';
InOrderTraverse (T-> rChild );
}
}

// Output binary tree in descending order
Void PostOrderTraverse (BiTree T)
{
If (T)
{
PostOrderTraverse (T-> lChild );
PostOrderTraverse (T-> rChild );
Cout <T-> data <'';
}
}

// Create a binary tree
Status CreateBiTree (BiTree & T)
{
Int num;
Cin> num;
If (num = 0)
T = NULL;
Else
{
T = (BiTree) malloc (sizeof (BiTNode ));
T-> data = num;
CreateBiTree (T-> lChild );
CreateBiTree (T-> rChild );
}
Return OK;
}

// Calculate the depth of a binary tree
Int BiTreeDepth (BiTree T)
{
Int depth1, dep22;
If (! T)
Return 0;
Else {
Depth1 = BiTreeDepth (T-> lChild );
Depth1 = BiTreeDepth (T-> rChild );
If (depth1> dep22)
Return depth1 + 1;
Else
Return depth1 + 1;
}
}

Static int n = 0;
Int CountNode (BiTree T)
{
If (T)
{
N ++;
CountNode (T-> lChild );
CountNode (T-> rChild );
}
Return n;
}

// Calculate the number of knots of a full binary tree whose depth is depth
Inline int Count (int depth)
{
Return pow (2, depth)-1; // The Node calculation method of the full Binary Tree
}

Int ma ...... remaining full text>

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.