Javatpoint Logo
Javatpoint Logo

Display Leaf nodes from Preorder of a BST in Java

An input array is given to us. That input array is the preorder traversal of a Binary Search Tree (BST). The task is to detect and print the leaf nodes of the Binary Search Tree. A leaf node is a node of a tree that has no child.

Example 1:

Input

int preorderArr[] = {25, 20, 10, 5, 12, 22, 36, 30, 28, 40, 38, 48}

Output: {5, 12, 22, 28, 38, 48}

Explanation: The nodes of the values 5, 12, 22, 28, 38, and 48 are the leaf nodes.

Example 2:

Input

int preorderArr[] = {891, 326, 291, 531, 966}

Output: {291, 531, 966}

Explanation: The nodes of the values 291, 531, and 966 are the leaf nodes.

Simple Approach

The simple or the naïve approach is to get one more traversal. We know that if we get any of the two traversals from the three (inorder, preorder, and postorder), we can get the binary tree, and from that, we can easily get the leaf nodes. Preorder is already given to us. So, our task is to find either postorder traversal or the inorder traversal. We know that if we do the inorder traversal of a binary search tree, we get the value of nodes in the sorted order (property of a BST).

Thus, all we need to do is to create an array called inorderArr[]. Copy all of the elements of the preorderArr in it and sort the array inorderArr[]. Now, we have got the two traversals (one is preorder, and the other is inorder). All we need to do is to construct a binary search tree from these two traversals and then find out the leaf nodes in it and display it. The following program makes the concept clearer.

FileName: DisplayLeafBST.java

Output:

For a Binary Search Tree that has the preorder traversal as: 
25 20 10 5 12 22 36 30 28 40 38 48 

The leaf nodes are: 
5 12 22 28 38 48 

For a Binary Search Tree that has the preorder traversal as: 
891 326 291 531 966 

The leaf nodes are: 
291 531 966

Complexity Analysis: The method createBST() consumes O(n2) time in order to construct the Binary Search Tree. Other methods are also taking time to produce their result. However, the maximum time is consumed by the method createBST(). Thus, the overall time complexity of the program is O(n2). Also, the program uses an auxiliary array for storing the leaf nodes as well as the inorder traversal. Thus, the space complexity of the program is O(n), where n is the total number of elements present in the array preorderArr[].

Optimization: If we observe, we find that, there is no need for method createBST() in the above program. Without creating the Binary Search Tree, we can easily find out the leaf nodes. Also, searching an element in the inorderArr[] does not require a linear search. It is because the inorderArr[] is sorted. Therefore, we can also binary search. It will reduce the time complexity from O(n) to O(log(n)). Observe the following program.

FileName: DisplayLeafBST1.java

Output:

For a Binary Search Tree that has the preorder traversal as: 
25 20 10 5 12 22 36 30 28 40 38 48 

The leaf nodes are: 
5 12 22 28 38 48 

For a Binary Search Tree that has the preorder traversal as: 
891 326 291 531 966 

The leaf nodes are: 
291 531 966

Complexity Analysis: The method leafNodesRecursion() takes O(n) time. In each recursive call, we are invoking the method binrySearch() that takes O(log(n)) time. Thus, the overall time complexity of the program is O(n x log(n)), where n is the total number of elements present in the array preorderArr[]. The space complexity of the program remains the same as the previous program.

Let's do some more optimization to reduce the time complexity.

Approach: Using Stack

Using the stack and the property of the Binary Search Tree, the time complexity of the program can be reduced. First of all, we need to traverse the preorderArr[] with the help of two pointers, m and n. In the beginning, m = 0, and n = 1. When preorderArr[m] > preorderArr[n], then we can conclude that preorderArr[n] is the left part of preorderArr[m]. We know that preorder follows the pattern root -> left -> right. Therefore, the element preorderArr[m] is pushed into the stack.

For those nodes that are not implementing BST the rule, it is required to remove the elements from the stack until preorderArr[m] is greater than the top element of the stack and break the loop when it does not and then display the corresponding mth value.

FileName: DisplayLeafBST1.java

Output:

For a Binary Search Tree that has the preorder traversal as: 
25 20 10 5 12 22 36 30 28 40 38 48 

The leaf nodes are: 
5 12 22 28 38 48


For a Binary Search Tree that has the preorder traversal as: 
891 326 291 531 966 

The leaf nodes are: 
291 531 966

Complexity Analysis: The maximum time a node is traversed is only two times. Therefore, the time complexity of the program is O(n). The program uses a stack for storing elements. Therefore, the space complexity remains the same as the previous programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA