Exemplo n.º 1
0
void BinaryTree<T>::printPaths(const Node * a, vector<T> &v) const
{
 v.push_back(a->elem);
 if(a->left==NULL && a->right==NULL)
 {
    typename vector<T>::iterator it;
	cout << "Path:";
	for( it = v.begin(); it < v.end(); it++ )
	{
		
		cout <<" "<<*it;
	}
	cout << endl;
	return;
 }
 if(a->left!=NULL)
 {
	printPaths(a->left,v);
	v.pop_back();
 }
 if(a->right!=NULL)
 {
	printPaths(a->right,v);
    v.pop_back();
 }

  
}
Exemplo n.º 2
0
void BinaryTree<T>::printPaths(Node * subRoot, vector<T> printMe) const
{
	if(subRoot->left==NULL && subRoot->right==NULL)
		{
		cout << "Path: ";
		for(int i=0; i<printMe.size(); i++)
			{
			cout << printMe[i] << " ";
			}
		cout << endl;

		return;
		}
		
	if(subRoot->left!=NULL)
		{
		printMe.push_back((subRoot->left)->elem);
		printPaths(subRoot->left, printMe);
		printMe.pop_back();	
		}

	if(subRoot->right!=NULL)
		{
		printMe.push_back((subRoot->right)->elem);
		printPaths(subRoot->right, printMe);
		printMe.pop_back();	
		}
}
Exemplo n.º 3
0
void BinaryTree<T>::printPaths(const Node *sub_root, std::vector<T> vec) const
{
	// base case:
	if(sub_root == NULL)
	{
		return;
	}
	// resursive case:
	else
	{
		vec.push_back(sub_root->elem);
		if( (sub_root->left == NULL)&&(sub_root->right == NULL) )
		{
			cout << "Path:";
			for(int i = 0; i < vec.size(); i = i+1)
			{
				cout << " " << vec[i];
			}
			cout << endl;
		}
		else
		{
			printPaths(sub_root->left, vec);
			printPaths(sub_root->right, vec);
		}
	}
}	
Exemplo n.º 4
0
void BinaryTree<T>::printPaths(Node * subRoot, list<T> path) const
{	
	
	if(subRoot == NULL)
		return;

	if(subRoot->left == NULL && subRoot->right == NULL){
			path.push_back(subRoot->elem);
			size_t temp = path.size();
			cout << "Path:";
			for(size_t i = 0; i < temp; i++){
				cout<< ' ' << path.front();
				path.pop_front();	
			}
			cout<<endl;
			return;
		}
	else{
	
		path.push_back(subRoot->elem);
		printPaths(subRoot->left , path);	
		printPaths(subRoot->right , path);
		
	}
    // your code here
}
Exemplo n.º 5
0
Arquivo: DS.cpp Projeto: pjedson/PX
void printPaths(node *root, int path[], int plength) {
	if (root == NULL) {
		return ;
	}
	
	path[plength] = root->data;
	plength++;

	if (root->left == NULL && root->right == NULL) {
		printArray(path, plength);
	}
				
	printPaths(root->right, path, plength);
	printPaths(root->left, path, plength);
}
Exemplo n.º 6
0
int main() {
    auto tree = BinTree<int>();
    std::cout << "size: " << tree.size() << '\n';
    std:: cout<< "maxDepth: " << tree.maxDepth() << '\n';
    std::cout << "traverse in order: ";
    tree.traverse_inorder();
    std:: cout << '\n';

    tree.insert(2);
    tree.insert(1);
    tree.insert(3);

    std::cout << tree.root->value << '\n';
    std::cout << tree.root->left->value << '\n';
    std::cout << tree.root->right->value << '\n';
    std::cout << "lookup 1: " << tree.lookup(1) << '\n';
    std::cout << "lookup 7: " << tree.lookup(7) << '\n';
    std::cout << "size: " << tree.size() << '\n';
    std:: cout<< "maxDepth: " << tree.maxDepth() << '\n';
    tree.insert(5);
    tree.insert(7);
    std::cout << "size: " << tree.size() << '\n';
    std::cout << "maxDepth: " << tree.maxDepth() << '\n';

    std::cout << "traverse in order: ";
    tree.traverse_inorder();
    std:: cout << '\n';

    std::cout << "hasPathSum\n";
    std::cout << tree.hasPathSum(3) << '\n';
    std::cout << tree.hasPathSum(4) << '\n';
    std::cout << tree.hasPathSum(17) << '\n';

    tree.printPaths();
    tree.insert(4);
    std::cout << "after insert(4):\n";
    tree.printPaths();

    std::cout << "-------------\n";
    std::cout << "traverse in order: ";
    tree.traverse_inorder();
    std:: cout << '\n';
    tree.mirror();
    std::cout << "traverse in order: ";
    tree.traverse_inorder();
    std:: cout << '\n';

}
Exemplo n.º 7
0
/*Print All Paths from Root to Leaf - Karumanchi problem 20 (trees)*/
void printPaths(tree_node *root, int *paths, int level){
	paths[level] = root->data;
	if(root->left == NULL && root->right == NULL){
		int i = 0;
		printf("Path : ");
		for(; i <= level; i++){
			printf("%d\t", paths[i]);
		}
		printf("\n");
		return;
	}
	level++;
	printPaths(root->left, paths, level);
	printPaths(root->right, paths, level);
	return;
}
Exemplo n.º 8
0
void BST<ItemType>::printPaths()
{
    ItemType path[100];
    int l = 0;
    
    printPaths(root, path, l);
}
Exemplo n.º 9
0
void BinaryTree<T>::printPaths() const
{
	T* thePaths = new T[500];
    int theLength = 0;
    printPaths(theLength, root, thePaths);
    // your code here
}
Exemplo n.º 10
0
main()
{
	struct node *root = build20_5_156();
	printPaths(root, NULL);
	//printf("%s -4\n", hasPathsum(root, -4)? "has Pathsum:": "don't have Pathsum :");
	//printf("%s 3\n", hasPathsum(root, 3)? "has Pathsum:": "don't have Pathsum :");
}
Exemplo n.º 11
0
void BinaryTree<T>::printPaths(const Node * subRoot, vector<T> & v) const{
	if (subRoot == NULL)
		return;
	v.push_back(subRoot->elem);
	if(subRoot->left == NULL && subRoot->right == NULL){
		cout<<"Path:";
		for(int i = 0; i < (int)v.size(); i++)
			cout<<" "<<v[i];
		cout<<endl;
	}
	
	else{
		printPaths(subRoot->left,v);
		printPaths(subRoot->right,v);
	}
	v.pop_back();
}
Exemplo n.º 12
0
int main()
{
	struct node* root = NULL;
	root =  build12345();
	printf("no of nodes %d\n",size(root));
	printPaths(root);
	return 0;
}
Exemplo n.º 13
0
void printUtility(struct node *root)
{

int path[1000];
int pathlen =0;
 printPaths(root,path,pathlen);



}
Exemplo n.º 14
0
/*void printUtility(struct node *root)
{

int path[1000];
int pathlen =0;
 printPaths(root,path,pathlen);



} */
void printPaths(struct node *root,int path[], int pathlen)
{

if (root == NULL)
return ;
path[pathlen] = root->data;
pathlen = pathlen+1;

if(root->left == NULL && root->right == NULL)
printArray(path,pathlen);

else
{

printPaths(root->left,path,pathlen);
printPaths(root->right,path,pathlen);

}

}
void printPaths(struct node *node,int *path,int pathLen)
{
	int i;

	if(node == NULL){
		printf("\n");
		return;
	}
	else{
		*(path+pathLen) = node->data;
		pathLen++;

		if(node->left == NULL && node->right == NULL)
			for(i=0;i<pathLen;++i)
				printf("\t%d",path[i]);

		printPaths(node->left,path,pathLen);
		printPaths(node->right,path,pathLen);
	}
}
Exemplo n.º 16
0
void printAllPathsToLeaf(tree_node* root){
	if(root == NULL) return;
	printf("All Paths to Leaf\n");
	int height = height_of_tree(root);
	int *paths = (int *)malloc(sizeof(int)*height);
	printPaths(root, paths, 0);
	printf("Path with sum = 11 starting from root till a leaf exists ? %d\n",isPathWithSumExists(root, 11));
	PrintAllPathExists(root, paths, 0 ,4);
	free(paths);
	return;
}
Exemplo n.º 17
0
int printPaths(struct node *binaryTree, struct listnode *listhead)
{
	if(binaryTree == NULL){
		printf("empty tree\n");
		return;
	}
	PushOnList(&listhead, binaryTree->data);
        if(binaryTree->left == NULL && binaryTree->right == NULL){
		printf("path :");
                printlinkedlist(listhead);
		printf("\n");
		return;
	}
	if(binaryTree->left != NULL){
		printPaths(binaryTree->left, listhead);
	}
	if(binaryTree->right != NULL){
		printPaths(binaryTree->right, listhead);
	}
        return;
}
Exemplo n.º 18
0
void Circuit::getPaths(gate* g)//gets all paths in the circuit and prints them
{
    Paths.push_back(g); // push this gate into the vector
    if (Paths.size() > 1) PathDelay+=Paths[Paths.size()-2]->getDelay(); //add delay of previous gate
    for(std::map<std::string, wire*>::iterator i = wireMap.begin(); i != wireMap.end(); i++)
        if (i->second->getWSource()->getName() == g->getName())
            for (int j = 0; j < i->second->getWDestionations().size(); j++)
                getPaths(i->second->getWDestionations()[j]);
    if ((g->getIsFlip() && !g->getFlipIn()) || g->getType() == "output") //if the gate is an output flipflop or normal output
        printPaths(calcReqTime(g));
    Paths.pop_back();
    PathDelay-= g->getDelay();
}
Exemplo n.º 19
0
int main()
{
  struct node *root = newNode(10);
  root->left        = newNode(8);
  root->right       = newNode(2);
  root->left->left  = newNode(3);
  root->left->right = newNode(5);
  root->right->left = newNode(2);

  /* Print all root-to-leaf paths of the input tree */
  printPaths(root);
  return 0;
}
void testPrintPaths() {

struct node* rootNode = insert(NULL,5);
rootNode = insert(rootNode,4);
rootNode = insert(rootNode,8);
rootNode = insert(rootNode,3);
rootNode = insert(rootNode,7);
rootNode = insert(rootNode,9);
rootNode = insert(rootNode,2);
rootNode = insert(rootNode,1);
printPaths(rootNode);

}
Exemplo n.º 21
0
void printPaths(node *n, int *a, int l)
{
  if(n -> left == NULL && n -> right == NULL)
    {
      int i;
      a = realloc(a, ++l * sizeof(int));
      *(a + l - 1) = n -> data;

      printf("\n");
      
      for(i = 0;i < l;i++)
	printf("%d ", a[i]);

      return;
    }
  else
    {
      a = realloc(a, ++l * sizeof(int));
      *(a + l - 1) = n -> data;
      printPaths(n -> left, a, l);
      printPaths(n -> right, a, l);
    }
}
Exemplo n.º 22
0
Arquivo: DS.cpp Projeto: pjedson/PX
void printTreeinfo(node *root) {
	int paths[102];
	printf("Tree size = %d\n", size(root));
	printf("Tree max Depth = %d\n", maxDepth(root));
//	printf("Tree min Valuse = %d\n", bst_minValue(root));
//	printf("Tree max Valuse = %d\n", bst_maxValue(root));
	printf("Tree min Valuse = %d\n", minValue(root));
	printf("Tree max Valuse = %d\n", maxValue(root));

	printf("Has Path sum 40  = %d\n", hasPathSum(root, 40));
	printf("Has Path sum 20  = %d\n", hasPathSum(root, 20));
	printPaths(root, paths, 0);

}
Exemplo n.º 23
0
int main()
{
  node *root = NULL, *rootb = NULL, *rooth = NULL, *temp;
  int sum = 25, *a, l = 0;
  a = (int *)malloc(sizeof(int));

  root = insert(root, 10);
  root = insert(root, 7);
  root = insert(root, 6);
  root = insert(root, 8);
  root = insert(root, 13);
  root = insert(root, 15);
  root = insert(root, 11);

  printf("Part2: Size: \t%d\n", size(root, 0));
  printf("Part3: Depth: \t%d\n", maxDepth(root));
  printf("Part4: Min: \t%d\n", minValue(root));
  printf("Part5: Post: \t");printPostorder(root);printf("\n");
  printf("Part6: Path Sum: %d %s\n", sum, hasPathSum(root, sum)?"True":"False");
  printf("Part7: Leaf Paths");printPaths(root, a, l);
  printf("\n");
  inorder(root);printf("\n");
  printf("Part8: Mirror: ");mirror(root);inorder(root);printf("\n");

  rootb = insert(rootb, 10);
  rootb = insert(rootb, 7);
  rootb = insert(rootb, 6);
  rootb = insert(rootb, 8);
  rootb = insert(rootb, 13);
  rootb = insert(rootb, 15);
  rootb = insert(rootb, 11);

  printf("Part9: doubleTree: ");doubleTree(rootb);inorder(rootb);printf("\n");
  printf("Part10: sameTree: %s\n", sameTree(root, root)?"True":"False");
  mirror(root);
  printf("Part11: isBST: %s\n", isBST(root)?"True":"False");
  printf("Part12: Tree list: ");
  rooth = treeList(root);
  temp = rooth -> left;
  printf("%d ", rooth -> data);
  while(temp != rooth)
    {
      printf("%d ", temp -> data);
      temp = temp -> left;
    }
  printf("\n");

  return(0);
}
Exemplo n.º 24
0
void BinaryTree<T>::printPaths(int length, Node* subRoot, T* &paths) const {

	if (subRoot == NULL) return;

	paths[length] = subRoot->elem;

	length = length+1;

	if (subRoot->left== NULL && subRoot->right == NULL) { 
		cout << "Path: ";


		for (int i = 0; i < length; i++) {
			cout<<paths[i]<< " ";

		}
		cout <<endl;
	}

	printPaths(length, subRoot->left, paths);
	printPaths(length, subRoot->right, paths);


}
Exemplo n.º 25
0
void BST<ItemType>::printPaths(TreeNode<ItemType> *node, ItemType path[], int pathLength)
{
    if(root == NULL)
    {
        cout << "Tree is empty" << endl;
        return;
    }
    
    else if(node == NULL) return;
    
    path[pathLength] = node->element;
    pathLength++;
    
    if(node->left == NULL && node->right == NULL)
    {
        printArray(path, pathLength);
    }
    
    else
    {
        printPaths(node->left, path, pathLength);
        printPaths(node->right, path, pathLength);
    }
}
main()
{
	int sum=3;
	int pathLen=0;
	int path[100];
	struct node *root;

	root=build();

	printf("\n\n");
	print(root);
	printf("\n\n");

	printf("root-to-leaf paths are :\n\n");
	printPaths(root,path,pathLen);
	printf("\n\n");

}
Exemplo n.º 27
0
/* Driver program to test above functions*/
int main()
{ 
  
  /* Constructed binary tree is
            10
          /   \
        8      2
      /  \    /
    3     5  2
  */
  struct node *root = newnode(10);
  root->left        = newnode(8);
  root->right       = newnode(2);
  root->left->left  = newnode(3);
 
  
  printPaths(root);
  
  return 0;
}
Exemplo n.º 28
0
int main() {
    struct node* root = NewNode(4);
    insert(root, 2);
    insert(root, 5);
    insert(root, 1);
    insert(root, 3);

    printf("size of tree: %d\n", size(root));
    printf("maxDepth of tree: %d\n", maxDepth(root));
    printf("minValue of tree: %d\n", minValue(root));

    printTree(root);
    printf("\n");

    printPostorder(root);
    printf("\n");

    if (hasPathSum(root, 9)) printf("Tree has a root-to-leaf path that sums to 9\n");
    printPaths(root);

    return 0;
}
Exemplo n.º 29
0
/* Driver program to test above functions*/
int main()
{
  
  /* Constructed binary tree is
            10
          /   \
        8      2
      /  \    /
    3     5  2
  */
  struct node *root = newnode(10);
  root->left        = newnode(8);
  root->right       = newnode(2);
  root->left->left  = newnode(3);
  root->left->right = newnode(5);
  root->right->left = newnode(2);
  int sum=0;
  printPaths(root, &sum);
  printf("Total Sum is %d\n",sum);
  printf("Total sum is %d\n",Sum(root, 0));
  getchar();
  return 0;
}
Exemplo n.º 30
0
void bellmanFord(int startNode)
{
    printf("\nBellman Ford:");
    int *distance = (int*)malloc(sizeof(int)*nrOfVerteces);
    int *parents = (int*)malloc(sizeof(int)*nrOfVerteces);

    int i;

    for(i=0; i<nrOfVerteces; i++)
    {
        if(i == startNode)
        {
            distance[i] = 0;
            parents[i] = i;
        }
        else
        {
            distance[i] = MAX;
            parents[i] = i;
        }

    }

    int x, y;

    for(i=0; i < nrOfVerteces-1; i++)
        for(x=0; x<nrOfVerteces; x++)
            for(y=0; y<nrOfVerteces; y++)
                if(x != y && adjMatrix[x][y] != 0)
                    if(distance[x] + adjMatrix[x][y] < distance[y])
                    {
                        distance[y] = distance[x] + adjMatrix[x][y];
                        parents[y] = x;
                    }

    printPaths(parents, distance, startNode);
}