Chained storage of binary tree
To achieve the basic operation of the two-fork tree: Establishment, traversal, calculation depth, knot number, leaves and so on.
Enter C, first preface to create two fork tree, #表示空节点;
Input h: Compute the height of the binary tree;
Input L: Calculate the number of the leaves of the binary tree;
Input N: Compute the total number of binary tree nodes;
Input 1: First sequence traversing the binary tree;
Input 2: Middle sequence traversal binary tree;
Input 3: Subsequent traversal of the binary tree;
Enter F: Find the number of nodes in the value =x;
Enter P: Prints all nodes in indented text.
It's simply not necessary to explain the code.
#include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Two-fork tree chain storage means * * typedef char DATATYPE;
/* The actual type of user-defined DataType/typedef struct Node {DataType data; Node *lchild, *rchild; * * Left and Right child pointer/} Bintnode;
/* Node type */typedef bintnode *BINTREE;
int sum=0; void Displaybintree (Bintree T); /* Use the text form to represent the binary tree/void Createbintree (Bintree *t); /* Construct two fork list */void preorder (Bintree T); /* Pre-order traversal binary tree/void Inorder (Bintree T); /* In-order traversal binary tree/void Postorder (Bintree T); /* Subsequent traversal of the binary tree/INT nodes (Bintree T); /* COMPUTE Summary points */int leafs (Bintree T); /* Calculate the total number of leaves */int hight (Bintree T); /* Compute the height of the binary tree/int Find (Bintree T,char x);
The number of nodes looking for value =x; int main () {Bintree T;
Char FLG;
while (CIN>>FLG) switch (FLG) {case ' C ': GetChar ();
Createbintree (&t); cout<< "Created success!"
<<endl;
Break Case ' H ': cout<< "height=" <
Above is a binary tree chain storage of a small example, need to learn to students please refer to, thank you support