-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.c
More file actions
107 lines (81 loc) · 1.81 KB
/
binaryTree.c
File metadata and controls
107 lines (81 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node * rigth;
struct node *left;
}Node;
Node *root = NULL;
Node* insert(Node *leaf,int num){
if (root==NULL){
Node *leaf = (Node *)malloc(sizeof(Node));
leaf->data=num;
leaf->rigth=NULL;
leaf->left=NULL;
root=leaf;
printf("This is the root value %d\n",root->data);
}else{
if(leaf==NULL){
leaf = (Node *)malloc(sizeof(Node));
leaf->data=num;
leaf->rigth=NULL;
leaf->left=NULL;
printf("este es el valor de la hoja %d\n",leaf->data);
}else{
if( num<leaf->data){
printf("recorriendo hacia la izquierda\n");
leaf->left = insert (leaf->left,num);
}else if( num>leaf->data){
printf("recorriendo hacia la derecha\n");
leaf->rigth = insert (leaf->rigth,num);
}
}
return leaf;
}
}
void preOrden(Node *leaf){
if(leaf!=NULL){
printf("%d\n",leaf->data);
preOrden(leaf->left);
preOrden(leaf->rigth);
}
}
void inOrden(Node *leaf){
if(leaf!=NULL){
inOrden(leaf->left);
printf("%d\n",leaf->data);
inOrden(leaf->rigth);
}
}
void postOrden(Node *leaf){
if(leaf!=NULL){
postOrden(leaf->left);
postOrden(leaf->rigth);
printf("%d\n",leaf->data);
}
}
int main(){
int optChoosed;
int num;
do{
printf("1.- Insertar dato\n");
printf("2.-Recorridos\n");
scanf("%d",&optChoosed);
switch(optChoosed){
case 1:
printf("Dame el dato a insertar\n");
scanf("%d",&num);
insert(root,num);
break;
case 2:
printf("Pre-Orden\n");
preOrden(root);
printf("In-Orden\n");
inOrden(root);
printf("Post-Orden\n");
postOrden(root);
break;
}
}while(optChoosed!=6);
return 0;
}