Binary tree Depth (recursive and non-recursive)---Java implementation __ Two fork Tree

Source: Internet
Author: User

Recursive Implementation

In order to find the depth of the tree, we can first find the depth of its left subtree and the depth of the right subtree, which can be implemented recursively, and the exit of recursion is the node is empty. The return value is 0;

non-recursive implementation

Using the hierarchical traversal algorithm, set the variable level to record the number of layers of the current node, and set the variable to point to the last node of the current layer, and let the level point to the +1 operation when the last node of the current layer is processed. The Set Variable cur records the number of nodes that the current layer has accessed, and when Cur equals last, the level of access ends.

Hierarchical traversal in the tree to find the width, output a layer of nodes, a layer of nodes, the number of nodes in each layer can take a similar algorithm.

The width of the tree: based on the depth algorithm of the tree, add a variable max with the highest number of layer nodes accessed by a record, compared to Max and last before accessing each layer, if Max is larger, Max does not change, if Max is smaller than the last, assign the last value to Max;

Code:

Import java.util.LinkedList;
	  public class Deep {//recursive implementation 1 public int finddeep (bitree root) {int Deep = 0;
		  if (root!= null) {int lchilddeep = Finddeep (root.left);
		  int rchilddeep = Finddeep (root.right); Deep = lchilddeep > Rchilddeep?
	  Lchilddeep + 1:rchilddeep + 1;
  return deep;
	  ///recursive implementation 2 public int findDeep1 (Bitree root) {if (root = null) {return 0;
	   else {int lchilddeep = FINDDEEP1 (root.left);//Find the depth of the left subtree int rchilddeep = FINDDEEP1 (root.left);/find the depth of the right subtree return lchilddeep > Rchilddeep?
  Lchilddeep + 1:rchilddeep + 1;//Saozi right subtree depth greater than the entire tree depth}//non-recursive implementation public int findDeep2 (Bitree root)
	
	 {if (root = null) return 0;
	 Bitree current = null;
	 linkedlist<bitree> queue = new linkedlist<bitree> ();
	 Queue.offer (root);
	 int cur,last;
	 int level = 0; while (!queue.isempty ()) {cur = 0;//the number of nodes that the layer has traversed last = queue.size ()////When the current layer is traversed, the elements in the queue are all elements of the next layer, and the length of the queue is thisThe number of nodes in the layer while (cur < last)//when the last node in this layer has not been traversed the loop {current = Queue.poll ();/out team an element cur++;
			 Queue the left and right nodes of the current node (if present) if (Current.left!= null) {Queue.offer (current.left);
			 } if (current.right!= null) {Queue.offer (current.right);
  } level++;//Level+1} return levels after each traversal;
	public static void Main (string[] args) {Bitree root = Bitree.buildtree ();
	Deep Deep = new Deep ();
	System.out.println (Deep.finddeep (root));
	System.out.println (DEEP.FINDDEEP1 (root));	
 System.out.println (DEEP.FINDDEEP2 (root)); }
}
The structure of a tree

A

/              \

B C

/    \                 \

D E G


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.