[Data structure and algorithm] Binary Tree breadth Traversal

Source: Internet
Author: User

It is relatively simple to think about the breadth traversal of a binary tree. The left son and right son of the current node are stored in the queue for future access.

  • Code Implementation

/*** Source code name: treebfs. java * Date: * program function: Binary Tree breadth traversal * copyright: [email protected] * a2bgeek */import Java. util. using list; import Java. util. queue; public class treebfs {class treenode <t> {private t mnodedata; private treenode <t> mleftchild; private treenode <t> mrightchild; Public treenode (t data, treenode <t> left, treenode <t> right) {// todo auto-generated constructor stubmnodedata = data; mleftchild = left; Mrightchild = right;} public t getdata () {return mnodedata;} public void setdata (t data) {mnodedata = data;} public treenode <t> getleft () {return mleftchild;} public void setleft (treenode <t> left) {mleftchild = left;} public treenode <t> getright () {return mrightchild ;} public void setright (treenode <t> right) {mrightchild = right;} public treenode <string> createtree () {treenode <string> H = new treenode <s Tring> ("H", null, null); treenode <string> G = new treenode <string> ("g", null, null ); treenode <string> F = new treenode <string> ("F", null, null); treenode <string> E = new treenode <string> ("e", null, null); treenode <string> d = new treenode <string> ("D", null, H); treenode <string> C = new treenode <string> ("C ", f, g); treenode <string> B = new treenode <string> ("B", d, e); treenode <string> A = new treenode <Str Ing> ("A", B, c); return a;} public void bfsiterate (treenode <string> root) {queue <treenode <string> queue = new queue list <treenode <string> (); queue. offer (Root); While (! Queue. isempty () {treenode <string> node = queue. poll (); system. out. print (node. getdata () + ""); If (node. getleft ()! = NULL) {queue. Offer (node. getleft () ;}if (node. getright ()! = NULL) {queue. offer (node. getright () ;}} public static void main (string [] ARGs) {treebfs = new treebfs (); treenode <string> root = treebfs. createtree (); treebfs. bfsiterate (Root );}}


[Data structure and algorithm] Binary Tree breadth Traversal

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.