Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

iterative inorder traversal

/*
	This performs iterative in-order traversal 
	through a binary search tree.
	In a binary search tree, for ever node v:
	- Elements in left subtree rooted at v 
	are less than element stored at v.
	- Elements in right subtree rooted at v
	are greater than or equal to one at v. 

	Let n be the number of nodes in the 
	binary search tree.
	Time complexity: O(n) 
	Space complexity: O(1)
*/
import java.util.function.Consumer;

public class IterativeInOrderTraversal {
	private BTNode BTRoot;

	public IterativeInOrderTraversal() {
		/*
		 * Create tree below:
		 * * 4
		 * / 
		 * 2 7
		 * * / 
		 * * 6 8
		 */
		BTRoot = new BTNode(4, null, null, null);
		BTNode rootLeft = new BTNode(2, null, null, BTRoot);
		BTRoot.left = rootLeft;
		BTNode rootRight = new BTNode(7, null, null, BTRoot);
		BTRoot.right = rootRight;
		BTNode rootRightLeft = new BTNode(6, null, null, rootRight);
		BTNode rootRightRight = new BTNode(8, null, null, rootRight);
		rootRight.left = rootRightLeft;
		rootRight.right = rootRightRight;
	}

	public static void main(String[] args) {
		IterativeInOrderTraversal application = new IterativeInOrderTraversal();
		// The in-order traversal yields a sorted list
		application.iterativeInOrderTraversal((node) -> {
			System.out.print(node.val + " ");
		}); // 2 4 6 7 8

	}

	// Use three pointers to traverse the tree iteratively
	public void iterativeInOrderTraversal(
			Consumer<BTNode> callback) {
		if (BTRoot == null) {
			return;
		}

		BTNode currentNode = BTRoot, previousNode = null;
		BTNode nextNode;

		while (currentNode != null) {
			if (previousNode == null || previousNode == currentNode.parent) {
				if (currentNode.left != null) {
					nextNode = currentNode.left;
				} else {
					callback.accept(currentNode);
					nextNode = (currentNode.right != null) ? currentNode.right : currentNode.parent;
				}

			} else if (previousNode == currentNode.left) {
				callback.accept(currentNode);
				nextNode = (currentNode.right != null) ? currentNode.right : currentNode.parent;
			} else {
				nextNode = currentNode.parent;
			}

			previousNode = currentNode;
			currentNode = nextNode;
		}
	}

	// Class representing a binary tree node
	// with pointers to value, left, right, and parent nodes
	private class BTNode {
		int val;
		BTNode left;
		BTNode right;
		BTNode parent;

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

inorder traversal - iterative method

void inOrder(struct Tree *root) {
    stack<Tree *> s;
    Tree *curr = root;
    while (curr != NULL || s.empty() == false)
    {
        while (curr !=  NULL)
        {
            s.push(curr);
            curr = curr->left;
        }
        cout << s.top()->data << " ";
        curr = s.top()->right;
        s.pop();
    } 
}
Comment

inorder traversal - iterative method

void inOrder(struct Tree *root) {
    stack<Tree *> s;
    Tree *curr = root;
    while (curr != NULL || s.empty() == false)
    {
        while (curr !=  NULL)
        {
            s.push(curr);
            curr = curr->left;
        }
        cout << s.top()->data << " ";
        curr = s.top()->right;
        s.pop();
    } 
}
Comment

PREVIOUS NEXT
Code Example
Java :: java integer division to float 
Java :: how to develop web apps using java 
Java :: how to generate random large string in java 
Java :: export java path in ubuntu 
Java :: how to copy an object in java 
Java :: javafx get button id 
Java :: string a int java 
Java :: arc() method from the processing library 
Java :: how to code java??????????? 
Java :: Send keybord key P in selenium java 
Java :: java list tostring 
Java :: show all spring boot beans 
Java :: csv compare 
Java :: Read json url in android - NAYCode.com 
Java :: java jre 
Java :: kotlin edittext default value 
Java :: if ternaire java 
Java :: bool in java 
Java :: java math random 
Java :: java get parent class 
Java :: java user home directory macos 
Java :: how to print each element of an arraylist on a new line in java 
Java :: java check prime number 
Java :: java insertion sort 
Java :: java stream findany 
Java :: java binary exponentiation 
Java :: Java Enum compareTo() 
Java :: java comment 
Java :: singleton 
Java :: java literals 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =