acm-data Structure-two-fork search tree (c + + implementation)

Source: Internet
Author: User

Recently studied "computer algorithm", a little to understand the two-fork search tree.

/*@header bstree@abstract bstree@discussion insert,delete,search and Order@author walterbright,2016*/#include<iostream>using namespacestd;template<classType>classBstree;//template classes must be declared in advance as friend classesTemplate <classType>classtreenode{ Public: FriendclassBstree<type>;Private: TreeNode*Lchild; TreeNode*Rchild; Type data;}; Template<classType>classbstree{ Public: Bstree () {Tree=NULL; }    ~Bstree () {DeleteTree; } TreeNode<type>*Search (Type x); TreeNode<type>*ISearch (Type x); voidInsert (Type x); voidDelete (Type x); voidInorder ();//Middle Sequence Traversal    voidPreorder ();//First Order Traversal    voidPostorder ();//Post -order traversal (equivalent to a first-time reverse order in the search right)Private: TreeNode<Type> *Tree; TreeNode<type>* Search (treenode<type> *t,type x); voidDelete (treenode<type> *t,type x); voidVisit (treenode<type> *t); voidInorder (treenode<type> *t); voidPreorder (treenode<type> *t); voidPostorder (treenode<type> *t);}; Template<typename type>TreeNode<type>* bstree<type>:: Search (Type x) {returnSearch (tree,x);} Template<typename type>TreeNode<type>* Bstree<type>::search (treenode<type> *T,type x) {    BOOLFound=false;  while(t&&!found) {        if(x==t->data) {Found=true; }        Else if(x<t->data) {T=t->Lchild; }        Else{T=t->Rchild; }    }    if(found) {returnT; }    Else{        returnNULL; }}template<typename type>voidBstree<type>::visit (treenode<type> *t) {cout<<t->data<<' ';} Template<typename type>voidBstree<type>:: Insert (Type x) {BOOLFound=false; TreeNode<Type> *p=tree,*q;//Q is the parent node of P     while(p) && (!found)) {Q=p;//To make sure P goes down next .        if(x==p->data) {Found=true; }        Else if(x<p->data) {P=p->Lchild; }        Else{p=p->Rchild; }    }     if(!found) {P=NewTreenode<type>; P->lchild=p->rchild=NULL; P->data=x; if(tree) {if(x<q->data) {Q->lchild=p; }            Else{Q->rchild=p; }        }        Else{Tree=Q; }}}template<typename type>voidBstree<type>::D elete (Type x) {Delete (tree,x);} Template<typename type>voidBstree<type>::D elete (treenode<type> *T,type x) {    BOOLFound=false; TreeNode<Type> *p=t,*Q;  while(p) && (!found)) {        if(x==p->data) {Found=true;  Break; } q=p; if(x<p->data) {P=p->Lchild; }        Else{p=p->Rchild; }    }    if(found) {//can you save code by referencing a pointer?             if(P->lchild==null&&p->rchild==null) {//Remove leaves                if(x<=q->data) {Q->lchild=NULL; }                Else{Q->rchild=NULL; } P=NULL; }            Else if(P->lchild==null) {//Delete a node that contains only the right node                if(x<=q->data) {Q->lchild=p->Rchild; }                Else{Q->rchild=p->Rchild; } P=NULL; }            Else if(P->rchild==null) {//Delete a node that contains only the left node                if(x<=q->data) {Q->lchild=p->Lchild; }                Else{Q->rchild=p->Lchild; } P=NULL; }            Else{//Iterate Deleteq=p; P=p->Lchild;  while(p->rchild) {P=p->Rchild; } x= (Q->data) = (p->data); Q=q->Lchild;            Delete (Q,X); }}}template<typename type>voidBstree<type>:: Inorder () {inorder (tree); cout<<Endl;} Template<typename type>voidBstree<type>::P reorder () {preorder (tree); cout<<Endl;} Template<typename type>voidBstree<type>::P ostorder () {postorder (tree); cout<<Endl;} Template<typename type>voidBstree<type>::inorder (treenode<type> *t) {    if(t) {inorder (t-lchild);        Visit (t); Inorder (t-rchild); }}template<typename type>voidBstree<type>::P Reorder (treenode<type> *t) {    if(t) {Visit (t); Preorder (t-lchild); Preorder (t-rchild); }}template<typename type>voidBstree<type>::P ostorder (treenode<type> *t) {    if(t) {postorder (t-lchild); Postorder (t-rchild);    Visit (t); }}intMain () {Bstree<int>Bst1; Bst1. Insert ( the);/* the*/Bst1. Insert ( -);/*             /  \         */Bst1. Insert ( -);/* the*/Bst1. Insert ( -);/*           /  \           */Bst1. Insert ( -);/* the*/Bst1. Insert (Ten);/*         /  \             */Bst1. Insert ( +);/*Ten*/Bst1. Insert ( -);/*           /  \           */Bst1. Insert ( -);/* the*/Bst1.    Inorder (); Bst1.    Preorder (); Bst1.    Postorder (); cout<<Endl; BOOLS1=bst1. Search ( +);cout<<s1<<Endl; BOOLS2=bst1. Search ( -);cout<<s2<<Endl; Bst1.    Inorder (); Bst1. Delete ( -); Bst1.    Inorder (); return 0;}
View Code

acm-data Structure-two-fork search tree (c + + implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.