# GÓC SÁNG TẠO > Khu vực lập trình > C/C++ >  Thuật toán cây nhị phân tìm kiếm

## thangpham18

Code:


```

[COLOR=#000000][/COLOR][COLOR=#FF8000]#include <stdio.h>#include<stdlib.h>#include<conio.h>[/COLOR][COLOR=#0000BB]typedef int element_type[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000BB]typedef struct node [/COLOR][COLOR=#007700]{  [/COLOR][COLOR=#0000BB]element_type element[/COLOR][COLOR=#007700];  [/COLOR][COLOR=#0000BB]struct node [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700], *[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700];} [/COLOR][COLOR=#0000BB]NODE[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000BB]void khoi_tao_cay[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]** [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  *[/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700];}[/COLOR][COLOR=#0000BB]void insert[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]**[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  if ([/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element [/COLOR][COLOR=#007700]< (*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700])->[/COLOR][COLOR=#0000BB]element[/COLOR][COLOR=#007700])    if ((*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700])->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700])      [/COLOR][COLOR=#0000BB]insert[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700], &(*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700])->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700]);    else       (*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700])->[/COLOR][COLOR=#0000BB]left [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700];  else    if ((*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700])->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700])      [/COLOR][COLOR=#0000BB]insert[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700], &(*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700])->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700]);    else       (*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700])->[/COLOR][COLOR=#0000BB]right [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700];}[/COLOR][COLOR=#0000BB]void insert_node[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]element_type e[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]**[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){   [/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700];   [/COLOR][COLOR=#0000BB]tmp [/COLOR][COLOR=#007700]= ([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*)[/COLOR][COLOR=#0000BB]malloc[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]sizeof[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE[/COLOR][COLOR=#007700]));   [/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]e[/COLOR][COLOR=#007700];   [/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]left [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700];   [/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]right [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700];   if (*[/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]== [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700])     *[/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700];   else     [/COLOR][COLOR=#0000BB]insert[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]tmp[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);}[/COLOR][COLOR=#0000BB]void nhap_cay[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]**[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  [/COLOR][COLOR=#0000BB]element_type e[/COLOR][COLOR=#007700];  do {    [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Nhap element (-1 de ket thuc) : "[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]scanf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"%d"[/COLOR][COLOR=#007700], &[/COLOR][COLOR=#0000BB]e[/COLOR][COLOR=#007700]);    if ([/COLOR][COLOR=#0000BB]e [/COLOR][COLOR=#007700]!= -[/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700])      [/COLOR][COLOR=#0000BB]insert_node[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]e[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);  } while ([/COLOR][COLOR=#0000BB]e [/COLOR][COLOR=#007700]!= -[/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700]);}[/COLOR][COLOR=#0000BB]void NLR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  if ([/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700])  {    [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"%d "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]NLR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]NLR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700]);  }}[/COLOR][COLOR=#0000BB]void NRL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  if ([/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700])  {    [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"%d "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]NRL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]NRL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700]);  }}[/COLOR][COLOR=#0000BB]void LNR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  if ([/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700])  {    [/COLOR][COLOR=#0000BB]LNR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"%d "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]LNR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700]);  }}[/COLOR][COLOR=#0000BB]void LRN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  if ([/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700])  {    [/COLOR][COLOR=#0000BB]LRN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]LRN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"%d "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element[/COLOR][COLOR=#007700]);  }}[/COLOR][COLOR=#0000BB]void RNL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  if ([/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700])  {    [/COLOR][COLOR=#0000BB]RNL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"%d "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]RNL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700]);  }}[/COLOR][COLOR=#0000BB]void RLN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]NODE [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]){  if ([/COLOR][COLOR=#0000BB]root [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000BB]NULL[/COLOR][COLOR=#007700])  {    [/COLOR][COLOR=#0000BB]RLN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]right[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]RLN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]left[/COLOR][COLOR=#007700]);    [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"%d "[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000BB]element[/COLOR][COLOR=#007700]);  }}[/COLOR][COLOR=#0000BB]void main[/COLOR][COLOR=#007700](){   [/COLOR][COLOR=#0000BB]khoi_tao_cay[/COLOR][COLOR=#007700](&[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]nhap_cay[/COLOR][COLOR=#007700](&[/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Duyet cay NLR : "[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]NLR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Duyet cay NRL : "[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]NRL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Duyet cay LNR : "[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]LNR[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Duyet cay LRN : "[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]LRN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Duyet cay RNL : "[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]RNL[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]printf[/COLOR][COLOR=#007700]([/COLOR][COLOR=#DD0000]"Duyet cay RLN : "[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]RLN[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]root[/COLOR][COLOR=#007700]);   [/COLOR][COLOR=#0000BB]getch[/COLOR][COLOR=#007700]();}[/COLOR] 



```

----------


## xecutkit

Các bạn nào có nhu cầu hay khó khăn về mấy cái loại đó có thể PM cho mình, mình sẽ cố gắng giúp đỡ !!!

Sống ở đời Cần phải có 1 tấm lòng !!!!!!!!!!!!!!!!!!!!!!!!!!!

----------


## hautran200594

Tôi quan tâm nhưng bạn có Ví dụ hoặc Demo bằng Pascal không? Tôi không rành C lắm

----------


## thomtomtit

*Cây nhị phân, đồ thị...*

Xem nội dung và có cả Demo : http://etalks.vn/forum/upload/showthread.php?t=94

----------

