예제 #1
0
int main()
{
//    LinkedList<int> *l = new LinkedList<int>;
//    l->prependElement(3);
//    l->prependElement(5);
//    l->prependElement(6);
//    l->insertElement(5, new LinkedListElement<int>(12));
//    l->print();
//    l->deleteElement(5);
//    l->print();
//    std::cout << "============" << std::endl;
//    RBTree<int> t(5);
    RBTree<int> t;
    srand (1);
    for (int i = 0; i < 18; i++) {
//        t.insert(rand());
        t.insert(i);
    }
//    t.insert(40);
//    t.insert(60);
//    t.insert(-10);
//    t.insert(2);
//    t.insert(91);
//    t.insert(4);
//    t.insert(6);
//    t.insert(18);
//    t.insert(17);
//    t.insert(50);
    printPretty(&t, 1, 0, std::cout);
//    std::cout << t.min()->data() << std::endl;
//    std::cout << t.max()->data() << std::endl;
    t.deleteNode(t.find(11));
    printPretty(&t, 1, 0, std::cout);
//    t.print();
//    t.deleteNode(t.find(40), 0);
//    std::cout << "==" << std::endl;
//    t.print();

    return 0;
}
예제 #2
0
int printTree(TreeNode* root) {
	cout << "Tree pretty print with level=1 and indentSpace=0\n\n";
	// Output to console
	//printPretty(root, 1, 0, cout);

	cout << "\n\nTree pretty print with level=5 and indentSpace=3,\noutput to file \"tree_pretty.txt\".\n\n";
	//// Create a file and output to that file
	ofstream fout("tree_pretty.txt");
	//// Now print a tree that's more spread out to the file
	printPretty(root, 5, 0, fout);

	return 0;
}
void handleIO() {
#ifdef FILE_IO
    std::string problem = "rscn";
    std::ifstream inFile(problem + "_in.txt");
    std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
    std::cin.rdbuf(inFile.rdbuf()); //redirect std::cin to inFile!

    std::ofstream outFile(problem + "_out.txt");
    std::streambuf *coutbuf = std::cout.rdbuf();
    std::cout.rdbuf(outFile.rdbuf());
#endif

    Node* create_binary_tree_from_data(void);
    std::vector<int> find_vertical_sum(Node* node);
    void printPretty(Node *root, int level, int indentSpace, std::ostream& out);

    // Create a Binary Tree Manually
    Node* bt_root = create_binary_tree_from_data();
    // binary_tree_postorder(bt_root);
    std::cout << "Binary tree created," << std::endl;
    printPretty(bt_root, 1, 2, std::cout);

    // Print the Binary Tree
    std::cout << std::endl << "Computed vertical sums are," << std::endl;
    // Remove nodes that have single child
    for (auto sum : find_vertical_sum(bt_root))
        std::cout << " " << sum;
    std::cout << std::endl;
        


#ifdef FILE_IO
    std::cin.rdbuf(cinbuf);
    inFile.close();
    std::cout.rdbuf(coutbuf);
    outFile.close();
#endif
}
예제 #4
0
파일: klammern.c 프로젝트: cryptix/Gruppe42
int main(void){
	/* Deklariert a, b, c, d und e vom Typ short int,
	 * werden fuer die Klammerkombinationen deklarieren 
	 */
	short int a,b,c,d,e;

	/* Deklaration des short int Arrays 'perms', mit PERMCNT Elementen,
	 * wird fuer die Ergebnisse der Terme verwendet.
	 */
	short int perms[PERMCNT];
	
	/* Deklariert und Definiert 'treffer' vom Typ short int,
	 * wird fuer die Anzahl der unterschiedlichen Termwerte verwendet.
	 * Am Anfang noch nie, also 0
	 */
	short int treffer = 0;
	
	/* Deklariert und Definiert 'rand' vom Typ short int,
	 * wird verwendet um unterschiedliche Belegung 
	 * fuer die Variablen a bis f zu liefern.
	 */
	short int rand = notRandom(500);

	/* Die Hauptschleife wird mit a,b,c,d und e = -GRENZE initialisiert,
	 * laeuft solange a kleiner ist als die Grenze,
	 * und in jedem Durchgang wird e inkrementiert.
	 * Wenn e die Grenze erreicht, wird d inkrementiert.
	 * Wenn d die Grenze erreicht, wird c inkrementiert. usw.
	 */
	for(a=b=c=d=e=-GRENZE; a<GRENZE; e++) {
		if(e==GRENZE){ e=-GRENZE; d++; }	   
		if(c==GRENZE){ c=-GRENZE; b++; }	   
		if(d==GRENZE){ d=-GRENZE; c++; }      
		if(b==GRENZE){ b=-GRENZE; a++; }      

		/* Um zu verhindern, dass durch 0 geteilt wird */
		if(d==e || d ==0){e++; continue;} 

		/* a bis e sind zugewiesen,
		 * jetzt wird in jedem durchgang der Wert der Terme ausgerechnet 
		 * und im Array gespeichert
		 */
		perms[0] = a*b+c/d-e;
		perms[1] = (a*b+c)/d-e;
		perms[2] = (a*b+c)/(d-e);
		perms[3] = (a*(b+c))/(d-e);
		perms[4] = a*(b+c)/d-e;
        perms[5] = a*(b+c/d)-e;

		/* Dieser Block ueberprueft ob die ergebnisse sich unterscheiden
		 * und gibt eine mehr oder weniger zufaellige Kombination der Variablen aus
		 */
		if(noEquals(perms)==0 && rand == treffer++) {
			/* Saubere Ausgabe der Permutationen und der Ergebnisse */
			printPretty(a,b,c,d,e,perms); 

			/* Das Programm nach der ersten Ausgabe beenden */
			return 0;
		}
	}

	return 0;
} /* main */
예제 #5
0
// use the printPretty helper to make the tree look nice
void BinarySearchTree::print_tree() {
	printPretty(root, 1, 0, std::cout);
}