-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cpp
70 lines (64 loc) · 973 Bytes
/
Tree.cpp
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
#include "Tree.h"
#include <stdio.h>
#include <malloc.h>
SearchTree MakeEmpty(SearchTree T)
{
if (T != NULL)
{
MakeEmpty(T->Left);
MakeEmpty(T->Right);
free(T);
}
return NULL;
}
Position Find(ElementType X, SearchTree T)
{
if (T == NULL)
return NULL;
if (T->Element < X)
return Find(X, T->Right);
else if (T->Element > X)
return Find(X, T->Left);
else
return T;
}
Position FindMin(SearchTree T)
{
if (T == NULL)
return NULL;
if (T->Left == NULL)
return T;
else
return FindMin(T->Left);
}
Position FindMax(SearchTree T)
{
if (T == NULL)
return NULL;
if (T->Right == NULL)
return T;
else
return FindMin(T->Right);
}
SearchTree Insert(ElementType X, SearchTree T)
{
if (T == NULL)
{
T = (Position)malloc(sizeof(struct TreeNode));
T->Element = X;
T->Left = NULL;
T->Right = NULL;
}
else
{
if (T->Element > X)
{
T->Left = Insert(X, T->Left);
}
else
{
T->Right = Insert(X, T->Right);
}
}
return T;
}