Data Structure-Binary Tree

Source: Internet
Author: User

// Binary Tree
// Time: 05/07/08
// Program: Zhang jianbo
// Function: input a binary tree to complete pre-order, middle-order, post-order, and sequence Traversal

# Include <iostream. h>
# Include "menu. H"
# Include "key. H"

Struct tree
{
Int data; // node data
Struct tree * left; // left subtree
Struct tree * right; // right subtree
};

Struct Mt {
Int data [100]; // save each layer of Elements
Int N; // number of elements on each layer
}; // The node where sequence traversal stores binary trees

Struct Mt mT [100]; // assume that the maximum number of layers of a binary tree cannot exceed 100

Typedef struct tree treenode; // structure type of the tree
Typedef treenode * btree; // indicator type of the Tree node

Void _ inputdata (int * data); // input binary data

Btree insertnode (btree root, int value); // insert a binary tree node
Btree createbtree (int * data, int Len); // create a binary tree
Void inorder (btree PTR); // sequential traversal of Binary Trees
Void preorder (btree PTR); // pre-sequential traversal of Binary Trees
Void postorder (btree PTR); // post-sequential traversal of Binary Trees
Void Cx (btree PTR, int N); // Binary Tree sequence Traversal

Void _ preorder_main (int * data, int N); // preorder traversal _ Test Program
Void _ inorder_main (int * data, int N); // In-order traversal _ Test Program
Void _ postorder_main (int * data, int N); // list_test Program
Void _ cenxu_order_main (int * data, int N); // sequence traversal _ Test Program

Void _ viewtree_main (int * data, INT); // view tree
Int _ edit_data (int * data); // modify the initial data.

Int _ f3_main () {// program entry
Menu M [10]; // draw a menu
M [1]. Name = "input Tree ";
M [2]. Name = "browser Tree ";
M [3]. Name = "Forward traversal ";
M [4]. Name = "sequential traversal ";
M [5]. Name = "sequential traversal ";
M [6]. Name = "sequence traversal ";
M [7]. Name = "return ";
M [8]. Name = "";
Int data_tree [1000] = {5, 6, 4, 8, 2, 3, 7, 1, 9 };
Int n = 9;
Int * Data = data_tree;
Int id = 1;
While (1)
{
Showmenu ("Data Structure-binary tree traversal", M, 8); // display menu
Id = selectmenuid (); // obtain the selected menu ID
Switch (ID ){
Case 1: {n = _ edit_data (data); initkey ();} break;
Case 2: {_ viewtree_main (data, n); initkey ();} break;
Case 3: {_ preorder_main (data, n); initkey ();} break;
Case 4: {_ inorder_main (data, n); initkey ();} break;
Case 5: {_ postorder_main (data, n); initkey ();} break;
Case 6: {_ cenxu_order_main (data, n); initkey ();} break;
Case 7: Return 0;
Case 8: break;
}
}
Return 0;
}

Int _ edit_data (int * Data) {// modify the initial data
Int N;
Cout <"/n enter the total number of nodes N = ";
Cin> N;
Cout <"input data of each node" <Endl;
Cout <"for example, input in sequence: 5, 6, 4, 8, 2, 3, 7, 1, 9/N" <Endl;
For (INT I = 0; I <n; I ++) {// input data
Cout <"[" <I <"] = ";
Cin> data [I];
Cout <"/N ";
}
Cout <"/N :";
For (Int J = 0; j <n; j ++) cout <data [J] <"";
Return N;
}

Btree insertnode (btree root, int value) {// insert a binary tree node

Btree newnode; // tree root
Btree current; // current Tree node metrics
Btree back; // parent node indicator
Newnode = new treenode; // create a new node
Newnode-> DATA = value; // create node content
Newnode-> left = NULL; // you can specify the initial value of a metric.
Newnode-> right = NULL; // you can specify the initial value of a metric.
If (root = NULL) return newnode; // It is the location where the root node returns data to the new node.
Else
{
Current = root; // retain the current tree indicator
While (current! = NULL)
{
Back = current; // reserve the parent node metrics
If (current-> DATA> value) // compare the node Value
Current = Current-> left; // left subtree
Else
Current = Current-> right; // right subtree
}
If (back-> DATA> value) Back-> left = newnode; // left subtree
Else back-> right = newnode; // right subtree
}
Return root; // return the root of the tree
}

