# Print all odd nodes of Binary Search Tree in swift

Swift program for Print all odd nodes of Binary Search Tree. Here more solutions.

``````import Foundation
// Swift 4 program for
// Detect and print all leaf nodes in binary search tree

// Binary Search Tree Node
class TreeNode
{
// Data value
var data: Int;
// Indicates left and right subtree
var left: TreeNode? ;
var right: TreeNode? ;
init(_ data: Int)
{
self.data = data;
self.left = nil;
self.right = nil;
}
}
class BinarySearchTree
{
var root: TreeNode? ;
init()
{
self.root = nil;
}
// Insert a node in BST
{
// Create new node of binary search tree
let node: TreeNode? = TreeNode(value);
if (node  != nil)
{
if (self.root == nil)
{
// When adds a first node in binary tree
self.root = node;
}
else
{
var find: TreeNode? = self.root;
// Add new node to proper position
while (find  != nil)
{
if (find!.data >= value)
{
if (find!.left == nil)
{
find!.left = node;
return;
}
else
{
// Visit left sub-tree
find = find!.left;
}
}
else
{
if (find!.right == nil)
{
find!.right = node;
return;
}
else
{
// Visit right sub-tree
find = find!.right;
}
}
}
}
}
else
{
print("\nMemory Overflow");
}
}
// Display the value of odd nodes in binary search tree
func printOddNode(_ node: TreeNode? )
{
if (node  != nil)
{
// Visit to left child
self.printOddNode(node!.left);
if (node!.data % 2  != 0)
{
// When node contain odd value
print(node!.data, terminator: "  ");
}
// Visit to right child
self.printOddNode(node!.right);
}
}
static func main()
{
let tree: BinarySearchTree = BinarySearchTree();
// Add nodes in binary search tree
/*
5
/ \
/   \
/     \
3       9
/ \     / \
1   4   8   11
/ \     /      \
-3   2   6        12

*/
// Test
tree.printOddNode(tree.root);
}
}
BinarySearchTree.main();``````

Output

``-3  1  3  5  9  11``

## 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.