Thứ Ba, 15 tháng 5, 2012

Tong Gia Tri Cac Nut Tren CNP

Posted by Unknown Thứ Ba, tháng 5 15, 2012, under |


// TongNut.cpp : Defines the entry point for the console application.
//


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

typedef int element_type;
typedef struct node
{
  element_type element;
  struct node *left, *right;
} NODE;

NODE *root;

void khoi_tao_cay(NODE ** root)
{
  *root = NULL;
}

void insert(NODE *tmp, NODE **root)
{

  if (tmp->element < (*root)->element)
    if ((*root)->left)
      insert(tmp, &(*root)->left);
    else
       (*root)->left = tmp;
  else
    if ((*root)->right)
      insert(tmp, &(*root)->right);
    else
       (*root)->right = tmp;
}

void insert_node(element_type e, NODE **root)
{
   NODE *tmp;

   tmp = (NODE *)malloc(sizeof(NODE));
   tmp->element = e;
   tmp->left = NULL;
   tmp->right = NULL;
   if (*root == NULL)
     *root = tmp;
   else
     insert(tmp, root);
}

void nhap_cay(NODE **root)
{
  element_type e;
  printf("\n NHAP CAY NHI PHAN ! \n");
  printf("\n Nhap -1 de ket thuc !\n ");
  int i=1;
  do {

    printf("\n Nhap phan tu thu %d :",i);
    scanf("%d", &e);
    if (e != -1)
      insert_node(e, root);
 i++;
  } while (e != -1);
}

char Search(NODE *root,int x)
{
   NODE *temp = root;
   while (temp != NULL ) {
      if (temp -> element == x)
         return 1;
      else
      {
         if (temp -> element > x)
            temp = temp -> left;
         else
            temp = temp -> right;
      }
   }
   return 0;
}


int tong_cay(NODE *root)
{
if (root==NULL)
return 0;
else
{
return root->element + tong_cay(root->left) + tong_cay(root->right);
}

}

void main()
{
   khoi_tao_cay(&root);
   nhap_cay(&root);
 
   printf("\n Tong gia tri cac nut cua cay:%d",tong_cay(root));
 
   getch();
}


Tim Duong Di Tu Goc Toi Nut Co Gia Tri Lon Nhat CNP

Posted by Unknown Thứ Ba, tháng 5 15, 2012, under |


//
//

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

typedef int element_type;
typedef struct node
{
  element_type element;
  struct node *left, *right;
} NODE;

NODE *root;

void khoi_tao_cay(NODE ** root)
{
  *root = NULL;
}

void insert(NODE *tmp, NODE **root)
{

  if (tmp->element < (*root)->element)
    if ((*root)->left)
      insert(tmp, &(*root)->left);
    else
       (*root)->left = tmp;
  else
    if ((*root)->right)
      insert(tmp, &(*root)->right);
    else
       (*root)->right = tmp;
}

void insert_node(element_type e, NODE **root)
{
   NODE *tmp;

   tmp = (NODE *)malloc(sizeof(NODE));
   tmp->element = e;
   tmp->left = NULL;
   tmp->right = NULL;
   if (*root == NULL)
     *root = tmp;
   else
     insert(tmp, root);
}

void nhap_cay(NODE **root)
{
  element_type e;
  printf("\n NHAP CAY NHI PHAN ! \n");
  printf("\n Nhap -1 de ket thuc !\n ");
  int i=1;
  do {

    printf("\n Nhap phan tu thu %d :",i);
    scanf("%d", &e);
    if (e != -1)
      insert_node(e, root);
 i++;
  } while (e != -1);
}



int gia_tri_nut_max(NODE *root)
{
int max=root->element;
while (root->right!=NULL)
{
root=root->right;
max=root->element;
}
return max;
}

char Search(NODE *root,int x)
{
   NODE *temp = root;
   while (temp != NULL ) {
      if (temp -> element == x)
         return 1;
      else
      {
         if (temp -> element > x)
            temp = temp -> left;
         else
            temp = temp -> right;
      }
   }
   return 0;
}


char Search(NODE *root,int x) ;
void duong_di_tu_goc(NODE *root, int x)
{

if (Search(root,x))
{
while (root->element!=x)
{
printf(" %d -> ", root->element);
if (root->element>x)
 root=root->left;
else
 if (root->element<x)
  root=root->right;
}

printf(" %d -> ",root->element);
}
else

 return ;
}

