Example 1: java binary tree
public class BinaryTree {
private int key;
private BinaryTree left, right;
public BinaryTree(int key) {
this.key = key;
}
public BinaryTree(int key, BinaryTree left, BinaryTree right) {
this.key = key;
setLeft(left);
setRight(right);
}
public int getKey() {
return key;
}
public BinaryTree getLeft() {
return left;
}
public BinaryTree getRight() {
return right;
}
public boolean hasLeft() {
return left != null;
}
public boolean hasRight() {
return right != null;
}
public String toString(){
int h = height(this);
int i;
String result = "";
for (i=1; i<=h; i++) {
result += printGivenLevel(this, i);
}
return result;
}
public int size(){
return size(this);
}
public static int size(BinaryTree tree){
if(tree == null) return 0;
return 1 + size(tree.getLeft()) + size(tree.getRight());
}
public int height(){ return height(this);}
public static int height(BinaryTree tree){
if(tree == null) return 0;
int left = height(tree.getLeft());
int right = height(tree.getRight());
return Math.max(left, right) + 1;
}
public String printGivenLevel (BinaryTree root ,int level) {
if (root == null) return "";
String result = "";
if (level == 1) {
result += root.getKey() + " ";
return result;
}else if (level > 1) {
String left = printGivenLevel(root.left, level-1);
String right = printGivenLevel(root.right, level-1);
return left + right;
}else{
return "";
}
}
public void setLeft(BinaryTree left) {
this.left = left;
}
public void setRight(BinaryTree right) {
this.right = right;
}
}
Example 2: binary tree in data structure
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
if(data < parent->data) {
current = current->leftChild;
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}
else {
current = current->rightChild;
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}
Example 3: write a program to insert a node in bst in c
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
void print_preorder(node * tree)
{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}
}
void print_inorder(node * tree)
{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}
void print_postorder(node * tree)
{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}
void deltree(node * tree)
{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}
node* search(node ** tree, int val)
{
if(!(*tree))
{
return NULL;
}
if(val < (*tree)->data)
{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}
void main()
{
node *root;
node *tmp;
root = NULL;
insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);
printf("Pre Order Display\n");
print_preorder(root);
printf("In Order Display\n");
print_inorder(root);
printf("Post Order Display\n");
print_postorder(root);
tmp = search(&root, 4);
if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}
deltree(root);
}
Example 4: binary tree in c implementation
11 void insert(node ** tree, int val) {
12 node *temp = NULL;
13 if(!(*tree)) {
14 temp = (node *)malloc(sizeof(node));
15 temp->left = temp->right = NULL;
16 temp->data = val;
17 *tree = temp;
18 return;
19 }
20
21 if(val < (*tree)->data) {
22 insert(&(*tree)->left, val);
23 } else if(val > (*tree)->data) {
24 insert(&(*tree)->right, val);
25 }
26 }