Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to Spaces
Python
C
Java
class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def insert(node, data): if node is None: return TreeNode(data) else: if data < node.data: node.left = insert(node.left, data) elif data > node.data: node.right = insert(node.right, data) return node def inOrderTraversal(node): if node is None: return inOrderTraversal(node.left) print(node.data, end=", ") inOrderTraversal(node.right) root = TreeNode(13) node7 = TreeNode(7) node15 = TreeNode(15) node3 = TreeNode(3) node8 = TreeNode(8) node14 = TreeNode(14) node19 = TreeNode(19) node18 = TreeNode(18) root.left = node7 root.right = node15 node7.left = node3 node7.right = node8 node15.left = node14 node15.right = node19 node19.left = node18 # Inserting new value into the BST insert(root, 10) # Traverse inOrderTraversal(root) #Python
#include
#include
typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* createNode(int data) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } TreeNode* insert(TreeNode* node, int data) { if (node == NULL) { return createNode(data); } else { if (data < node->data) { node->left = insert(node->left, data); } else if (data > node->data) { node->right = insert(node->right, data); } } return node; } void inOrderTraversal(TreeNode* node) { if (node == NULL) { return; } inOrderTraversal(node->left); printf("%d, ", node->data); inOrderTraversal(node->right); } int main() { TreeNode* root = createNode(13); TreeNode* node7 = createNode(7); TreeNode* node15 = createNode(15); TreeNode* node3 = createNode(3); TreeNode* node8 = createNode(8); TreeNode* node14 = createNode(14); TreeNode* node19 = createNode(19); TreeNode* node18 = createNode(18); root->left = node7; root->right = node15; node7->left = node3; node7->right = node8; node15->left = node14; node15->right = node19; node19->left = node18; // Inserting new value into the BST insert(root, 10); // Traverse inOrderTraversal(root); return 0; } //C
public class Main { public static class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { this.data = data; left = null; right = null; } } public static TreeNode insert(TreeNode node, int data) { if (node == null) { return new TreeNode(data); } else { if (data < node.data) { node.left = insert(node.left, data); } else if (data > node.data) { node.right = insert(node.right, data); } } return node; } public static void inOrderTraversal(TreeNode node) { if (node == null) { return; } inOrderTraversal(node.left); System.out.print(node.data + ", "); inOrderTraversal(node.right); } public static void main(String[] args) { TreeNode root = new TreeNode(13); TreeNode node7 = new TreeNode(7); TreeNode node15 = new TreeNode(15); TreeNode node3 = new TreeNode(3); TreeNode node8 = new TreeNode(8); TreeNode node14 = new TreeNode(14); TreeNode node19 = new TreeNode(19); TreeNode node18 = new TreeNode(18); root.left = node7; root.right = node15; node7.left = node3; node7.right = node8; node15.left = node14; node15.right = node19; node19.left = node18; // Inserting new value into the BST insert(root, 10); // Traverse inOrderTraversal(root); } } // Java
Python result:
C result:
Java result:
3, 7, 8, 10, 13, 14, 15, 18, 19,
3, 7, 8, 10, 13, 14, 15, 18, 19,
3, 7, 8, 10, 13, 14, 15, 18, 19,