本文整理汇总了C++中BST::deleteNode方法的典型用法代码示例。如果您正苦于以下问题:C++ BST::deleteNode方法的具体用法?C++ BST::deleteNode怎么用?C++ BST::deleteNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BST
的用法示例。
在下文中一共展示了BST::deleteNode方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
BST<int> tree;
tree.insertNode(5);
tree.insertNode(7);
tree.insertNode(18);
tree.insertNode(1);
tree.insertNode(10);
tree.insertNode(30);
tree.insertNode(5);
tree.traverseTree(tree.root);
tree.deleteNode(5);
tree.deleteNode(5);
tree.deleteNode(30);
tree.traverseTree(tree.root);
tree.insertNode(2);
tree.insertNode(20);
tree.traverseTree(tree.root);
Node<int>* toDel = tree.findNode(2);
tree.deleteNode(toDel);
tree.traverseTree(tree.root);
return 0;
}
示例2: main
// Driver
int main()
{
/* BST yang akan dibuat
50
/ \
30 70
/ \ / \
20 40 60 80 */
// Creating BST
BST B;
// Inserting
B.root = B.insert(B.root, 50);
B.root = B.insert(B.root, 30);
B.root = B.insert(B.root, 20);
B.root = B.insert(B.root, 40);
B.root = B.insert(B.root, 70);
B.root = B.insert(B.root, 60);
B.root = B.insert(B.root, 80);
// Inorder Traverse
B.inorder(B.root);
// Deleting
B.root = B.deleteNode(B.root, 20);
cout << "Result" << endl;
B.inorder(B.root);
cout << "\nDelete 30\n";
B.root = B.deleteNode(B.root, 30);
B.inorder(B.root);
cout << "\nDelete 50\n";
B.root = B.deleteNode(B.root, 50);
B.inorder(B.root);
// Searching
tree_node *temp = B.search(B.root, 80);
if(temp == NULL)
cout << "\nKEY NOT_FOUND" << endl;
else
cout << "\nKEY FOUND: " << temp->key << endl;
return 0;
}
示例3: main
int main(){
BST<int> ex;
int fx[]={6,3,9,2,1,4,5,8,10};
for(int i=0;i<sizeof(fx)/4;i++)
ex.addNode(fx[i]);
ex.display(2);
cout<<endl;
ex.deleteNode(10);
ex.display(2);
cout<<endl;
ex.deleteNode(2);
ex.display(2);
cout<<endl;
ex.deleteNode(4);
ex.display(2);
cout<<endl;
ex.deleteNode(6);
ex.display(2);
}
示例4: main
int main(){
BST tree;
//test 1
tree.insert(5,"ahmed");
tree.insert(7,"daramally");
tree.inOrder();
cout << "Current H :" << tree.getMaxHeight() << "\n";
cout << "\ntest 1 is successful!\n\n";
//test 2
tree.insert(1,"omar");
tree.inOrder();
cout << "Current H :" << tree.getMaxHeight() << "\n";
cout << "\ntest 2 is successful!\n\n";
//test 3
tree.set(7,"non-daramally");
cout << "Current H :" << tree.getMaxHeight() << "\n";
tree.inOrder();
cout << "\ntest 3 is successful!\n\n";
//test 4
tree.insert(2,"mostafa");
tree.insert(3,"todo");
tree.inOrder();
cout << "Current H :" << tree.getMaxHeight() << "\n";
cout << "\ntest 4 is successful!\n\n";
//test 5
tree.modify(3,"darsh");
cout << "Value after edit : " << tree.lookUp(3) << "\n";
tree.inOrder();
cout << "\ntest 5 is successful!\n\n";
//test 6
cout << "before delete\nContains key? : " << tree.containsKey(3) << "\n";
tree.deleteNode(3);
cout << "after delete\nContains key? : " << tree.containsKey(3) << "\n";
tree.inOrder();
cout << "Current H :" << tree.getMaxHeight() << "\n";
cout << "\ntest 6 is successful!\n\n";
//test 7
cout << "Num of Nodes : " << tree.countNodes() << "\n";
tree.displayMin();
tree.displayMax();
puts("");
tree.preOrder();
puts("");
tree.postOrder();
cout << "\ntest 7 is successful!\n\n";
}
示例5: main
int main()
{
BST obj;
Node * newPtr = NULL,*k =NULL;
int data, flag=1,j;
while(flag)
{
cout<<"Menu\n1. Insert\n2. IN-ORDER Traversal\n3. PRE-ORDER Traversal\n4. POST-ORDER Traversal\n5. Search\n6. Find Min & Max\n7. Delete\n8. Display\n9. Exit\n";
int x;
cin>>x;
switch(x)
{
case 1:
cout<<"Enter Element to be added to TREE: ";
cin>>data;
newPtr = obj.createNewNode(data);
if(newPtr==NULL)
{
cout<<"Cannot create node!! ABORTING!!!\n";
system("pause");
exit(1);
}
obj.insert(newPtr);
break;
case 2:
cout<<"IN-ORDER Traversal:\n\n";
obj.inOrder(obj.getRoot());
break;
case 3:
cout<<"PRE-ORDER Traversal:\n\n";
obj.preOrder(obj.getRoot());
break;
case 4:
cout<<"POST-ORDER Traversal:\n\n";
obj.postOrder(obj.getRoot());
break;
case 5:
cout<<"Enter element to be searched for: ";
cin>>j;
k=obj.find(j);
if(k!=NULL)
cout<<k->getData()<<" is Found! \n";
else
cout<<"Node not found!!\n";
break;
case 6:
cout<<"\nMINIMUM = "<<obj.findMin(obj.getRoot())->getData();
cout<<"\nMAXIMUM = "<<obj.findMax(obj.getRoot())->getData();
break;
case 7:
cout<<"Enter the node to be deleted from tree: ";
cin>>j;
obj.deleteNode(j);
cout<<"The BST is now:\n\n";
obj.display(obj.getRoot(),4);
break;
case 8:
cout<<"The BST is now:\n\n";
obj.display(obj.getRoot(),4);
break;
case 9:
exit(0);
}
cout<<"\nDo you want to continue? [yes -> 1, No -> 0]";
cin>>flag;
system("cls");
}
return 0;
}