Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to perform an iterative depth first search through a binary tree, in Java?

/*
	This is an implementation that collects the 
	values of the nodes of a binary tree by performing 
	an iterative depth first search through the tree.

	Let n be the number of binary tree nodes
	Time complexity: O(n) 
	Space complexity: O(n)
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;

public class DepthFirstSearchIter {
	private BTNode BTRoot;

	public DepthFirstSearchIter() {
		/*
		 * Create tree below:
		 * 1
		 * 
		 * 2
		 * / 
		 * 3 4
		 */
		BTRoot = new BTNode(1, null, null);
		BTNode rootRight = new BTNode(2, null, null);
		BTRoot.right = rootRight;
		BTNode rootRightLeft = new BTNode(3, null, null);
		BTNode rootRightRight = new BTNode(4, null, null);
		rootRight.left = rootRightLeft;
		rootRight.right = rootRightRight;
	}

	public static void main(String[] args) {
		DepthFirstSearchIter application = new DepthFirstSearchIter();
		List<Integer> values = application.dfsTraversalIter();
		System.out.println(values); // [1, 2, 3, 4]
	}

	// Perform depth first traversal through the tree.
	public List<Integer> dfsTraversalIter() {
		List<Integer> list = new ArrayList<>();
		populateList(BTRoot, list);
		return list;
	}

	// Helper method to populate list by performing
	// iterative depth first search through the tree.
	private void populateList(BTNode root, List<Integer> list) {
		if (root == null) {
			return;
		}
		Stack<BTNode> stack = new Stack<>();
		BTNode current;
		stack.push(root);
		while (!stack.isEmpty()) {
			current = stack.pop();
			list.add(current.val);

			if (current.right != null) {
				stack.push(current.right);
			}

			if (current.left != null) {
				stack.push(current.left);
			}
		}
	}
	// Class representing a binary tree node
	// with pointers to value, left, and right nodes
	private class BTNode {
		int val;
		BTNode left;
		BTNode right;

		public BTNode(int val, BTNode left, BTNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: how to compare 3 numbers in java 
Java :: restart application programmatically android 
Java :: array ausgeben java 
Java :: java stop executing my program 
Java :: circular list java 
Java :: module-info.java module not found 
Java :: java multiplication table nested loop 
Java :: program to print each word of a string 
Java :: java switch class instanceof 
Java :: how to convert an object into string with different fields in java 
Java :: add value with n variable with Arraylist in java 
Java :: how to extract decimal vqalue from float in android studio 
Java :: java button 
Java :: hide actionbar android 
Java :: input java 
Java :: android save int 
Java :: file to image javafx 
Java :: find duplicate value in array using java 
Java :: how to get the last element of array in java 
Java :: how to not open key board on start 
Java :: hibernate create query count return 
Java :: spring security enable global cors 
Java :: border in android 
Java :: get first character of string java 
Java :: check character is alphanumeric java 
Java :: how to check my java heap space size 
Java :: create new java swing button 
Java :: java or symbol 
Java :: thymeleaf string not equal 
Java :: export java home 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =