loading...
loading...

Thursday, June 9, 2016

Contoh Coding Binary Tree C++



BINARY TREE


Hallo teman teman

kali ini saya akan mengeshare bagaimana coding untuk Binary Tree

untuk lebih jelasnya  Dalam ilmu komputer, sebuah pohon biner (binary tree) adalah
sebuah 
pohon struktur data di mana setiap simpul memiliki paling banyak dua anak. Secara khusus anaknya dinamakan kiri dan kanan. Penggunaan secara umum pohon biner adalah Pohon biner terurut, yang lainnnya adalah heap biner.

berikut adalah code C++ nya

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

/* A <span id="mvuhno2x61oe_1" class="mvuhno2x61oe">binary tree</span> node has data, pointer to left child
   and a pointer to right child */
struct node
{
   int data;
   struct node* left;
   struct node* right;
};

/* Prototypes for funtions needed in printPaths() */
void printPathsRecur(struct node* node, int path[], int pathLen);
void printArray(int ints[], int len);

/*Given a binary tree, print out all of its root-to-leaf
 paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(struct node* node)
{
  int path[1000];
  printPathsRecur(node, path, 0);
}

/* Recursive helper function -- given a node, and an array containing
 the path from the root node up to but not including this node,
 print out all the root-leaf paths.*/
void printPathsRecur(struct node* node, int path[], int pathLen)
{
  if (node==NULL)
    return;

  /* append this node to the path array */
  path[pathLen] = node->data;
  pathLen++;

  /* it's a leaf, so print the path that led to here  */
  if (node->left==NULL && node->right==NULL)
  {
    printArray(path, pathLen);
  }
  else
  {
    /* otherwise try both subtrees */
    printPathsRecur(node->left, path, pathLen);
    printPathsRecur(node->right, path, pathLen);
  }
}


/* UTILITY FUNCTIONS */
/* Utility that prints out an array on a line. */
void printArray(int ints[], int len)
{
  int i;
  for (i=0; i<len; i++)
  {
    printf("%d ", ints[i]);
  }
  printf("\n");
}   

/* utility that allocates a new node with the
   given data and NULL left and right pointers. */  
struct node* newnode(int data)
{
  struct node* node = (struct node*)
                       malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;
  
  return(node);
}
  
/* Driver program to test above functions*/
int main()
{
  
 struct node *root = newnode(5);
  root->left        = newnode(4);
  root->right       = newnode(8);
  root->left->left  = newnode(11);
  root->right->left  = newnode(13);
  root->right->right  = newnode(4);
  root->left->left->left  = newnode(7);
  root->left->left->right = newnode(2);
  root->right->right->right = newnode(1);
  
  printPaths(root);
  
  double number1 = 0.0;
double number2 = 0.0;
double number3 = 0.0;
double number4 = 0.0;
double answer;
cout <<"masukkan 4 angka dari path di atas"<<endl;
cin>>number1;
cin>>number2;
cin>>number3;
cin>>number4;

answer = (number1 + number2 + number3 + number4) /4;
cout<<"rata-ratanya adalah "<<answer<<endl;


  
  system ("pause");
  getchar();
  return 0;}

sekian untuk postingan kali ini

jangan lupa like commend and share yah

Terima kasih :)


www.ayeey.com www.resepkuekeringku.com www.desainrumahnya.com www.yayasanbabysitterku.com www.luvne.com www.cicicookies.com www.tipscantiknya.com www.mbepp.com www.kumpulanrumusnya.com www.trikcantik.net

0 comments:

Post a Comment