R
Official website
The official website for the R programming language is:
Static typing
R is not statically typed; it is dynamically typed. In R, types are checked at runtime rather than during the compilation process. This means:
- You can assign any type of value to a variable without declaring its data type beforehand.
- Type errors will be caught when the code is executed rather than before the code is run.
For example, you can assign a numeric value to a variable one moment and then assign a character string to the same variable later without encountering a type declaration error. This flexibility is a characteristic of dynamically typed languages like R.
Example code
Below is an example of how you can implement a binary search tree (BST) search in R. In this example, we first define a simple BST structure using lists (each node is a list with a value, a left child, and a right child). Then we create a recursive function, findInBST, which searches for a given target value within the tree.
# Define a function to create a new BST node
newNode <- function(value, left = NULL, right = NULL) {
list(value = value, left = left, right = right)
}
# Recursive function to search for a value in the BST.
# Returns the node if found, or NULL if not found.
findInBST <- function(root, target) {
# if the tree is empty, return NULL
if (is.null(root)) {
return(NULL)
}
# if the current node's value matches the target, return the node
if (root$value == target) {
return(root)
}
# if target is smaller than node's value, search the left subtree
if (target < root$value) {
return(findInBST(root$left, target))
} else {
# if target is larger than node's value, search the right subtree
return(findInBST(root$right, target))
}
}
# Example usage:
# Manually constructing a binary search tree:
# 8
# / \
# 3 10
# / \ \
# 1 6 14
# / \
# 4 7
node1 <- newNode(1)
node4 <- newNode(4)
node7 <- newNode(7)
node6 <- newNode(6, left = node4, right = node7)
node3 <- newNode(3, left = node1, right = node6)
node14 <- newNode(14)
node10 <- newNode(10, right = node14)
root <- newNode(8, left = node3, right = node10)
# Searching for a value
targetValue <- 7
result <- findInBST(root, targetValue)
if (!is.null(result)) {
cat("Found:", result$value, "\n")
} else {
cat("Value", targetValue, "not found in the BST.\n")
}
Explanation
-
Node Structure:
We create a node as a list that holds the currentvalueand pointers (leftandright) to its children. The helper functionnewNodemakes node creation concise. - Search Function (
findInBST):- It first checks if the current node (
root) isNULL. If yes, it returnsNULLmeaning the target is not found. - If the node’s value equals the target, it returns the current node.
- If the target is less than the node’s value, the function recursively searches the left subtree.
- Otherwise, it recursively searches the right subtree.
- It first checks if the current node (
- Example Tree and Usage:
We create a simple BST manually and demonstrate how to search for a value, printing the result.
This code can be run directly in an R environment, and you can modify it further if needed.