NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

//pendeklarasian struct sebuah tree awal
struct Node{
int data;
Node *kiri;
Node *kanan;
};

//fungsi untuk menambahkan node baru
void tambah(Node **root, int databaru)
{
//jika root masih kosong
if((*root) == NULL)
{
//pembuatan node baru
Node *baru;
//pengalokasian memori dari node yang telah dibuat
baru = new Node;
//inisialisasi awal node yang baru dibuat
baru->data = databaru;
baru->kiri = NULL;
baru->kanan = NULL;
(*root) = baru;
(*root)->kiri = NULL;
(*root)->kanan = NULL;
printf("Data bertambah!");
}
//jika data yang akan dimasukkan lebih kecil daripada elemen root, maka akan diletakkan di node sebelah kiri.
else if(databaru<(*root)->data)
tambah(&(*root)->kiri, databaru);
//jika data yang akan dimasukkan lebih besar daripada elemen root, maka akan diletakkan di node sebelah kanan
else if(databaru>(*root)->data)
tambah(&(*root)->kanan, databaru);
//jika saat dicek data yang akan dimasukkan memiliki nilai yang sama dengan data pada root
else if(databaru == (*root)->data)
printf("Data sudah ada!");
}

//fungsi yang digunakan untuk mencetak tree secara preOrder
void preOrder(Node *root)
{
if(root != NULL){
printf("%d ", root->data);
preOrder(root->kiri);
preOrder(root->kanan);
}
}

//fungsi yang digunakan untuk mencetak tree secara inOrder
void inOrder(Node *root)
{
if(root != NULL){
inOrder(root->kiri);
printf("%d ", root->data);
inOrder(root->kanan);
}
}

//fungsi yang digunakan untuk mencetak tree secara postOrder
void postOrder(Node *root)
{
if(root != NULL){
postOrder(root->kiri);
postOrder(root->kanan);
printf("%d ", root->data);
}
}

//fungsi utama
int main()
{
//deklarasikan variabel
int pil, data;// c;
Node *pohon; //*t;
pohon = NULL; //inisialisasi node pohon
//perulangan do-while
do
{
system("cls"); //bersihkan layar
printf("t#PROGRAM TREE C++#");
printf("nt==================");
printf("nMENU");
printf("n----n");
printf("1. Tambahn");
printf("2. Lihat pre-ordern");
printf("3. Lihat in-ordern");
printf("4. Lihat post-ordern");
printf("5. Exitn");
printf("Pilihan : ");
scanf("%d", &pil);
switch(pil)
{
//jika pil bernilai 1
case 1 :
printf("nINPUT : ");
printf("n-------");
printf("nData baru : ");
scanf("%d", &data);
//panggil fungsi untuk menambah node yang berisi data pada tree
tambah(&pohon, data);
break;

//jika pil bernilai 2
case 2 :
printf("nOUTPUT PRE ORDER : ");
printf("n------------------n");
if(pohon!=NULL)
//panggil fungsi untuk mencetak data secara preOrder
preOrder(pohon);
else
printf("Masih kosong!");
break;

//jika pil bernilai 3
case 3 :
printf("nOUTPUT IN ORDER : ");
printf("n------------------n");
if(pohon!=NULL)
//panggil fungsi untuk mencetak data secara inOrder
inOrder(pohon);
else
printf("Masih kosong!");
break;

//jika pil bernilai 4
case 4 :
printf("nOUTPUT POST ORDER : ");
printf("n------------------n");
if(pohon!=NULL)
//panggil fungsi untuk mencetak data secara postOrder
postOrder(pohon);
else
printf("Masih kosong!");
break;
}
_getch();
}while(pil != 5); //akan diulang jika input tidak samadengan 5
return EXIT_FAILURE;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.