int main()
{
	BinarySearchTree *tree = new BinarySearchTree();

	tree->insertNode( 5 );
	tree->insertNode( 6 );
	tree->insertNode( 2 );
	tree->insertNode( 8 );

	tree->printPr();
	cout << endl;

	tree->printSim();
	cout << endl;

	tree->printObr();
	cout << endl;

	tree->deleteNode( tree->searchNodeByKey(5) );

	tree->printPr();
	cout << endl;

	system("pause");
	return 0;
}
int main()
{
    BinarySearchTree bTree;
    Node* bTreeRoot = nullptr;
    bTree.insertNode(bTreeRoot,12);
    bTree.insertNode(bTreeRoot,7);
    bTree.insertNode(bTreeRoot,22);
    bTree.insertNode(bTreeRoot,4);
    bTree.insertNode(bTreeRoot,9);
    bTree.insertNode(bTreeRoot,15);
    bTree.insertNode(bTreeRoot,29);
    bTree.insertNode(bTreeRoot,1);
    bTree.insertNode(bTreeRoot,5);
    bTree.insertNode(bTreeRoot,8);
    bTree.insertNode(bTreeRoot,11);
    bTree.insertNode(bTreeRoot,13);
    bTree.insertNode(bTreeRoot,18);
    bTree.insertNode(bTreeRoot,24);
    bTree.insertNode(bTreeRoot,31);
    //bTree.inorder(bTreeRoot);
    bTree.setRoot(bTreeRoot);
    cout<< "The ancestor is " << bTree.findLCA(13,29)<<endl;
    cout<< "The distance between nodes is " << bTree.computeDistance(13,29)<<endl;
    return 0;
}
示例#3
0
int main() {
    BinarySearchTree<int, int> bst;
    enum {QUIT, INSERT, SEARCH, INORDER, PREORDER, POSTORDER, SUCCESSOR, PREDECESSOR, DELETE, GETBYINDEX};
    int option = -1;
    int el;
    while (option != QUIT) {
        cout << "What you want to do now?"<<"\n";
        cout <<"0. QUIT 1. INSERT 2.SEARCH 3. INORDER 4. PREORDER 5.POSRORDER 6.SUCCESSOR 7.PREDECESSOR 8.DELETE 9.GETBYINDEX\n";
        cin >> option;
        switch (option) {
            case INSERT:    // 1
                {
                    int T;
                    cout << "Number of item to insert" << "\n";
                    cin >> T;
                    while (T-- > 0) {
                        cin >> el;
                        bst.insertNode(el, el);
                    }
                    break;
                }
            case SEARCH: // 2
                {
                    cout << "Value you want to search?" << "\n";
                    cin >> el;
                    Node<int,int>* node = bst.searchNode(el);
                    cout << "Searched node is " << node->getValue() << "\n";
                    break;
                }
            case INORDER: // 3
                cout << "Doing inorder traversal"<<"\n";
                bst.inOrder();
                cout << "\n";
                break;
            case PREORDER: // 4
                cout << "Doing preorder traversal"<<"\n";
                bst.preOrder();
                cout << "\n";
                break;
            case POSTORDER: // 5
                cout << "Doing postorder traversal"<<"\n";
                bst.postOrder();
                cout << "\n";
                break;
            case SUCCESSOR: // 6
                {
                    cout << "Whose successor are you looking for?"<<"\n";
                    cin >> el;
                    Node<int,int>* node = bst.successor(el);
                    cout << "Successor to node " << el << " is " << node->getValue() << "\n";
                    break;
                }
            case PREDECESSOR: // 7
                {
                    cout << "Whose predecessor are you looking for?"<<"\n";
                    cin >> el;
                    Node<int,int>* node = bst.predecessor(el);
                    cout << "Predecessor to node " << el << " is " << node->getValue() << "\n";
                    break;
                }
            case DELETE: // 8
                cout << "Which element you want to delete?"<<"\n";
                cin >> el;
                bst.deleteNode(el);
                break;
            case GETBYINDEX: // 9
                {
                    cout << "Enter Index of the item you are looking for?"<<"\n";
                    cin >> el;
                    Node<int,int>* node = bst.getNodeByIndex(el);
                    cout << "Item found is " <<node->getValue() << "\n";
                    break;
                }
            case QUIT: // 0
                cout<<"GoodBye!";
                return 0;
        }
    }
    return 0;
}