Binary Tree depth algorithm
Algorithm idea:
Uses a binary tree's post-order traversal non-recursive algorithm. Since the post-sequential traversal non-recursive algorithm uses a stack implementation, each time it is accessed at the bottom of a path, it is accessed to the right. Therefore, the maximum value of stack traversal is the maximum depth of binary trees.
# Include <iostream> # include <stack> using namespace std; struct BinTree {int data; BinTree * lc; BinTree * rc;} BTNode, * BinTree; int max (int, int B) {return (a> B )? A: B;} int BinTreeDepth (BinTree T) {stack <BinTree> s; BinTree p = T, r = NULL; int depth = 0; while (p |! S. empty () {if (p) {// walk s from the root node to the left. push (p); int size = s. size (); // Obtain the stack size depth = max (depth, size); // replace the maximum value p = p-> lc ;} else {// the left cannot go to the right p = s. top (); if (p-> rc & p-> rc! = R) // if the right subtree exists and has not been accessed {p = p-> rc; s. push (p); int size = s. size (); // Obtain the stack size depth = max (depth, size); // replace the maximum value p = p-> lc;} else {p = s. top (); s. pop (); cout <p-> data <endl; r = p; // record the recently accessed node p = NULL; // after the node is accessed, reset the p pointer to prevent the left child from being pushed to the stack again.} return depth ;}
Algorithms for finding binary tree depth
Question: The binary tree is represented by a binary linked list and an algorithm used to calculate the depth of the binary tree is compiled.
The answer is:
Int height (Bitree T) {if (T = NULL) return 0; u = height (T-> lchild); v = height (T-> rchild ); if (u> n) return (u + 1) return (v + 1 )}
You can think of recursion as running a sentence. When you need to save the status, the system will automatically use the stack to save it for you. Let's take your example:
N is a global variable and the initial value is 0;
Height (T) is called for the first time. Suppose T! = NULL
Because T! = NULL: Skip if (T = NULL) return 0;
The key is u = height (T-> lchild). Call the function itself: at this time, T-> lchild is saved in the stack. If it is a call, it must start with the function:
Take a look at this time T2 (actually T-> lchild), if (T = NULL) return 0;
Assume that T is not NULL, and then continue to run in the case of u = height (T-> lchild); call this function itself --
Suppose it is T-> lchild = NULL. This is fine. This function executes return 0;
Slow: The second-level function call u = height (T-> lchild) has been computed. At this time, u = 0;
Do you still remember the second call to run v = height (T-> rchild? Well, this process is exactly the same as u = height (T-> lchild.
Assume that the obtained value is v = 0.
Then the second call to if (u> n) return (u + 1)
Return (v + 1)
Assume that u> n is returned, and 1 is returned;
Okay. This wave is complete;
You still remember the height of the first call, and then return the value to u, u = 1;
Then run v = height (T-> rchild); in the first call. The analysis is the same as above.
This process is indeed complicated. Think about it. Haha.
The basic idea is that if the current node has a subnode, it will continue to access and Recursively search for the subnode until the leaf node.
Procedure tree (a: node, depth: integer); begin if result <depth then result: = depth; if. leftchild <> nil then tree (. leftchild, depth + 1); if. rightchild <> nil then tree (. rightchild, depth + 1); end;
Note: result is a global variable and a result.
In fact, we 'd better not use any global variables.
Int depth (node * bt) {if (bt = NULL) return 0; ldepth = depth (bt-> left) + 1; rdepth = depth (bt-> right) + 1; return max (ldepth, rdepth );}
The global variable is a bug.
Int Height (BiTree T) {int m, n; if (! T) return (0); else m = Height (T-> lchild); n = Height (T-> rchild); return (m> n? M: n) + 1 );}
Recursive Algorithms for tree depth
// Struct bnode {struct bnode * lc, * rc); int depth (struct bnode * r) {return r = NULL? 0: 1 + max (depth (r-> lc), depth (r-> rc ));}
Q: design an algorithm to find the depth of a binary tree
A binary tree is stored in a binary linked list, and the depth of this binary tree is calculated by designing an algorithm.
Answer:
# Include <stdio. h> # include <stdlib. h> typedef struct node {char data; struct node * left, * right;} Node, * PNode; PNode createBtree (PNode root) // create a binary tree, input in the console, input {char data; scanf ("% c", & data); if (data = '') {root = NULL; return root ;} root = (PNode) malloc (sizeof (Node); root-> data = data; root-> left = createBtree (root-> left ); root-> right = createBtree (root-> right); return root;} int depth (PNode root) // This is the function you want. {Int ld, rd; if (root = NULL) {return 0;} ld = 1 + depth (root-> left ); rd = 1 + depth (root-> right); return ld> rd? Ld: rd;} int main () {PNode root = NULL; root = createBtree (root); printf ("% d", depth (root); return 0 ;}
For testing, a binary tree creation program is written;
The result is displayed as follows:
Virtual nodes are entered with spaces. For example, if you enter
First-order traversal
234 space, 5 space, 6 space, 7 space, and press enter to see the result.
In addition, this algorithm calculates the depth from 1, that is, the root node is under the depth.