Skip to content

Commit 8a6c0b3

Browse files
U-SANTHOSH-PC\SANTHOSHU-SANTHOSH-PC\SANTHOSH
authored andcommitted
added travels.c - meant to implement the depth first and breadth first routines
1 parent 88b9d66 commit 8a6c0b3

File tree

7 files changed

+202
-102
lines changed

7 files changed

+202
-102
lines changed

Binary-Trees/Makefile

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ CFLAGS = -O0 -g -std=c99 -Wall -Wextra -Wshadow -pedantic -Werror
1414
CXXFLAGS = -O0 -g -std=c++11 -Wall -Wextra -Wshadow -pedantic -Weffc++ -Werror
1515
# define any directories containing header files other than /usr/include
1616
#
17-
INCLUDES =
17+
INCLUDES =
1818

1919
# define library paths in addition to /usr/lib
2020
# if I wanted to include libraries not in /usr/lib I'd specify
2121
# their path using -Lpath, something like:
22-
LFLAGS =
22+
LFLAGS =
2323

2424
# define any libraries to link into executable:
25-
# if I want to link in libraries (libx.so or libx.a) I use the -llibname
25+
# if I want to link in libraries (libx.so or libx.a) I use the -llibname
2626
# option, something like (this will link in libmylib.so and libm.so:
27-
LIBS =
27+
LIBS =
2828

2929
# define the C source files
30-
SRCS = main.c utilityFunctions.c stanfordProblems.c graphVizPrintBST.c
30+
SRCS = main.c utilityFunctions.c stanfordProblems.c graphVizPrintBST.c traversals.c
3131

32-
# define the C object files
32+
# define the C object files
3333
#
3434
# This uses Suffix Replacement within a macro:
3535
# $(name:string1=string2)
@@ -39,11 +39,11 @@ SRCS = main.c utilityFunctions.c stanfordProblems.c graphVizPrintBST.c
3939
#
4040
OBJS = $(SRCS:.c=.o)
4141

42-
# define the executable file
42+
# define the executable file
4343
MAIN = binaryTreeExe
4444

4545
#
46-
# The following part of the makefile is generic; it can be used to
46+
# The following part of the makefile is generic; it can be used to
4747
# build any executable just by changing the definitions above and by
4848
# deleting dependencies appended to the file from 'make depend'
4949
#
@@ -53,12 +53,12 @@ MAIN = binaryTreeExe
5353
all: $(MAIN)
5454
@echo $(MAIN) has been compiled
5555

56-
$(MAIN): $(OBJS)
56+
$(MAIN): $(OBJS)
5757
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
5858

5959
# this is a suffix replacement rule for building .o's from .c's
6060
# it uses automatic variables $<: the name of the prerequisite of
61-
# the rule(a .c file) and $@: the name of the target of the rule (a .o file)
61+
# the rule(a .c file) and $@: the name of the target of the rule (a .o file)
6262
# (see the gnu make manual section about automatic variables)
6363
.c.o:
6464
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
@@ -71,4 +71,4 @@ depend: $(SRCS)
7171

7272
# DO NOT DELETE THIS LINE -- make depend needs it
7373

74-
# Tutorial: http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html
74+
# Tutorial: http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html

Binary-Trees/binaryTree.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <stdbool.h> // true and false
44
#include <stdio.h>
55
#include <stdlib.h>
6+
#include <limits.h> // INT_MAX etc
67

