sreeram
BAN USERthis prints combinations of size 3 and can be generalized ...
{
#include<iostream>
#include<string>
using namespace std;
void combination(string a,string dest){
int l = dest.length();
if(a.empty() && l == 3 ){
cout<<dest<<endl;}
else{
if(!a.empty() && dest.length() < 3 ){
combination(a.substr(1,a.length()),dest+a[0]);}
if(!a.empty() && dest.length() <= 3 ){
combination(a.substr(1,a.length()),dest);}
}
}
int main(){
string demo("abcd");
combination(demo,"");
return 0;
}
}
- sreeram January 30, 2013if i correctly understand your question this is nothing but traversal of the tree
u can use either BFS or DFS
lets say its n- ary tree
the structure will be something like
{struct tree {
int data ;
struct tree *children[n]; //array of pointers to n children (they may be nulls also )
}
now use a queue for BFS
while dequeuing a node in the queue
traverse all the nodes like
for(i=0;i<n;i++)
if(!current ->children[i])
enqueue(current ->children[i])
}
hope its clear ...
int rotatedindex(int *arr,int start,int end)
{
int mid=(start+end)/2;
if(mid ==n-1 || arr[mid] > arr[mid+1])
return mid ;
else if(arr[start] < arr[mid])
return rotatedindex(arr,mid+1,end);
else
return rotatedindex(arr,start,mid-1)
}
this doesnt handle duplicates ....
{
#include<stdio.h>
int main()
{
int n=703;
char arr[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
while(n){
printf("%c ",arr[(n)%26]);
n=(n)/26;
}
return 0;
}
}
guys is this as simple as this or am i missing something .... of course the above program prints the required atring in reverse we can avoid that by using recursion or store it in a string to reverse it ...
i do agree...its costly ...i actually meant o(1) space implementation ..
there is no point if u try to use a stack or queue ...
because recursion anyway takes O(n) space on the stack
just check this function ...which converts the tree to list
{
struct node * converttolist(struct node *root)
{
if(root ==NULL)
return NULL;
struct node *realhead=root;
struct node *head=root;
while(head->left)
head=head->left;
while(root)
{
if(root->right){
root=root->right;
while(root){
head->left=root;
head=head->left;
root=root->left);
}
}
root=root->left;
}
return realhead;
}
}
now that i got a list i can just traverse the list to delete tree
Assuming its a normal tree without parent pointers ...........
}
- sreeram February 15, 2013