Ejemplo n.º 1
0
int main() {
    /*
     *        5
     *      /   \
     *     2     7
     *    / \   / \
     *   1   4  6  8
     *      /
     *     3
     */

    TreeNode *root = new TreeNode(5);
    root->left = new TreeNode(2);
    root->left->left = new TreeNode(1);
    root->left->right = new TreeNode(4);
    root->left->right->left = new TreeNode(3);

    root->right = new TreeNode(7);
    root->right->left = new TreeNode(6);
    root->right->right = new TreeNode(8);

    BSTIterator i = BSTIterator(root);
    while (i.hasNext()) cout << i.next();
    cout << endl;

    return 0;
}
Ejemplo n.º 2
0
/**
 * Your BSTIterator will be called like this:
 * BSTIterator i = BSTIterator(root);
 * while (i.hasNext()) cout << i.next();
 */
int main() {
    vector<int> nums;
    for (int i = 0; i < 10; ++i) nums.push_back(i);
    BSTConstructor constructor;
    TreeNode *root = constructor.sortedArrayToBST(nums);
    BSTIterator i = BSTIterator(root);
    while (i.hasNext()) cout << i.next() << endl;
    constructor.free(root);
    return 0;
}
Ejemplo n.º 3
0
int main(){
	Node tree;
	CreatBFT(&tree);
	PreOrder(tree);
	cout<<endl;
	BSTIterator bst = BSTIterator(tree);
	cout<<bst.hasNext()<<endl;
	cout<<bst.next()<<endl;
	
	return 0;
}
Ejemplo n.º 4
0
int main() {
    TreeNode *root = NULL;
    for (int i = 0; i < 10; i++) {
        root = insert(root, i);
    }
    BSTIterator i = BSTIterator(root);
    while (i.hasNext()) {
        cout << i.next() << endl;
    }
    return 0;
}
int main()
{
    //BSTIterator s1;
    TreeNode *root = constructTree(vector<int>() = {5, 3, 8, 1, 4, 6, 9, -1, 2, -1, -1, -1, 7});
    showTree(root);
    BSTIterator i = BSTIterator(root);
    while (i.hasNext())
        cout << i.next() << ends;
    cout << endl;
    return 0;
}
TEST(BinarySearchTreeIteratorTestCase, Normal) {
    int test_tree[] = {4,2,6,1,3,5,7};
    vector<int> tree(test_tree, test_tree + sizeof(test_tree)/sizeof(int));

    TreeNode* root = generate_binary_tree(tree);
    binary_tree_print(root);

    std::cout << "BSTIterator:" << std::endl;

    BSTIterator i = BSTIterator(root);
    while(i.hasNext()) {
        std::cout << i.next() << std::endl;
    }
}
Ejemplo n.º 7
0
	void Crawler::addWords(BST < Pair < string,int > >* newOccurrences, string url){
		BSTIterator<Pair <string,int> > iter = newOccurrences->Iterator();
		BSTNode<Pair<string,int> > newNode(Pair<string,int>("",-1));
		BSTNode<Word>* oldNode;
		Occurrence occ;
		occ.setURL(url);
		while(iter.hasNext()){
			newNode = iter.next();
			//is either a new node or an old node
			oldNode = words->Insert(Word(newNode.GetValue().getFirst()));
			occ.setOccurrences(newNode.GetValue().getSecond());
			oldNode->GetValue().addOccurrence(occ);
		}
	}
Ejemplo n.º 8
0
int main()
{

	int value[15] = { 8, 4, 2, -1, -1, 7, -1, -1, 12, 11, -1, -1, 15, -1, -1 };
	struct TreeNode* root = NULL;
	int index = 0;
	int nums = 15;
	root = Node(root, &index, value, nums);

	BSTIterator *mine = new BSTIterator(root);

	while (mine->hasNext())
		cout << mine->next() << endl;
	cout << endl;
	return 0;
}
int main()
{
	vector<TreeNode*> nodes;
	for (int i = 0; i < 7; ++i) {
		nodes.push_back(new TreeNode(i));
	}
	nodes[1]->left = nodes[0];
	nodes[1]->right = nodes[3];
	nodes[3]->left = nodes[2];
	nodes[4]->left = nodes[1];
	nodes[4]->right = nodes[5];
	nodes[5]->right = nodes[6];
	BSTIterator it = BSTIterator(nodes[4]);
	while (it.hasNext()) {
		cout << it.next() << ' ';
	}
	cout << endl;
	return 0;
}
int main(int argc, const char *argv[]) {
    auto test = [](std::initializer_list<int> li, std::initializer_list<int> expected_li) {
        TreeNode *root = CreateTree(li);
        std::vector<int> result;
        std::vector<int> expected(expected_li);
        BSTIterator it = BSTIterator(root);
        while (it.hasNext()) {
            result.push_back(it.next());
        }
        std::cout << "[";
        std::copy(result.begin(), result.end(), std::ostream_iterator<int>(std::cout, ", "));
        std::cout << "]" << std::endl;
        
        return result == expected;
    };
    
    assert(test({2, 1, 3}, {1, 2, 3}));
    assert(test({1}, {1}));
    
    return 0;
}