C program to copy one binary tree to another [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Can any one help me with a C program to copy one binary tree to another binary tree?
I am okay with either algorithm or C functions so that I can implement them.
Thank you :)

If the tree you want added is a proper subset (no overlaps) and the tree doesn't have to be balanced, you can just append its root node to the correct insertion point. By that, I mean something like:
10
/ \
1 70
*
5
/ \
2 7
where you can create a link on the right of the 1 to attach to the 5. This will work because the entire sub-tree slots in between 1 and 10 (which is what I meant by "no overlaps").
In fact, if the tree also isn't even sorted, you can just attach the root node to any leaf that you want:
40
/ \
1 10
*
5
/ \
8 789
In that case, overlaps are not a concern since the tree is not sorted so it's a safe bet to assume you don't care about order.
However, if you have a balanced and sorted tree, your best bet is probably to traverse one of the trees and use the insert method to add each value found to the other tree.
That will both properly combine "overlapping" trees and keep the target tree balanced if necessary. So, using the first example above, you'd get something like:
7
/ \
/ \
2 10
/ \ \
1 5 70
The "algorithm" then becomes something like:
def copyTree (source, destination):
item = source.first()
while item != none_left:
destination.insert (item.value)
item = source.next (item)
That way, you don't have to worry about whether the attachment will be problematic because the insert method of the destination tree will automatically do the right thing (sort, balance, etc).

Related

