Length of the weighted path of a binary tree (deep search or extensive search) -- 408 of my postgraduate entrance exam

Source: Internet
Author: User

My postgraduate entrance exam ended yesterday. I did not take a good job in professional courses. It was 408 million. It attracted no more attention than the data structure algorithm questions every year, 13 points! This year, the length of the weighted path of a binary tree was obtained. I wrote the algorithm IDEA and the code was blank. Because the time was too late and I was panic, it was a mentality problem, in the end, there was a feeling that the sky would collapse. I was very frustrated when I came back, because I felt that I could write a program, so I was not satisfied!

Let's talk about this question. Tree's length definition: The sum of the length of all leaves in the tree. For example, in the following tree, WPL is 4*2 + 3*1 = 11.



1. The deep search (pre-sequential traversal) algorithm can be recursive. The program is simple. First, judge whether the current root is a leaf. If so, add the weight path length of the leaf, the depth of a leaf can be passed as a parameter. Finding depth is a problem that must be solved. Then recursion is performed on the left and right subtree. Because it is recursive, I use global variables to record parts. I don't know if there are any methods without global variables. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/suO1xMnutsijrM7Sss6/vMHLzfW1wMrpyc + kernel/roaM8L3A + kernel/WyOfPwqOovai1xMrHyc/kernel = "brush: java; "> /************************************* * ****************************** created: created: 14: 42 file base: mainfile ext: cppauthor: Justme0 (http://blog.csdn.net/Justme0) purpose: calculate the length of the weighted path ********************************* * **********************************/# define _ CRT_SECURE_NO_WARNINGS # include # Include # Include Using namespace std;/*** Node definition of the binary linked list */struct Node {Node * left; Node * right; int weight; Node (): left (NULL ), right (NULL), weight (0) {} bool has_left () const {return NULL! = Left;} bool has_right () const {return NULL! = Right;} bool is_leaf () const {return! Has_left ()&&! Has_right () ;}};# pragma region Deep Search int wpl_dfs = 0;/*** preorder traversal to find the length of the weighted path (weighted path length) of the tree) */void DFS (Node * root, int depth) {if (root = NULL) {// root is an empty tree return;} if (root-> is_leaf ()) {wpl_dfs + = root-> weight * depth;} DFS (root-> left, depth + 1); DFS (root-> right, depth + 1 );} int get_WPL_dfs (Node * root) {DFS (root, 0); return wpl_dfs ;} # pragma endregion Deep Search # pragma region wide search/*** retrieve the tree's weighted path length (weighted path length) */int get_WPL_bfs (Node * root) {if (root = NULL) {// root is an empty tree return 0;} int wpl_bfs = 0; int depth = 0; Node * last = root; // last is the address of the rightmost node on each layer, used to determine the height of the queue Q; Q. push (root); do {Node * p = Q. front (); Q. pop (); if (p-> is_leaf () {wpl_bfs + = p-> weight * depth;} else {if (p-> has_left () {Q. push (p-> left);} if (p-> has_right () {Q. push (p-> right) ;}} if (p = last &&! Q. empty () {// process to the rightmost node of the layer last = Q. back (); // The End of the team is exactly the address of the rightmost node of the next layer + + depth ;}} while (! Q. empty (); return wpl_bfs;} # pragma endregion wide search/*** dynamic generation tree of input pre-sequential sequence */void creat_tree (Node * & root) {int info; cin> info; if (info =-1) {root = NULL;} else {root = new Node; root-> weight = info; creat_tree (root-> left); creat_tree (root-> right);}/*** post-order traversal of the release tree */void free_tree (Node * & root) {if (root = NULL) {return;} free_tree (root-> left); free_tree (root-> right); delete root; root = NULL ;} int main (int argc, char ** argv) {freopen ("cin.txt", "r", stdin); Node * tree = NULL; creat_tree (tree ); int dfs_wpl = get_WPL_dfs (tree); int bfs_wpl = get_WPL_bfs (tree); assert (dfs_wpl = bfs_wpl); cout <"Deep Search: WPL = "<dfs_wpl <endl; cout <" wide search: WPL = "<bfs_wpl <endl; free_tree (tree); return 0 ;} /* cin.txt 1 2-1 4-1-1 3-1-1 */

Btw: The algorithm I wrote during the examination was extensive search. Then I thought that I had to consider the depth of the queue used in the program. I was blinded and only wrote a function header, hope you can give me some points...




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.