Binary tree:
Each node has two child nodes, a left child node, and a right child node.
Node structure:
typedef struct NODE
{
Char Val;
NODE *left;
NODE *right;
} NODE;
Generate a two-tuple tree:
The recursive algorithm is used to generate new nodes and join the tree, ' # ' represents the empty node
node* Treeconstructor () {char ch;cin>>ch; NODE *root;if (ch== ' # ') {return NULL;} Else{root=new node;root->val=ch;root->left=treeconstructor (); Root->right=treeconstructor (); return root;}}
Traversing a two-tuple tree: Deep traversal of two-tuple trees: Using the STL container stack implementation, the node advanced out, first into the right node, and then into the left node, that is, advanced, first right after the left; step: 1, into the root node, 2, the top element of the stack, 3, if the stack top element right child non-empty, into the stack; , if the stack is non-empty, turn to 2.void depthfirstsearch (NODE *root)
{
Stack<node*> Node_stack;
Node_stack.push (root);
NODE *node;
while (! Node_stack.empty ())
{
Node=node_stack.top ();
Node_stack.pop ();
cout<<node->val<< "";
if (node->right!=null)
{
Node_stack.push (Node->right);
}
if (node->left!=null)
{
Node_stack.push (Node->left);
}
}
}
Breadth traversal Two-tuple tree: Using the STL container queue implementation, node advanced first out, first into the left node, then into the right node, that is, advanced first out, first left after the right; step: 1, into the root node; 2, the first element of the team, 3, if the team first element left child non-empty, join the team tail, 4, if the team first element right child non-empty, join If the team is not empty, turn to 2.void breadfirstsearch (NODE *root)
{
Queue<node*> Node_queue;
Node_queue.push (root);
NODE *node;
while (! Node_queue.empty ())
{
Node=node_queue.front ();
Node_queue.pop ();
cout<<node->val<< "";
if (node->left!=null)
{
Node_queue.push (Node->left);
}
if (node->right!=null)
{
Node_queue.push (Node->right);
}
}
}
First order, middle order, post-sequence traversal: recursive algorithm is adopted; Shortest path: Method one: Using recursive algorithm
int mindepth (NODE *root) {if (root==null) return 0;if (root->left==null&&root->right==null) return 1;int Leftdepth=mindepth (root->left), int rightdepth=mindepth (root->right), if (leftdepth==0) {return rightDepth+1;} if (rightdepth==0) {return leftdepth+1;} return min (leftdepth,rightdepth) +1;}
Suppose you have the following tree:
The recursive demo diagram is as follows:
Where 1,2,3,4,5,6 refers to the steps of the run, from left to right call the Mindepth function, the black line is the initial Entry function, the function returns the route for the function. Method Two:The two-fork tree is BFS, because it is traversed by the layer, so if a leaf node is found in a layer, then the minimum depth is found, at which point the current depth can be returned.
The code is as follows:
int minDepth1 (NODE *root) { if (root = NULL) return 0; int depth = 1; int currentlevel = 1; int nextlevel = 0; Queue<node*> Node_queue; Node_queue.push (root); while (! Node_queue.empty ()) { NODE *node = Node_queue.front (); Node_queue.pop (); currentlevel--; if (Node->left = = NULL && Node->right = = null) { return depth; } if (node->left! = NULL) { node_queue.push (node->left); nextlevel++; } if (node->right! = NULL) {Node_queue.push (node->right); nextlevel++; } if (CurrentLevel = = 0) { if (nextlevel! = 0) { depth++; } CurrentLevel = Nextlevel; Nextlevel = 0; } } return depth; }
The complete code is as follows:
Binary_tree.h
#include <iostream> #include <queue> #include <stack> #include <vector> #include <algorithm >using namespace Std;typedef struct Node{char val; NODE *left; NODE *right;} NODE; enum Order_mode {order_mode_prev = 0, Order_mode_mid, Order_mode_post};//enumeration, representing the way to traverse the tree (preamble, middle order, post order) node* Treeconstructor (); void Depthfirstsearch (node *root); void Breadfirstsearch (node *root); int mindepth (node *root); int minDepth1 (NODE *root); void Printtree (Order_mode method,node *root); void Printtreeinpre (NODE *root); void Printtreeinmid (NODE *root); void Printtreeinpost (NODE *root);
Main.cpp:
#include "binary_tree.h" int index=0; node* Treeconstructor () {char ch;cin>>ch; NODE *root;if (ch== ' # ') {return NULL;} Else{root=new node;root->val=ch;root->left=treeconstructor (); Root->right=treeconstructor (); return root;}} void Depthfirstsearch (NODE *root) {stack<node*> node_stack; Node_stack.push (root); NODE *node;while (! Node_stack.empty ()) {node=node_stack.top (); Node_stack.pop ();cout<<node->val<< ""; if (node->right!=null) {Node_stack.push (node->right);} if (node->left!=null) {Node_stack.push (node->left);}}} void Breadfirstsearch (NODE *root) {queue<node*> node_queue; Node_queue.push (root); NODE *node;while (! Node_queue.empty ()) {Node=node_queue.front (); Node_queue.pop ();cout<<node->val<< ""; if (node->left!=null) {Node_queue.push (node->left);} if (node->right!=null) {Node_queue.push (node->right);}}} int mindepth (NODE *root) {if (root==null) return 0;if (root->left==null&&root->right==null) return 1;int Leftdepth=mInDepth (root->left), int rightdepth=mindepth (root->right), if (leftdepth==0) {return rightdepth+1;} if (rightdepth==0) {return leftdepth+1;} return min (leftdepth,rightdepth) +1;} int minDepth1 (NODE *root) {if (root = NULL) return 0; int depth = 1; int currentlevel = 1; int nextlevel = 0; Queue<node*> Node_queue; Node_queue.push (root); while (! Node_queue.empty ()) {NODE *node = Node_queue.front (); Node_queue.pop (); currentlevel--; if (Node->left = = NULL && Node->right = = null) {return depth; } if (Node->left! = NULL) {Node_queue.push (node->left); nextlevel++; } if (node->right! = NULL) {Node_queue.push (node->right); nextlevel++; } if (CurrentLevel = = 0) {if (Nextlevel! = 0) {depth++; } CurrentLevel = Nextlevel; Nextlevel = 0; }} return depth; } void Printtree (Order_mode method,node *root) {if (Order_mode_prev==method) {printtreeinpre (root);} if (Order_mode_mid ==method) {printtreeinmid (root);} if (Order_mode_post==method) {printtreeinpost (root);}}; void Printtreeinpre (NODE *root) {if (root) {cout<<root->val<< "";p rinttreeinpre (Root->left); Printtreeinpre (Root->right);}} void Printtreeinmid (NODE *root) {if (root) {Printtreeinmid (root->left);cout<<root->val<< ""; Printtreeinmid (Root->right);}} void Printtreeinpost (NODE *root) {if (root) {printtreeinpost (root->left);p rinttreeinpost (root->right); cout <<root->val<< "";}} void Main () {NODE *root;root=treeconstructor ();cout<< "depth traversal:";d epthfirstsearch (Root);cout<<endl< < "breadth traversal:"; Breadfirstsearch (Root);cout<<endl<< "First Order traversal:";p rinttree (order_mode_prev,root);cout< <endl<< "Middle sequence Traversal:";p rinttrEE (order_mode_mid,root);cout<<endl<< "post-post traversal:";p rinttree (order_mode_post,root);cout<<endl< < "minimum path:"; int min1= mindepth (root);cout<<min1<< ""; int min2=mindepth1 (root); cout<<min2;}Run the results once:
Binary tree generation, traversal, and shortest path query