78
struct node {
89
int data;
@@ -14,10 +15,10 @@ int lookup(struct node* node, int target);
1415
int lookup_nonRecursive(struct node* node, int target);
1516
struct node* NewNode(int data);
1617
struct node* insert(struct node* node, int data);
17-
struct node* insert_nonRecursive(struct node* node, int data)
18+
struct node* insert_nonRecursive(struct node* node, int data);
1819
struct node* build123();
1920
void bst_print_dot(struct node* tree, FILE* stream);
2021
struct node* buildN(int n);
2122
void deleteTree(struct node** node_ref);
22-
23+
void structure ( struct node *tree );
2324
#endif

Binary-Trees/main.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
/*
44
http://cslibrary.stanford.edu/110/BinaryTrees.html
55
*/
6+
void graphviz_tree(struct node* tree){
7+
FILE *fp;
8+
fp = fopen("./tree.dot", "w+");
9+
bst_print_dot(tree, fp);
10+
printf("%s\n", "run: dot -Tpng tree.dot -o tree.png");
11+
fclose(fp);
12+
}
613

714
int main( void ) {
8-
FILE *fp;
9-
fp = fopen("./tree.dot", "w+");
1015
struct node* tree = build123();
11-
bst_print_dot(tree, fp);
12-
printf("%s\n", "run: dot -Tpng tree.dot -o tree.png");
13-
fclose(fp);
16+
structure(tree);
1417
deleteTree(&tree);
1518
}

Binary-Trees/stanfordProblems.c

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ struct node* build123(){
55
root = insert(root, 2);
66
root = insert(root, 1);
77
root = insert(root, 3);
8-
return(root);
8+
return(root);
99
}
1010

1111
struct node* buildN(int n){
1212
struct node* root = NULL;
1313
for (int i=1; i<=n ; i++ ) {
1414
insert(root, i);
1515
}
16-
return(root);
16+
return(root);
1717
}
1818

1919
/*
2020
Compute the number of nodes in a tree.
2121
*/
22-
int size(struct node* node) {
22+
int size(struct node* node) {
2323
if( node == NULL ) {
2424
return 0;
2525
}
@@ -46,78 +46,77 @@ int maxDepth(struct node* node) {
4646
return the minimum data value found in that tree.
4747
Note that the entire tree does not need to be searched.
4848
*/
49-
int minValue(struct node* node) {
49+
int minValue(struct node* node) {
5050
struct node* current = node;
5151
// loop down to find the leftmost leaf
5252
while (current->left != NULL) {
5353
current = current->left;
5454
}
55-
return(current->data);
55+
return(current->data);
5656
}
5757

5858
/*
5959
Given a non-empty binary search tree,
6060
return the maximum data value found in that tree.
6161
Note that the entire tree does not need to be searched.
6262
*/
63-
int maxValue(struct node* node) {
63+
int maxValue(struct node* node) {
6464
struct node* current = node;
6565
// loop down to find the leftmost leaf
6666
while (current->right != NULL) {
6767
current = current->right;
6868
}
69-
return(current->data);
69+
return(current->data);
7070
}
7171

7272
/*
7373
Given a binary search tree, print out
7474
its data elements in increasing
7575
sorted order.
76-
This is known as an "inorder" traversal of the tree.
76+
This is known as an "inorder" traversal of the tree.
7777
*/
78-
void printTree(struct node* node) {
78+
void printTree(struct node* node) {
7979
if (node == NULL) return;
8080
printTree(node->left);
8181
printf("%d ", node->data);
82-
printTree(node->right);
82+
printTree(node->right);
8383
}
8484

8585
/*
86-
Given a binary tree, print its nodes according to the "bottom-up" postorder
86+
Given a binary tree, print its nodes according to the "bottom-up" postorder
8787
traversal.
88-
-- both subtrees of a node are printed out completely before the node itself
89-
is printed, and each left subtree is printed before the right subtree.
88+
-- both subtrees of a node are printed out completely before the node itself
89+
is printed, and each left subtree is printed before the right subtree.
9090
*/
91-
void printPostorder(struct node* node) {
91+
void printPostorder(struct node* node) {
9292
if (node == NULL) return;
9393
printPostorder(node->left);
9494
printPostorder(node->right);
9595
printf("%d ", node->data);
9696
}
9797

98-
/*
99-
Given a binary tree and a sum, return true if the tree has a root-to-leaf
100-
path such that adding up all the values along the path equals the given sum.
98+
/*
99+
Given a binary tree and a sum, return true if the tree has a root-to-leaf
100+
path such that adding up all the values along the path equals the given sum.
101101
Return false if no such path can be found.
102102
103103
Strategy: subtract the node value from the sum when recurring down,
104-
and check to see if the sum is 0 when you run out of tree.
104+
and check to see if the sum is 0 when you run out of tree.
105105
*/
106106
int hasPathSum(struct node* node, int sum) {
107107
if (node == NULL) return sum == 0;
108108
else {
109-
return hasPathSum(node->left, sum - node->data)
110-
|| hasPathSum(node->right, sum - node->data)
109+
return hasPathSum(node->left, sum - node->data)
110+
|| hasPathSum(node->right, sum - node->data);
111111
}
112112
}
113113

114-
/*
115-
Given a binary tree, print out all of its root-to-leaf
116-
paths, one per line. Uses a recursive helper to do the work.
117-
*/
118-
void printPaths(struct node* node) {
119-
int path[1000];
120-
printPathsRecur(node, path, 0);
114+
// Utility that prints out an array on a line.
115+
void printArray(int ints[], int len) {
116+
int i;
117+
for (i=0; i<len; i++)
118+
printf("%d ", ints[i]);
119+
printf("\n");
121120
}
122121

123122
/*
@@ -140,18 +139,20 @@ void printPathsRecur(struct node* node, int path[], int pathLen) {
140139
printPathsRecur(node->right, path, pathLen);
141140
}
142141
}
143-
144-
// Utility that prints out an array on a line.
145-
void printArray(int ints[], int len) {
146-
int i;
147-
for (i=0; i<len; i++)
148-
printf("%d ", ints[i]);
149-
printf("\n");
142+
/*
143+
Given a binary tree, print out all of its root-to-leaf
144+
paths, one per line. Uses a recursive helper to do the work.
145+
*/
146+
void printPaths(struct node* node) {
147+
int path[1000];
148+
printPathsRecur(node, path, 0);
150149
}
151150

151+
152+
152153
/*
153-
Change a tree so that the roles of the left and right pointers are swapped
154-
at every node.
154+
Change a tree so that the roles of the left and right pointers are swapped
155+
at every node.
155156
*/
156157
void mirror(struct node* node) {
157158
if (node==NULL) return;
@@ -164,12 +165,12 @@ void mirror(struct node* node) {
164165
struct node* prevRight = node->right;
165166
node->right = node->left;
166167
node->left = prevRight;
167-
168+
168169
}
169170

170171
/*
171-
For each node in a binary search tree, create a new duplicate node,
172-
and insert the duplicate as the left child of the original node.
172+
For each node in a binary search tree, create a new duplicate node,
173+
and insert the duplicate as the left child of the original node.
173174
*/
174175
void doubleTree(struct node* node) {
175176
if (node==NULL) return;
@@ -184,16 +185,16 @@ void doubleTree(struct node* node) {
184185
node->left->left = prevLeft;
185186
}
186187

187-
/*
188+
/*
188189
Given two binary trees, return true if they are structurally
189-
identical -- they are made of nodes with the same values
190-
arranged in the same way.
190+
identical -- they are made of nodes with the same values
191+
arranged in the same way.
191192
*/
192193
int sameTree(struct node* a, struct node* b) {
193194
if( a == NULL && b == NULL) // both empty
194-
return true;
195+
return true;
195196
else if( a != NULL && b != NULL ) // both non-empty, so compare
196-
return( (a->data == b->data) &&
197+
return( (a->data == b->data) &&
197198
sameTree(a->left, b->left) &&
198199
sameTree(a->right, b->right) );
199200
else
@@ -230,7 +231,7 @@ int countTrees(int numKeys) {
230231

231232
return(sum);
232233
}
233-
}
234+
}
234235

235236
int isBST(struct node* node) {
236237
if (node==NULL) return(true);
@@ -240,7 +241,7 @@ int isBST(struct node* node) {
240241
// (bug -- an earlier version had min/max backwards here)
241242
if (node->left!=NULL && maxValue(node->left) > node->data)
242243
return(false);
243-
244+
244245
// false if the min of the right is <= than us
245246
if (node->right!=NULL && minValue(node->right) <= node->data)
246247
return(false);
@@ -253,19 +254,11 @@ int isBST(struct node* node) {
253254
return true;
254255
}
255256

256-
/*
257-
Returns true if the given tree is a binary search tree
258-
(efficient version).
259-
*/
260-
int isBST2(struct node* node) {
261-
return(isBSTRecur(node, INT_MIN, INT_MAX));
262-
}
263-
264257
/*
265258
Returns true if the given tree is a BST and its
266259
values are >= min and <= max.
267260
*/
268-
int isBSTRecur(struct node* node, int min, int max) {
261+
int isBSTRecur(struct node* node, int min, int max) {
269262
if (node==NULL) return(true);
270263
if (min > node->data)
271264
return(false);
@@ -276,4 +269,11 @@ int isBSTRecur(struct node* node, int min, int max) {
276269
// false if, recursively, the left or right is not a BST
277270
return (isBSTRecur(node->left, min, node->data) &&
278271
isBSTRecur(node->right, node->data+1, max)); // note the +1
279-
}
272+
}
273+
/*
274+
Returns true if the given tree is a binary search tree
275+
(efficient version).
276+
*/
277+
int isBST2(struct node* node) {
278+
return(isBSTRecur(node, INT_MIN, INT_MAX));
279+
}

0 commit comments

Comments
 (0)