Skip to main content

Detect next node at same level of a key in binary tree in swift

Next node of same level of tree

Swift program for Detect next node at same level of a key in binary tree. Here more solutions.

import Foundation
/* 
  Swift 4 program for
  Find the next node of the key at the same 
  level in the binary tree
*/
// Binary Tree node
class TreeNode
{
	var data: Int;
	var left: TreeNode? ;
	var right: TreeNode? ;
	init(_ data: Int)
	{
		// Set node value
		self.data = data;
		self.left = nil;
		self.right = nil;
	}
}
class BinaryTree
{
	var root: TreeNode? ;
	var status: Int;
	var result: TreeNode? ;
	init()
	{
		self.root = nil;
		self.status = -1;
		self.result = nil;
	}
	// Method which are find next node of same level
	func findNextNode(_ temp: TreeNode? , 
                      _ level : Int, _ node: Int)
	{
		if (temp  != nil)
		{
			if (self.status == -1 && temp!.data == node)
			{
				// When found node value
				self.status = level;
			}
			else if (self.status == level && self.result == nil)
			{
				// Next node of same level
				self.result = temp;
			}
			// Visit to left subtree
			self.findNextNode(temp!.left, level + 1, node);
			// Visit to right subtree
			self.findNextNode(temp!.right, level + 1, node);
		}
	}
	// This are handling the request of to finding next
	// node in same level by given node
	func nextNode(_ node: Int)
	{
		// Reset the result indicator
		self.status = -1;
		self.result = nil;
		// Find next node
		self.findNextNode(self.root, 0, node);
		print("\n Node : "
			+ String(node));
		print(" Result : ", terminator: "");
		// Test Case
		if (self.status == -1)
		{
			// Case A
			// When node values are not exist
			print("Not found");
		}
		else if (self.result == nil)
		{
			// Case B
			// When next node is not found
			print("None");
		}
		else
		{
			// Case C
			// When next node are exist
			print(self.result!.data);
		}
	}
	static func main()
	{
		// Create new tree
		let tree: BinaryTree = BinaryTree();
		/*
		   Make A Binary Tree
		  ---------------------
		        1
		       / \ 
		      /   \ 
		     /     \
		    2       4
		   /       / \
		  /       /   \
		  3      6     5
		   \      \   /
		    7      8 11
		              
		*/
		// Add node in Binary Tree
		tree.root = TreeNode(1);
		tree.root!.left = TreeNode(2);
		tree.root!.left!.left = TreeNode(3);
		tree.root!.left!.left!.right = TreeNode(7);
		tree.root!.right = TreeNode(4);
		tree.root!.right!.right = TreeNode(5);
		tree.root!.right!.right!.left = TreeNode(11);
		tree.root!.right!.left = TreeNode(6);
		tree.root!.right!.left!.right = TreeNode(8);
		// Testing
		tree.nextNode(3);
		tree.nextNode(7);
		tree.nextNode(6);
		tree.nextNode(5);
		tree.nextNode(8);
		tree.nextNode(1);
	}
}
BinaryTree.main();

Output

 Node : 3
 Result : 6

 Node : 7
 Result : 8

 Node : 6
 Result : 5

 Node : 5
 Result : None

 Node : 8
 Result : 11

 Node : 1
 Result : None




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment