Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
binary_tree_array = ['R', 'A', 'B', 'C', 'D', 'E', 'F', None, None, None, None, None, None, 'G'] def left_child_index(index): return 2 * index + 1 def right_child_index(index): return 2 * index + 2 def get_data(index): if 0 <= index < len(binary_tree_array): return binary_tree_array[index] return None right_child = right_child_index(0) left_child_of_right_child = left_child_index(right_child) data = get_data(left_child_of_right_child) print("root.right.left.data:", data) #Python
#include
#define ARRAY_SIZE 15 // Define the size of the array char* get_data(char binaryTreeArray[], int index); int left_child_index(int index); int right_child_index(int index); int main() { char binaryTreeArray[ARRAY_SIZE] = {'R', 'A', 'B', 'C', 'D', 'E', 'F', '\0', '\0', '\0', '\0', '\0', '\0', 'G', '\0'}; int rightChild = right_child_index(0); int leftChildOfRightChild = left_child_index(rightChild); char* data = get_data(binaryTreeArray, leftChildOfRightChild); if (data != NULL) { printf("root.right.left.data: %c\n", *data); } else { printf("No data found.\n"); } return 0; } char* get_data(char binaryTreeArray[], int index) { if (index >= 0 && index < ARRAY_SIZE) { return &binaryTreeArray[index]; } return NULL; } int left_child_index(int index) { return 2 * index + 1; } int right_child_index(int index) { return 2 * index + 2; } //C
public class Main { public static void main(String[] args) { Character[] binaryTreeArray = {'R', 'A', 'B', 'C', 'D', 'E', 'F', null, null, null, null, null, null, 'G'}; int rightChild = rightChildIndex(0); int leftChildOfRightChild = leftChildIndex(rightChild); Character data = getData(binaryTreeArray, leftChildOfRightChild); System.out.println("root.right.left.data: " + data); } public static int leftChildIndex(int index) { return 2 * index + 1; } public static int rightChildIndex(int index) { return 2 * index + 2; } public static Character getData(Character[] binaryTreeArray, int index) { if (index >= 0 && index < binaryTreeArray.length) { return binaryTreeArray[index]; } return null; } } //Java
Python result:
C result:
Java result:
root.right.left.data: E
root.right.left.data: E
root.right.left.data: E