CSE Helper
Powered By Dream Dragon Creation
Binary tree – C++ Program source code
-
#include <iostream>
-
#include <stdio h>
-
using namespace std;
-
-
class Node{
-
-
public:
-
int Value; // store the value of the Node
-
int index; // pointer to storing index of the tree
-
Node* leftChild; // pointer to the left Node
-
Node* rightChild; // pointer to the right Child
-
};
-
class BinaryTree{
-
public:
-
Node* root; // pointer for root of the tree
-
int NumElements; // number of elements in the tree
-
-
BinaryTree(){
-
root = NULL;
-
NumElements = 0;
-
}
-
int add_left_child(Node* parent, int input);
-
int add_right_child(Node* parent, int input);
-
void traverse_inorder(Node* root);
-
void traverse_preorder(Node* root);
-
void traverse_postorder(Node* root);
-
Node* create_Tree(int inputArray[], int index, int arraySize);
-
Node* expand_Tree(Node* root, int elements[] , int ArraySize);
-
void get_inputOrder(Node* root, int inputs[]);
-
};
-
-
Node* BinaryTree::create_Tree(int inputArray[],int index, int ArraySize){
-
this->NumElements = ArraySize; // store the number of elements in the array
-
Node* Root = new Node();
-
Root->Value = inputArray[index];
-
Root->index = index;
-
//if left child node excised the index of array return null, thats leaf
-
if ((2 * index + 1) >= ArraySize){
-
return Root;
-
}
-
//adding left child node
-
if (add_left_child(Root, inputArray[2*index + 1])){
-
Root->leftChild = create_Tree(inputArray, 2*index+1, ArraySize);
-
}
-
//if right child node excised the index of array return null, thats leaf
-
if ((2 * index + 2) >= ArraySize){
-
return Root;
-
}
-
// adding right child node
-
if (add_right_child(Root, inputArray[2*index+2])){
-
Root->rightChild = create_Tree(inputArray, (2 * index + 2),ArraySize);
-
}
-
return Root; // return the sub tree node pointer
-
}
-
-
int BinaryTree::add_left_child(Node* parent, int inputValue){
-
Node*newLeaf;
-
if (parent->leftChild == NULL){
-
newLeaf = new Node();
-
parent->leftChild = newLeaf;
-
newLeaf->Value = inputValue;
-
return 1;
-
}
-
else{
-
return -1;
-
}
-
}
-
-
int BinaryTree::add_right_child(Node* parent, int inputValue){
-
Node* newLeaf;
-
if (parent->rightChild == NULL){
-
newLeaf = new Node();
-
parent->rightChild = newLeaf;
-
newLeaf->Value = inputValue;
-
return 1;
-
}
-
else{
-
return -1;
-
}
-
}
-
-
// pre- order traversal : *Node *Left Tree *Right Tree
-
void BinaryTree::traverse_preorder(Node* root){
-
if (root)
-
{
-
cout << root->Value << " ";
-
traverse_preorder(root->leftChild);
-
traverse_preorder(root->rightChild);
-
}
-
}
-
-
//post order Travers: Left Tree *Right sub Tree * Node
-
void BinaryTree::traverse_postorder(Node* root){
-
if (root)
-
{
-
traverse_postorder(root->leftChild);
-
traverse_postorder(root->rightChild);
-
cout << root->Value << " ";
-
}
-
}
-
-
// In-order Travesal : *Left Tree *Node *Right Tree
-
void BinaryTree::traverse_inorder(Node* root){
-
if (root ){
-
traverse_inorder(root->leftChild);
-
cout << root->Value << " ";
-
traverse_inorder(root->rightChild);
-
}
-
}
-
-
// to expand methord
-
void BinaryTree::get_inputOrder(Node* root , int inputs[]){
-
if (root)
-
{
-
inputs[root->index] = root->Value;
-
get_inputOrder(root->leftChild, inputs);
-
get_inputOrder(root->rightChild, inputs);
-
}
-
}
-
-
Node* BinaryTree::expand_Tree(Node* root, int elements[] ,int ArraySize){
-
int allElements = this->NumElements + ArraySize;
-
int NewArray[12]; // int allElement should be the array size but in V S it consider as not constant value therefore i had to set it manually
-
this->get_inputOrder(root, NewArray); // get all the elements in the Tree and put in to array
-
int j = 0;
-
for (int i = 8; i < allElements; i++){
-
NewArray[i] = elements[j];
-
j++;
-
}
-
return create_Tree(NewArray,0,allElements) ; // all elemtment put in to the one new aray and create new tree and return the root
-
}
-
-
}
-
return add_value(root->leftChild, newvalue);
-
}
-
else{
-
if (root->rightChild == NULL){
-
Node* newNode = new Node;
-
newNode->leftChild = NULL;
-
newNode->rightChild = NULL;
-
newNode->Value = newvalue;
-
root->rightChild = newNode;
-
return 1;
-
}
-
return add_value(root->rightChild, newvalue);
-
}
-
}
-
-
int main(){
-
BinaryTree *binaryTree = new BinaryTree;
-
cout <<"Create tree[1,45,23,33,25,53,54,11]\n"<< endl;
-
int array[8] = { 1, 45, 23, 33, 25, 53, 54,11 };
-
Node* Root = binaryTree->create_Tree(array, 0,8);
-
cout << "InOrderTraversal of Tree : " << endl;
-
binaryTree->traverse_inorder(Root);
-
cout << "" << endl;
-
cout << "pre- order traversal : " << endl;
-
binaryTree->traverse_preorder(Root);
-
cout << "" << endl;
-
cout << "post order Traversal :" << endl;
-
binaryTree->traverse_postorder(Root);
-
cout << "" << endl;
-
cout << "\n \n" << endl;
-
cout << "Expand the same tree [3,22,7,64]\n" << endl;
-
int expand[] = { 3, 22, 7, 64 };
-
Root = binaryTree->expand_Tree(Root, expand, 4);
-
cout << "InOrderTraversal of Tree : " << endl;
-
binaryTree->traverse_inorder(Root);
-
cout << "" << endl;
-
cout << "pre- order traversal :" << endl;
-
binaryTree->traverse_preorder(Root);
-
cout << "" << endl;
-
cout << "post order Traversal :" << endl;
-
binaryTree->traverse_postorder(Root);
-
cout << "" << endl;
-
cout << "\n \n" << endl;
-
return 0;
-
}
Binary tree - java Program source code
-
package Binary_tree;
-
-
/**
-
*
-
* @author CSE Helper
-
*/
-
-
class Node{
-
public Node leftChild;
-
public Node rightChild;
-
public int value;
-
public int index;
-
}
-
public class Binary_Tree {
-
private Node root;
-
private int numElements;
-
private int elements[];
-
-
public Binary_Tree() {
-
root = null;
-
numElements = 0;
-
}
-
boolean add_left_child(Node parent, int input){
-
Node newLeaf;
-
if (parent leftChild == null){
-
newLeaf = new Node();
-
parent leftChild = newLeaf;
-
newLeaf value = input;
-
return true;
-
}else{
-
return false;
-
}
-
}
-
boolean add_right_child(Node parent , int input){
-
Node newLeaf;
-
if (parent rightChild == null){
-
newLeaf = new Node();
-
parent rightChild = newLeaf;
-
newLeaf value = input;
-
return true;
-
}else{
-
return false;
-
}
-
}
-
Node create_Tree(int inputArray[], int index, int arraySize){
-
numElements = arraySize; // store the number of elements in the array
-
elements = inputArray;
-
Node Root = new Node();
-
Root value = inputArray[index];
-
Root index = index;
-
//if left child node excised the index of array return null, thats leaf
-
if ((2 * index + 1) >= arraySize){
-
return Root;
-
}
-
//adding left child node
-
if (add_left_child(Root, inputArray[2 * index + 1])){
-
Root leftChild = create_Tree(inputArray, 2 * index + 1 , arraySize);
-
}
-
//if right child node excised the index of array return null, thats leaf
-
if ((2 * index + 2) >= arraySize){
-
return Root;
-
}
-
// adding right child node
-
if (add_right_child(Root, inputArray[2 * index + 2])){
-
Root rightChild = create_Tree(inputArray, (2 * index + 2),arraySize);
-
}
-
return Root; // return the sub tree node pointer
-
}
-
void traverse_inorder(Node root){
-
if (root != null ){
-
traverse_inorder(root leftChild);
-
System out print( root value + " ");
-
traverse_inorder(root rightChild);
-
}
-
}
-
void traverse_preorder(Node root){
-
if (root != null)
-
{ System out print( root value + " ");
-
traverse_preorder(root leftChild);
-
traverse_preorder(root rightChild);
-
}
-
}
-
void traverse_postorder(Node root){
-
if (root != null)
-
{ traverse_postorder(root leftChild);
-
traverse_postorder(root rightChild);
-
System out print( root value + " ");
-
}
-
}
-
Node expand_Tree(Node root, int elements[] , int ArraySize){
-
int allElements = numElements + ArraySize;
-
// int allElement should be the array size but in V S it consider as not constant value therefore i had to set it manually
-
int NewArray[] = new int[allElements];
-
// get all the elements in the Tree and put in to array
-
get_inputOrder(root, this elements);
-
int j = 0;
-
for (int i = 8; i < allElements; i++){
-
NewArray[i] = elements[j];
-
j++;
-
}
-
//add previous tree values to new array
-
for(int i = 0 ; i< numElements ; i++){
-
NewArray[i] = this elements[i];
-
}
-
//create new expanded tree
-
return create_Tree(NewArray,0,allElements) ; // all elemtment put in to the one new aray and create new tree and return the root
-
}
-
void get_inputOrder( Node root , int inputs[]){
-
if (root != null)
-
{
-
inputs[root index] = root value;
-
get_inputOrder(root leftChild, inputs);
-
get_inputOrder(root rightChild, inputs);
-
}
-
}
-
-
public static void main(String[] args){
-
Binary_Tree binaryTree = new Binary_Tree();
-
System out println( "Create tree [1,45,23,33,25,53,54,11] \n");
-
int array[] = { 1, 45, 23, 33, 25, 53, 54,11 };
-
Node Root = binaryTree create_Tree(array, 0,8);
-
System out println( "InOrderTraversal of Tree : " );
-
binaryTree traverse_inorder(Root);
-
System out println("");
-
System out println("pre- order traversal : " );
-
binaryTree traverse_preorder(Root);
-
System out println("");
-
System out println("post order Traversal :");
-
binaryTree traverse_postorder(Root);
-
System out println( "");
-
System out println("\n \n" );
-
System out println("Expand the same tree[3,22,7,64]\n" );
-
int expand[] = { 3, 22, 7, 64 };
-
Root = binaryTree expand_Tree(Root, expand, 4);
-
System out println("InOrderTraversal of Tree : " );
-
binaryTree traverse_inorder(Root);
-
System out println("");
-
System out println("pre- order traversal :" );
-
binaryTree traverse_preorder(Root);
-
System out println("");
-
System out println( "post order Traversal :" );
-
binaryTree traverse_postorder(Root);
-
System out println( "");
-
System out println("\n \n");
-
}
-
}