How to plan out a theatre using labels [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
For a school project, we have to develop a theatre booking system, with a graphical representation of the theatre, which is labelled in a really annoying way. Our tutor said we should represent the theatre with a 2-d array of labels, but referencing each label to change the colour is tricky, say if the user booked seat 10,10 then that wouldn't be 10,10 in the array.
Does anyone know of any good methods of solving this? Because I am stumped.
Here is a link to the seating plan: http://i.stack.imgur.com/U14ut.png
I would suggest that you use an array of labels for each row. For example for row A create labels named lblRowA with indexes 1 to 14 and repeat for the other rows (having an array for each row). That should make it easy to map requests onto real world seating.
In addition to the 2-D array of labels, you can use two 2-D arrays of the same size, one for the row letter and one for number of each seat represented by the array of labels.
For example, for labels(4, 7) the seat number might be seatNumbers(4,7) and the row letter might be rowLetters(4,7).
If you know how (or can figure it out), you use one 2D array of a class or structure where each member contains the two values, and possibly reservation information, etc. In that case, you could address the seat information with something like seats(4,7).rowLetter, seats(4,7).seatNumber, and seats(4,7).reserved. You could also have a reference to the seat labels in the seats class.

Sorting with just one loop. Help me with its efficiency [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have came up with a new sorting algorithm with just one do while loop in it, but i don't
know how to calculate the efficiency of it in best,average and worst case so please help me in calculating it.
The loop starts with i=1 and the end condition for while loop is i<=n-2 and some times the value of i increases in the loop and some times i value will be decremented based on some condition.
I think i will better understand if u illustrate through simple examples.
please help me............
Thank you in advance for those who help me.....
some times i value will be decremented based on some condition
This vagueness makes it impossible to analyze. If the "condition" is always true and i is decremented to zero, then the loop runs forever. So based on what you say, the time complexity could be anything from Theta(n) to infinity.
The way to work out time complexity is to calculate (or put an upper bound on) the number of operations performed, as a function of n. "Operations" in the case of sorting usually means comparisons and copies/moves, but if your algorithm does anything unusual then of course that has to be included.

database design and algorithm for Public Transport? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
We want to implement Public Transport Guide for Android.
Inputs will be beginning and destination point. Outputs will be the directives that
tell how can be gone to the destination with using buses, subways,... e.c
This is not easy for big cities and we must have a well-designed database for quick answer. Tranport algorithm must give optimum lines to the passanger.
I wanna see your precious ideas for database and algorithm design.
Thank you very much for answers.
Most likely you will need a graph there to calculate shortest path.
Your graph will be G=(V,E) such that V = {all possible stations} and E = {(u,v) | there is a transport connecting station u to station v}.
If your graph cannot fit into memory [which is probably the case for a big city], you might want to make a function successors(u) = { v | (u,v) in E } which will allow you to calculate the graph on the fly.
In this older question there is some discussion how to efficiently find a path between two vertices in a dynamic environment similar to the one you are describing.

adding the middle 20 values of array 0-99 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
This may seem quiete easy to everyone else but I am struggling to comprehend it.
I have to add up the numbers held in the middle 20 indexes of an array 0-99
but I cant work out from what index to what index I should add.
Is it from 40-60? or is that 22 values?
if you are having trouble counting things in lists of consecutive integers that are not starting at one then just imagine subtract the lowest index from all of them and then add 1. Then you will have a lsit of consecutive integers starting from 1 so whatever the top index is will be the count of the thing.
So in the case of 40-60 you can subtract 40 from everything and add one to see that these indexes map to the numbers 1 to 21.
To get your correct answer a simple extension of this can be used. 0-99 is 100 values (just add one to all of them to see this). That means you need to skip 40 values and then take the next 20.
So to work out the ones you skip you want to start at 0 and then take 40 items. You can easily see that after adding one to the list you want your top index to be 40 (so your indexes map to 1-40) which means the actual indexes are 0-39. So 40 then starts your list and in a similar way you can say that if 40 is your first index (40-39=1) then your last index will be 59 (since 59-39 = 20).
Thus the answer is that you are lookign at indexes 40-59.
I hope this not only helps you get the answer here but helps you work out how to do it on your own.
index 40 - index 59 is your middle-range ;)

Maximal matching in a bipartite graph [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am stuck with a maximal matching in a bipartite graph problem. The problem goes something like this:
Given a board with m circular holes and given a set of n circular discs. Holes are numbered as h1, ..., hm, and discs as d1, ..., dn.
We have a matrix A of m rows and n columns. A[i][j] = 1 if hi can fit dj (i.e., diameter of hi ≥ diameter of dj), and 0 otherwise.
Given the condition that any hole can contain at most one disc, I need to find the configuration for which holedisc fitting is maximal.
I have read that this problem can be modelled into network flow problem, but could not exactly follow how. Can someone explain how to do this? Also, is there any C code for this that I might be able to look at?
The reduction from bipartite matching to maximum flow is actually quite beautiful. When you are given a bipartite graph, you can think of the graph as two columns of nodes connected by edges from the first column to the second:
A ----- 1
B --\ 2
C \- 3
... ...
Z n
To reduce the problem to max-flow, you begin by directing all of the edges from the first column to the second column so that flow can only move from the left column to the right. After you do this, you introduce two new nodes s and t that act as the source and terminal nodes. You position s so that it is connected to all of the nodes on the left side and t so that each node in the right side is connected to it. For example:
A ----- 1
/ B --\ 2 \
s- C \- 3 - t
\ ... ... /
Z n
The idea here is that any path you can take from s to t must enter one of the nodes in the left column, then cross some edge to the right column, and from there to t. Thus there is an easy one-to-one mapping from an edge in a matching and an s-t path: just take the path from s to the source of the edge, then follow the edge, then follow the edge from the endpoint to the node t. At this point, our goal is to find the way to maximize the number of node-disjoint paths from s to t. We can accomplish this using a maximum-flow as follows. First, set the capacity of each edge leaving s to be 1. This ensures that at most one unit of flow enters each of the nodes in the first column. Similarly, set the capacity of each edge crossing the two columns to be one, ensuing that we either pick the edge or don't, rather than possibly picking it with some multiplicity. Finally, set the capacity of the edges leaving the second column into t to be one as well. This ensures that each node in the right-hand side is only matched once, since we can't push more than one unit of flow past it.
Once you've constructed the flow network, compute a maximum flow using any of the standard algorithms. Ford-Fulkerson is a simple algorithm that performs well here, since the maximum flow in the graph is equal to the number of nodes. It has a worst-case performance of O(mn). Alternatively, the highly optimized Hopcroft-Karp algorithm can do this in O(m√n) time, which can be much better.
As for a C implementation, a quick Google search for the Ford-Fulkerson step turned up this link. You'd need to construct the flow network before passing it into this code, but the construction isn't too complex and I think that you shouldn't have much trouble with it.
Hope this helps!

Resources