void main()
{
   khoi_tao_cay(&root);
   nhap_cay(&root);
 
printf("\n Nut co gia tri lon nhat trong cay :%d ", gia_tri_nut_max(root));

int x=gia_tri_nut_max(root);
printf("\n Duong di tu nut goc den nut co gia tri lon nhat:");
duong_di_tu_goc(root, x);

 


   getch();
}

Tinh Do Cao Cua Nut Gia Tri Bang X Trong CNP

Posted by Unknown Thứ Ba, tháng 5 15, 2012, under |


//  Do Cao tai goc h la 0
//

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

typedef int element_type;
typedef struct node
{
  element_type element;
  struct node *left, *right;
} NODE;

NODE *root;

void khoi_tao_cay(NODE ** root)
{
  *root = NULL;
}

void insert(NODE *tmp, NODE **root)
{

  if (tmp->element < (*root)->element)
    if ((*root)->left)
      insert(tmp, &(*root)->left);
    else
       (*root)->left = tmp;
  else
    if ((*root)->right)
      insert(tmp, &(*root)->right);
    else
       (*root)->right = tmp;
}

void insert_node(element_type e, NODE **root)
{
   NODE *tmp;

   tmp = (NODE *)malloc(sizeof(NODE));
   tmp->element = e;
   tmp->left = NULL;
   tmp->right = NULL;
   if (*root == NULL)
     *root = tmp;
   else
     insert(tmp, root);
}

void nhap_cay(NODE **root)
{
  element_type e;
  printf("\n NHAP CAY NHI PHAN ! \n");
  printf("\n Nhap -1 de ket thuc !\n ");
  int i=1;
  do {

    printf("\n Nhap phan tu thu %d :",i);
    scanf("%d", &e);
    if (e != -1)
      insert_node(e, root);
 i++;
  } while (e != -1);
}

char Search(NODE *root,int x)
{
   NODE *temp = root;
   while (temp != NULL ) {
      if (temp -> element == x)
         return 1;
      else
      {
         if (temp -> element > x)
            temp = temp -> left;
         else
            temp = temp -> right;
      }
   }
   return 0;
}

int tinh_do_cao_gia_tri_x(NODE *root, int x)
{
if (root!=NULL)
{
if (root->element==x)
return 0;
if (root->element>x)
return 1+tinh_do_cao_gia_tri_x(root->left,x);
if (root->element<x)
return 1+tinh_do_cao_gia_tri_x(root->right,x);
}
}

void main()
{
   khoi_tao_cay(&root);
   nhap_cay(&root);

   int x;
   printf("\n Nhap gia tri can tim :");
   scanf("%d",&x);
   if(Search(root,x)==1)
  printf("\n Do cao cua nut co gia tri bang %d la:%d ",x ,tinh_do_cao_gia_tri_x(root,x));
   else
  printf("\n Khong co nut gia tri bang %d  trong cay!", x);

   getch();
}

Tim Duong Di Tu Goc Den Nut X Cua CNP

Posted by Unknown Thứ Ba, tháng 5 15, 2012, under |


// Tim_CNP.cpp : Defines the entry point for the console application.
//

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

typedef int element_type;
typedef struct node
{
  element_type element;
  struct node *left, *right;
} NODE;

NODE *root;

void khoi_tao_cay(NODE ** root)
{
  *root = NULL;
}

void insert(NODE *tmp, NODE **root)
{

  if (tmp->element < (*root)->element)
    if ((*root)->left)
      insert(tmp, &(*root)->left);
    else
       (*root)->left = tmp;
  else
    if ((*root)->right)
      insert(tmp, &(*root)->right);
    else
       (*root)->right = tmp;
}

void insert_node(element_type e, NODE **root)
{
   NODE *tmp;

   tmp = (NODE *)malloc(sizeof(NODE));
   tmp->element = e;
   tmp->left = NULL;
   tmp->right = NULL;
   if (*root == NULL)
     *root = tmp;
   else
     insert(tmp, root);
}

void nhap_cay(NODE **root)
{
  element_type e;
  printf("\n NHAP CAY NHI PHAN ! \n");
  printf("\n Nhap -1 de ket thuc !\n ");
  int i=1;
  do {

    printf("\n Nhap phan tu thu %d :",i);
    scanf("%d", &e);
    if (e != -1)
      insert_node(e, root);
 i++;
  } while (e != -1);
}

