1
11
21
1211
111221
312211
13112221
1113213211
char* get_next_line(char* line)
{
char next_line[4096];
int next_line_ndx = 0;
char letter = line[0];
int num_letters = 0;
for (int i = 0; i < strlen(line); ++i)
{
if (letter != line[i+1])
{
next_line[next_line_ndx] = num_letters + '0';
next_line_ndx++;
next_line[next_line_ndx] = letter;
next_line_ndx++;
next_line[next_line_ndx] = 0;
letter = line[i+1];
num_letters = 0;
}
else
{
num_letters++;
}
}
return next_line;
}
Tuesday, August 4, 2009
Wednesday, July 29, 2009
breadth first search of binary tree (w/o the formatting)
7
/ \
1 2
/ \ /
0 3 4
\
2
7
1 2
0 3 4
2
def traverse_tree(node):
q = []
last_level = 0
current_level = 0
q.append([node, current_level])
while len(q) > 0:
node, current_level = q.pop()
if current_level != last_level:
last_level = current_level
print "\n"
print node + " "
left_child = node.left
right_child = node.right
if left_child not NULL:
q.append([left_child, current_level + 1])
if right_child not NULL:
q.append([right_child, current_level + 1])
/ \
1 2
/ \ /
0 3 4
\
2
7
1 2
0 3 4
2
def traverse_tree(node):
q = []
last_level = 0
current_level = 0
q.append([node, current_level])
while len(q) > 0:
node, current_level = q.pop()
if current_level != last_level:
last_level = current_level
print "\n"
print node + " "
left_child = node.left
right_child = node.right
if left_child not NULL:
q.append([left_child, current_level + 1])
if right_child not NULL:
q.append([right_child, current_level + 1])
Subscribe to:
Posts (Atom)