Thursday, May 22, 2014

Pencerminan DBS in C++ Pseudo Code

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

struct node {
    int data;
    struct node* left;
    struct node* right;
};


struct node* simpul(int data) {
  struct node* node = (struct node*)
  malloc(sizeof(struct node));
  node->data = data;
  node->left = NULL;
  node->right = NULL;
 
  return(node);
}

void cerminkan(struct node* node)
{
  if (node==NULL)
    return;
  else
  {
    struct node* temp;
   

    cerminkan(node->left);
    cerminkan(node->right);

    temp        = node->left;
    node->left  = node->right;
    node->right = temp;
  }
}

void inOrder(struct node* node)
{
  if (node == NULL)
    return;
 
  inOrder(node->left);
  cout << node->data << " ";

  inOrder(node->right);
}


int main()
{
  struct node *root = simpul(4);
  root->left = simpul(5);
  root->right= simpul(6);
  root->left->left  = simpul(10);
  root->left->right = simpul(20);
  root->right->right = simpul(30);
  cout << "BST : ";
  inOrder(root);
  cerminkan(root);
  cout << endl << "BST CERMINAN: ";
  inOrder(root);
  getchar();
  return 0;
}

Artikel Terkait


EmoticonEmoticon