Btree createbtree (int * data, int Len) {// create a binary tree
Btree root = NULL; // tree root indicator
Int I;

For (I = 0; I <Len; I ++) // use a loop to create a Tree Structure
Root = insertnode (root, data [I]);
Return root;
}
Void preorder (btree PTR) {// Binary Tree preorder traversal
If (PTR! = NULL)
{
Cout <PTR-> data <"";
Preorder (PTR-> left );
Preorder (PTR-> right );
}
}

Void inorder (btree PTR) {// sequential traversal of a binary tree
If (PTR! = NULL) // termination condition
{
Inorder (PTR-> left); // left subtree
Cout <PTR-> data <"";
Inorder (PTR-> right); // right subtree
}
}

Void postorder (btree PTR) {// post-sequential traversal of Binary Trees
If (PTR! = NULL)
{
Postorder (PTR-> left );
Postorder (PTR-> right );
Cout <PTR-> data <"";
}
}

Void _ preorder_main (int * data, int N) {// preorder traversal _ Test Program
Btree root = NULL;
Root = createbtree (data, n); // create a binary tree
Cout <"Tree node content n = ";
Preorder (Root); // traverse Binary Trees in ascending order
}

Void _ inorder_main (int * data, int N) {// ordinal traversal _ Test Program
Btree root = NULL;
Root = createbtree (data, n); // create a binary tree
Cout <"Tree node content n = ";
Inorder (Root); // traverses binary trees in the middle order
}

Void _ postorder_main (int * data, int N) {// traverse the column in descending order _ Test Program
Btree root = NULL;
Root = createbtree (data, n); // create a binary tree
Cout <"Tree node content n = ";
Postorder (Root); // post-order traversal of Binary Trees
}
//~

//////////////////////////////////////// //////

Int max_n = 0; // maximum number of storage layers
Void Cx (btree PTR, int N) // sequential Traversal
{
If (PTR! = NULL)
{
N ++; // layer count

If (max_n <n) max_n = N; // maximum number of storage layers

MT [N]. Data [MT [N]. N] = PTR-> data; // Save the data of the current Layer
MT [N]. n = MT [N]. n + 1; // Add the number of elements in the layer to 1.
CX (PTR-> left, n); // continue browsing the left Node
CX (PTR-> right, n); // continue to browse the right Node
}
}

Void _ cenxu_order_main (int * data, int N) {// sequence Traversal

Btree root = NULL;

Root = createbtree (data, n); // create a binary tree

Cout <"Tree node content ";

For (INT m = 0; m <100; m ++) MT [M]. n = 0;


CX (root, 0 );

Cout <"the data of each layer of the binary tree is as follows:" <Endl;
For (INT Mm = 0; mm <= max_n; mm ++)
{
For (INT Mn = 0; Mn <mT [mm]. N; Mn ++)
{
Cout <mT [mm]. Data [Mn] <"";
}
Cout <"/N ";
}
}

Void viewtree (btree PTR) {// view tree
If (PTR! = NULL) // termination condition
{
Cout <PTR-> data;
If (PTR-> left! = NULL | PTR-> right! = NULL)
{
Cout <"(";
Viewtree (PTR-> left );
If (PTR-> right! = NULL) cout <",";
Viewtree (PTR-> right );
Cout <")";
}
}
}

Void _ viewtree_main (int * data, int N) // view tree
{

Btree root = NULL;

Root = createbtree (data, n); // create a binary tree

Cout <"Tree node content n = ";

Viewtree (Root );

}

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.