ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Objective-C Binary Search Tree Node Insertion

Objective-C

Goal -- WPM

Ready
Exercise Algorithm Area
1#import <Foundation/Foundation.h>
2
3// Definition for a binary tree node.
4@interface TreeNode : NSObject
5@property (nonatomic, assign) NSInteger val;
6@property (nonatomic, strong) TreeNode *left;
7@property (nonatomic, strong) TreeNode *right;
8
9- (instancetype)initWithValue:(NSInteger)val;
10@end
11
12@implementation TreeNode
13
14- (instancetype)initWithValue:(NSInteger)val {
15self = [super init];
16if (self) {
17_val = val;
18_left = nil;
19_right = nil;
20}
21return self;
22}
23
24@end
25
26// Function to insert a node into a Binary Search Tree
27TreeNode* insertIntoBST(TreeNode* root, NSInteger val) {
28// Base case: If the tree is empty, create a new node and return it.
29if (root == nil) {
30return [[TreeNode alloc] initWithValue:val];
31}
32
33// If the value to insert is less than the current node's value,
34// recursively insert into the left subtree.
35if (val < root.val) {
36root.left = insertIntoBST(root.left, val);
37}
38// If the value to insert is greater than the current node's value,
39// recursively insert into the right subtree.
40else if (val > root.val) {
41root.right = insertIntoBST(root.right, val);
42}
43// If the value is equal, we can choose to ignore it or handle duplicates.
44// Here, we ignore duplicates to maintain BST properties (no duplicates).
45// If duplicates were allowed, we might insert into the right subtree.
46
47// Return the (possibly updated) root node.
48return root;
49}
50
51// Helper function to print the BST in-order (for verification)
52void printInOrder(TreeNode* node) {
53if (node == nil) {
54return;
55}
56printInOrder(node.left);
57printf("%ld ", (long)node.val);
58printInOrder(node.right);
59}
60
61int main(int argc, const char * argv[]) {
62@autoreleasepool {
63TreeNode *root = nil;
64root = insertIntoBST(root, 50);
65root = insertIntoBST(root, 30);
66root = insertIntoBST(root, 20);
67root = insertIntoBST(root, 40);
68root = insertIntoBST(root, 70);
69root = insertIntoBST(root, 60);
70root = insertIntoBST(root, 80);
71root = insertIntoBST(root, 30); // Duplicate value, should be ignored
72
73printf("In-order traversal of the constructed BST: ");
74printInOrder(root);
75printf("\n");
76}
77return 0;
78}
Algorithm description viewbox

Objective-C Binary Search Tree Node Insertion

Algorithm description:

This Objective-C code demonstrates how to insert a new node into a Binary Search Tree (BST). A BST is a data structure where each node has at most two children, and the left child's value is less than the parent's, while the right child's value is greater. This insertion logic is crucial for maintaining the BST property and is fundamental for efficient searching, insertion, and deletion operations in many applications, such as databases and symbol tables.

Algorithm explanation:

The `insertIntoBST` function recursively inserts a new node with a given value into a Binary Search Tree. The base case for the recursion is when the `root` is `nil`, indicating an empty spot where the new node can be created and returned. If the `root` is not `nil`, the function compares the `val` to be inserted with the `root.val`. If `val` is less than `root.val`, the function recursively calls itself on the left subtree (`root.left`). If `val` is greater, it calls itself on the right subtree (`root.right`). Duplicate values are handled by simply returning the current `root` without insertion, ensuring that each value appears only once in the tree. The function always returns the `root` of the (potentially modified) subtree. The time complexity for insertion is O(h), where h is the height of the tree. In the worst case (a skewed tree), h can be O(n), making it O(n). In a balanced tree, h is O(log n), resulting in O(log n) complexity. The space complexity is O(h) due to the recursion stack, which is O(n) in the worst case and O(log n) in the average case for a balanced tree.

Pseudocode:

function insertIntoBST(root, val):
  if root is nil:
    return new TreeNode with value val

  if val < root.value:
    root.left = insertIntoBST(root.left, val)
  else if val > root.value:
    root.right = insertIntoBST(root.right, val)
  // else (val == root.value), do nothing (ignore duplicate)

  return root