-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha5f5.c
More file actions
111 lines (95 loc) · 2.56 KB
/
a5f5.c
File metadata and controls
111 lines (95 loc) · 2.56 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
108
109
110
111
// File name: a5f5.c
#include <stdio.h>
#include <stdlib.h>
typedef char BinTreeElementType;
typedef struct BinTreeNode *BinTreePointer;
struct BinTreeNode {
BinTreeElementType Data;
BinTreePointer LChild, RChild;
} ;
typedef enum {
FALSE, TRUE
} boolean;
void CreateBST(BinTreePointer *Root);
void BSTInsert(BinTreePointer *Root, BinTreeElementType Item);
void BSTLevel(BinTreePointer Root, BinTreeElementType Item, int Level);
main()
{
BinTreePointer ARoot;
int Level;
CreateBST(&ARoot);
BSTInsert(&ARoot, 'P');
BSTInsert(&ARoot, 'R');
BSTInsert(&ARoot, 'O');
BSTInsert(&ARoot, 'C');
BSTInsert(&ARoot, 'E');
BSTInsert(&ARoot, 'D');
BSTInsert(&ARoot, 'U');
BSTInsert(&ARoot, 'R');
BSTInsert(&ARoot, 'E');
BSTLevel(ARoot, 'P', Level);
BSTLevel(ARoot, 'O', Level);
BSTLevel(ARoot, 'C', Level);
BSTLevel(ARoot, 'E', Level);
BSTLevel(ARoot, 'D', Level);
system("PAUSE");
}
void BSTLevel(BinTreePointer Root, BinTreeElementType Item, int Level)
{
BinTreePointer LocPtr;
boolean Found;
LocPtr = Root;
Found = FALSE;
Level = 1;
while (!Found && LocPtr != NULL)
{
if (Item < (LocPtr)->Data)
{
(LocPtr) = (LocPtr)->LChild;
Level++;
}else if (Item > (LocPtr)->Data){
(LocPtr) = (LocPtr)->RChild;
Level++;
}else
Found = TRUE;
}
if(!Found)
Level = -1;
printf("Level of %c: ",Item);
printf("%d\n",Level);
}
void CreateBST(BinTreePointer *Root)
{
*Root = NULL;
}
void BSTInsert(BinTreePointer *Root, BinTreeElementType Item)
{
BinTreePointer LocPtr, Parent;
boolean Found;
LocPtr = *Root;
Parent = NULL;
Found = FALSE;
while (!Found && LocPtr != NULL) {
Parent = LocPtr;
if (Item < LocPtr->Data)
LocPtr = LocPtr ->LChild;
else if (Item > LocPtr ->Data)
LocPtr = LocPtr ->RChild;
else
Found = TRUE;
}
if (Found)
printf("To %c EINAI HDH STO DDA\n", Item);
else {
LocPtr = (BinTreePointer)malloc(sizeof (struct BinTreeNode));
LocPtr ->Data = Item;
LocPtr ->LChild = NULL;
LocPtr ->RChild = NULL;
if (Parent == NULL)
*Root = LocPtr;
else if (Item < Parent ->Data)
Parent ->LChild = LocPtr;
else
Parent ->RChild = LocPtr;
}
}