-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.c
More file actions
149 lines (128 loc) · 3.32 KB
/
binaryTree.c
File metadata and controls
149 lines (128 loc) · 3.32 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "binaryTree.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void initTree(struct treeNode *root)
{
root->left = NULL;
root->right = NULL;
}
void addChildTreeNode(struct treeNode *root, char *data, treeDirection td)
{
struct treeNode *newNode = (struct treeNode *)malloc(sizeof(struct treeNode));
strcpy(newNode->data, data);
newNode->left = NULL;
newNode->right = NULL;
if(td == LEFT)
root->left = newNode;
else if(td == RIGHT)
root->right = newNode;
else
free(newNode);
}
void initTreeChild(struct treeNode *root, treeDirection td)
{
struct treeNode *newNode = (struct treeNode *)malloc(sizeof(struct treeNode));
newNode->left = NULL;
newNode->right = NULL;
if(td == LEFT)
root->left = newNode;
else if(td == RIGHT)
root->right = newNode;
else
free(newNode);
}
void initTreeChildren(struct treeNode *root)
{
initTreeChild(root, LEFT);
initTreeChild(root, RIGHT);
}
struct treeNode *treeCopy(struct treeNode *orignal)
{
struct treeNode *copy = (struct treeNode *)malloc(sizeof(struct treeNode));
strcpy(copy->data, orignal->data);
if(orignal->left)
copy->left = treeCopy(orignal->left);
else
copy->left = NULL;
if(orignal->right)
copy->right = treeCopy(orignal->right);
else
copy->right = NULL;
return copy;
}
void visitTree(struct treeNode *treeRoot, int depth)
{
printf("%2d:\t%s\n", depth, treeRoot->data);
if(treeRoot->left != NULL)
{
printf("Going left\n");
visitTree(treeRoot->left, depth + 1);
}
if(treeRoot->right != NULL)
{
printf("Going right\n");
visitTree(treeRoot->right, depth + 1);
}
printf("Going up to %d\n", depth - 1);
}
void deleteTree(struct treeNode *treeRoot)
{
if(treeRoot->left != NULL)
{
deleteTree(treeRoot->left);
}
if(treeRoot->right != NULL)
{
deleteTree(treeRoot->right);
}
free(treeRoot);
}
void recursivePrint(struct treeNode* node, FILE* f, int i)
{
int lChild = 2*i;
int rChild = 2*i + 1;
fprintf(f, "node%d [label=\"%s\"];\n", i, node->data);
if (node->left)
{
fprintf(f, "node%d -> node%d;\n", i, lChild);
recursivePrint(node->left, f, lChild);
}
if (node->right)
{
fprintf(f, "node%d -> node%d;\n", i, rChild);
recursivePrint(node->right, f, rChild);
}
}
void generateDOT(struct treeNode* tree, FILE* stream)
{
fprintf(stream, "digraph BST {\n");
recursivePrint(tree, stream, 1);
fprintf(stream, "}\n");
}
void stepByStepGraph(struct treeNode* tree)
{
int x = 0;
char command[SIZE];
FILE *f;
system("rm tree*.svg");
while(tree->left)
{
f = fopen("tree.dot", "w");
generateDOT(tree, f);
fclose(f);
sprintf(command, "dot -Tsvg tree.dot -o tree%d.svg", x++);
system(command);
evaluateOne(tree, 0);
}
f = fopen("tree.dot", "w");
generateDOT(tree, f);
fclose(f);
sprintf(command, "dot -Tsvg tree.dot -o tree%d.svg", x++);
system(command);
}