Java Two fork tree recursive traversal algorithm

Source: Internet
Author: User

  1. //recursive middle sequence traversal
  2. public void Inorder () {
  3. System.out.print ("binarytree recursive middle Sequence Traversal:");
  4. Inordertraverserecursion (root);
  5. System.out.println ();
  6. }
  7. //Hierarchical Traversal
  8. public void Layerorder () {
  9. System.out.print ("Binarytree hierarchy Traversal:");
  10. linkedlist<node<integer>> queue = new linkedlist<node<integer>> ();
  11. Queue.addlast (root);
  12. node<integer> current = null;
  13. While (!queue.isempty ()) {
  14. Current = Queue.removefirst ();
  15. if (current.getleftchild () = null)
  16. Queue.addlast (Current.getleftchild ());
  17. if (current.getrightchild () = null)
  18. Queue.addlast (Current.getrightchild ());
  19. System.out.print (Current.getvalue ());
  20. }
  21. System.out.println ();
  22. }
  23. //Get two fork tree depth
  24. public int getdepth () {
  25. return getdepthrecursion (root);
  26. }
  27. private int getdepthrecursion (node<integer> Node) {
  28. if (node = = null)
  29. return 0;
  30. int llen = getdepthrecursion (Node.getleftchild ());
  31. int rlen = getdepthrecursion (Node.getrightchild ());
  32. int maxlen = Math.max (Llen, Rlen);
  33. return maxlen + 1;
  34. }
  35. //Recursive First order traversal
  36. public Void Preorder () {
  37. System.out.print ("Binarytree recursive First Order traversal:");
  38. Preordertraverserecursion (root);
  39. System.out.println ();
  40. }

Java Two fork tree recursive traversal algorithm

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.