Create a binary tree in layers and create a binary tree
First:
The relationship between Tree node types array and Binary Tree node numbers is mainly used to create:
If the parent node number is I, the left son node number is 2 * I, and the right son node number is 2 * I + 1.
// Create a binary tree using the hierarchical Traversal method # include <iostream> # include <queue> using namespace std; // The structure type of the binary linked list is defined as const int maxsize = 1024; typedef char datatype; typedef struct node {datatype data; struct node * lchild, * rchild;} bitree; bitree * creattree (); void PreOrder (bitree *); // TestCase: ebfad. g .. c # void main () {bitree * pb = creattree (); PreOrder (pb); cout <endl; system ("pause ");} // establish a binary tree through hierarchical traversal. 1 bitree * creattree () {char ch; int front = 1, rear = 0; bitree * root, * s; root = NULL; bitree * Q [maxsize]; // front is used to point to the parent node. Printf ("input binary tree by level, input '.' By virtual node, end with '#' input: \ n"); while (ch = getchar ())! = '#') {S = NULL; if (ch! = '. ') {// Allocate space for the node and set the data domain, pointer domain s = (bitree *) malloc (sizeof (bitree); s-> data = ch; s-> lchild = NULL; s-> rchild = NULL;} // enter a node and put it in the array rear ++; Q [rear] = s; // root node: if (rear = 1) root = s; else {// if it is a virtual node, if the application is less than s, you do not have to consider how to mount the node s if (s & Q [front]). {// if the subscript is an even number, it will be hung on the left subtree if (rear % 2 = 0) Q [front]-> lchild = s; // If the subscript is an odd number, else Q [front]-> rchild = s ;} // after the two nodes are set, the subscript of the parent node must be changed. if (rear % 2 = 1) front ++ ;}} return root ;} // First traverse the output void Visit (bitree * T) {if (T-> data! = '#') {Printf ("% c", T-> data) ;}} void PreOrder (bitree * T) {if (T! = NULL) {Visit (T); PreOrder (T-> lchild); PreOrder (T-> rchild );}}
Second:
Queues are used to create layers of Binary Trees.
(1) The first step is very special. The first step is the root of the tree.
Binary_node * pNode = A. front ();
A. pop ();
B. push (pNode );
A: bfad. g.. c
B: e
Tree:
(2) Each of the following steps is to extract two leaders from queue A and put them at the end of queue B (if it is NULL, it is not put ). Removes the first line from B, and the elements in queue A are exactly the children who retrieve the elements in queue B.
Binary_node * pfather = B. front ();
B. pop ();
Binary_node * pLchild = A. front (); // the child is left first.
A. pop ();
Binary_node * pRchild = A. front ();
A. pop ();
Pfather-> left = pLchild;
Pfather-> right = pRchild;
// Put the left child first
If (pLchild! = NULL)
{
B. push (pLchild );
}
If (pRchild! = NULL)
{
B. push (pRchild );
}
A: ad. g.. c
B: bf
Tree:
(3)
A:. g.. c
B: fad
Tree:
(4)
A:. c
B: adg
Tree:
(5)
A: c
B: dg
Tree:
(6)
A: NULL (when queue A is empty, the entire algorithm ends and the tree is constructed successfully)
B: g
Tree:
Complete.
BuildTree (Binary_node * & root) {char nodeData; // The data queue in the node <char> treeDataQueue; queue <Binary_node *> treeNodeQueue; // queue <Binary_node *> treeFatherQueue; // Binary_node * pTreeNode; // The Node Binary_node * pfather; Binary_node * pLchild; Binary_node * pRchild; while (! TreeDataQueue. empty () {nodeData = treeDataQueue. front (); treeDataQueue. pop (); if (nodeData! = '. ') PTreeNode = new Binary_node (nodeData); else pTreeNode = NULL; treeNodeQueue. push (pTreeNode);} // root node pTreeNode = treeNodeQueue. front (); treeNodeQueue. pop (); root = pTreeNode; treeFatherQueue. push (pTreeNode); while (! TreeNodeQueue. empty () {pfather = treeFatherQueue. front (); treeFatherQueue. pop (); pLchild = treeNodeQueue. front (); treeNodeQueue. pop (); if (pLchild! = NULL) {pfather-> left = pLchild; treeFatherQueue. push (pLchild);} if (! TreeNodeQueue. empty () {pRchild = treeNodeQueue. front (); treeNodeQueue. pop (); if (pRchild! = NULL) {pfather-> right = pRchild; treeFatherQueue. push (pRchild);} // end if (pRchild! = NULL)} // end if (! TreeNodeQueue. empty ()} // end while (! TreeNodeQueue. empty ())}