char Search(NODE *root,int x)
{
   NODE *temp = root;
   while (temp != NULL ) {
      if (temp -> element == x)
         return 1;
      else
      {
         if (temp -> element > x)
            temp = temp -> left;
         else
            temp = temp -> right;
      }
   }
   return 0;
}

char Search(NODE *root,int x) ;
void duong_di_tu_goc(NODE *root, int x)
{

if (Search(root,x))
{
while (root->element!=x)
{
printf(" %d -> ", root->element);
if (root->element>x)
root=root->left;
else
if (root->element<x)
root=root->right;
}

printf(" %d -> ",root->element);
}
else

return ;
}

void main()
{
   khoi_tao_cay(&root);
   nhap_cay(&root);

   int x;
   printf("\n Nhap gia tri can tim :");
   scanf("%d",&x);
   if(Search(root,x)==1)
  printf("\n Duong di tu goc den %d nhu sau: ",x);
   else
  printf("\n Khong co phan tu%d  trong cay!", x);


   duong_di_tu_goc(root,x);


   getch();
}

Duyet CNP Bang Mang

Posted by Unknown Thứ Ba, tháng 5 15, 2012, under |


// CNP_MANG.cpp
//

#include "stdafx.h"

#include <stdio.h>
#include <conio.h>
#define max 100
#define MAXLENGTH 100         //chi so toi da cua mang
#define NIL -1
typedef int DataType;
typedef int Node;
typedef struct {
   DataType Data[MAXLENGTH];   //Luu gia tri cua nut
   int MaxNode;
}Tree;
//Kiem tra cay rong
int EmptyTree(Tree T) {
   return T.MaxNode == 0;
}
//Xac dinh nut goc trong cay
Node Root(Tree T) {
   if (!EmptyTree(T))
      return 0;
   else
      return NIL;
}
//con trai cua nut p
int Left_Child(Node p, Tree T) {
      return 2*(p+1) - 1;
}
//con phai cua nut p
int Right_Child(Node p, Tree T) {
      return 2*(p+1);
}
//duyet tien tu
void NLR(Node p, Tree T) {
   if(T.Data[p]!=NIL) {
      printf("%d  ", T.Data[p]);
      NLR(Left_Child(p,T),T);
      NLR(Right_Child(p,T),T);
   }
}
//duyet trung tu
void LNR(Node p, Tree T) {
   if(T.Data[p]!=NIL) {
      LNR(Left_Child(p,T),T);
      printf("%d  ", T.Data[p]);
      LNR(Right_Child(p,T),T);
   }
}
//duyet hau tu
void LRN(Node p, Tree T) {
   if(T.Data[p]!=NIL) {
      LRN(Left_Child(p,T),T);
      LRN(Right_Child(p,T),T);
      printf("%d  ", T.Data[p]);
   }
}
/*doc cay
neu khong co con trai hoac phai thi nhap -1*/
void Read_Tree(Tree * T) {
   int i = 0, Child = 0;
   printf("Nhap vao so nut: ");
   scanf("%d",&(*T).MaxNode);
   while(i<(*T).MaxNode) {
      if(i==0)
      {
         printf("Nut goc:");
         scanf("%d",&(*T).Data[i]);
         (*T).Data[Left_Child(i,*T)] = NIL;
         (*T).Data[Right_Child(i,*T)] = NIL;
         i++;
      }
      else
      if((*T).Data[Child]!=NIL)
      {   int k;
         printf("Con trai %d: ",Child);
         k = Left_Child(Child,*T);
         scanf("%d",&(*T).Data[k]);
         if((*T).Data[k] != NIL) {
            (*T).Data[Left_Child(k,*T)] = NIL;
            (*T).Data[Right_Child(k,*T)] = NIL;
            i++;
         }
         printf("Con phai %d: ",Child);
         k = Right_Child(Child,*T);
         scanf("%d",&(*T).Data[k]);
         if((*T).Data[k] != NIL) {
            (*T).Data[Left_Child(k,*T)] = NIL;
            (*T).Data[Right_Child(k,*T)] = NIL;
            i++;
         }
         Child++;
      }
   }
}
void main() {
   Tree T;
   Read_Tree(&T);
   printf(" \n Duyet tien tu.\n");
   NLR(Root(T),T);
   printf("\n Duyet trung tu.\n");
   LNR(Root(T),T);
   printf("\n Duyet hau tu.\n");
   LRN(Root(T),T);
   getch();
}


Xem Nhiều

Bài đăng phổ biến

Lưu trữ blog

Blog Archive