GitHub one-day algorithm problem: Search binary Tree Interface implementation of the cluster

Source: Internet
Author: User

Read, think, write code!

Description

(1) All the common operations of search binary tree are realized here.

(2) limited to time and effort, the implementation of the more coarse, memory leaks, member variable access control, return type, exception security, etc. did not take care of

(3) All the means of realization are close to the bottom operation, the principle of attention. Later may be to pull back, to achieve a complete interface system.

/********************************************** [email protected]* title: Two cross-tree interface implementations * Specific: two fork tree creation, insertion, maximum, minimum, Pre-order recursive traversal and non-recursive traversal, * search for binary tree recursive and non-recursive search, the deletion of two fork tree (involving precursor, back drive, subtree movement) * Analysis: According to the nature of the search binary tree, in addition to the deletion operation of the other operation is relatively straightforward and easy. About search Binary Tree deletion * Except operation, is to delete a node after how to maintain the nature of the search binary tree problem, more complex, the implementation part of the explanation ***********************************************/#include <iostream> #include <stack> #include <queue>using namespace std;/** create a two-fork search tree node type, a char data, three pointers (left child, Right child and Parent) * The parent pointer (parent) is used in the INSERT and delete operations of the binary search tree, the acquisition precursor and subsequent operations, and the binary tree traversal is less than */class binary_tree_node{public:/* All declared as public, convenient for direct access, Notice the initialization! */Binary_tree_node (char a):d ATA (a), Left_child (null), Right_child (null), parent (null) {} ~binary_tree_node () {} char    Data    Binary_tree_node *left_child;    Binary_tree_node *right_child; Binary_tree_node *parent;};/ * * Create a two-fork search tree type, a data member is a pointer to the root node * Create a two-fork search tree by inserting nodes continuously * The traversal method of the binary search tree is no different from the traversal of the ordinary binary tree */class binary_tree{public:/* Custom Constructor, Parameter is a two-fork search tree node number, note initialization */binary_tree (unsigned int size): root (NULL), _size (size) {} ~binary_tree () {}/* Binary search tree creation, call Insert_node () Insert node, according to binary search tree size characteristics Insert */void Create_binary_tree (); binary_tree_node* Get_root (); Gets the root node void Inorder_tree_walk (binary_tree_node* node);//recursive algorithm for sequential traversal void Insert_node (Binary_tree_node *node); Insert the node void Preorder_tree_walk (binary_tree_node* node) by comparing the size of the node key field, or the recursive algorithm for the pre-order traversal void Postorder_tree_walk (binary_tre e_node* node);//recursive algorithm for subsequent traversal void Inorder_tree_walk2 (binary_tree_node* node);//non-recursive algorithm for sequential traversal void Preorder_tree_walk2 (Bina ry_tree_node* node);//non-recursive algorithm for pre-sequence traversal void Postorder_tree_walk2 (binary_tree_node* node);//non-recursive algorithm for subsequent traversal void Levelorder_tree _walk (binary_tree_node* node);//print binary tree by layer//part ii*/binary_tree_node* search (char a);//search for an element, non-recursive binary_tree_no de* Search2 (binary_tree_node* node, char a);//recursively search for an element char Max ();//Find the largest element char min ();//Find the smallest element void tree_delete (Bin Ary_tree_node *node);p rotected:void Visit (binary_tree_node* node);//Print node binary_tree_node* tree_successor (binary_tr    Ee_node *node); binary_tree_node* TreE_presuccessor (Binary_tree_node *node); void transplant (Binary_tree_node *first,binary_tree_node *second);//two fork in the search tree move subtree Private:binary_tree_node *root;// root node pointer unsigned int _size;//Two forks the number of nodes in the search tree};/* gets the root node */binary_tree_node* binary_tree::get_root () {return root;} /* Print node keyword */void binary_tree::visit (binary_tree_node* node) {cout<<node->data<< "";} /* Find a node after the drive, in two cases to find, the second case is more abstract, referring to the introduction of the algorithm 164 page */binary_tree_node* binary_tree::tree_successor (Binary_tree_node *node        {if (node->right_child!=null)//The right subtree of the node is not empty, and the minimum node of the right subtree is the rear drive {node=node->right_child;        while (node->left_child!=null) {node = node->left_child;    } return node;        else//the right subtree of the node is empty, looking up the first node that is larger than it, until it encounters a node traced through the left subtree, which is more abstract {binary_tree_node* TMP = node->parent;            while ((Tmp!=null) && (node==tmp->right_child)) {node = tmp;        TMP = tmp->parent;    } return TMP; }}/* find the precursor of a node, and find the rear-drive symmetry */binary_tree_node* binary_tree::tree_presuccessor (Binary_tree_node *node) {if (node->left_child!=null)//        The left subtree of the node is not empty, and the maximum node of the Zuozi is the precursor {node=node->left_child;        while (node->right_child!=null) {node=node->right_child;    } return node;        The else//left dial hand tree is empty, looking up the first node to be compared to a small node, which is traced through the right subtree, {binary_tree_node* tmp = node->parent;            while ((Tmp!=null) && (node==tmp->left_child)) {node = tmp;        TMP = tmp->parent;    } return TMP;    }}/* two fork in the search tree to move the subtree, replacing the second subtree with first*/void binary_tree::transplant (Binary_tree_node *first, Binary_tree_node *second) {    if (first->parent==null) Root=second;    else if (first==first->parent->left_child) first->parent->left_child=second;    else first->parent->right_child=second; if (second!=null) second->parent=first->parent;} /* Insert a new node according to the key size * from the root node to compare, smaller than the root nodes into the left sub-tree, larger than the root node into the right subtree, gradually toward the leaf node direction to find the new node position * And according to the keyword sizeHang on the left or right, set three pointers */void Binary_tree::insert_node (binary_tree_node* node) {Binary_tree_node *tmp1 = root;    Binary_tree_node *tmp2 = NULL;        while (tmp1!=null) {tmp2=tmp1;        if (Node->data < Tmp1->data) tmp1=tmp1->left_child;    else tmp1=tmp1->right_child;    } node->parent=tmp2;    if (tmp2==null) Root=node;    else if (Node->data < Tmp2->data) tmp2->left_child=node; else Tmp2->right_child=node;} /* * Create a node each time you enter a character, and then insert the node into the leaf node of the two-fork search tree * If the root node does not exist, the new node is set to the root node */void Binary_tree::create_binary_tree () {for (unsigned        int i=0;i<_size;++i) {cout<< "input a Charoctor" <<endl;        char data;        cin>>data;        Binary_tree_node *node = new Binary_tree_node (data);    Insert_node (node);    Recursive algorithm for sequence traversal in}}/* * parameter is the root node * The order of the sequence traversal is (left subtree, root node, right subtree) */void Binary_tree::inorder_tree_walk (binary_tree_node* node) { if (node!=null) {Inorder_tree_walk (noDe->left_child);        Visit (node);    Inorder_tree_walk (Node->right_child); Non-recursive algorithm for}}/* sequence traversal * The parameter is the root node * using stack storage as the non-recursive method of the middle sequence traversal, first the root node into the stack, first traversing the Zuozi until the left dial hand tree is empty, out of the stack and print the node keywords. Then go to the right subtree of the stack node. * Right subtree is not empty, continue: the node into the stack, traversal left subtree-----------------The right node */void binary_tree::inorder_tree_walk2 (Binary_tree_node    * node) {stack<binary_tree_node*> _stack;    Binary_tree_node *tmp = node; while (null!=tmp | |!_stack.empty ())//node is not empty or stack is not empty, you can continue to traverse {if (null!=tmp) {_stack.push (TMP);//Node The stack tmp=tmp->left_child;//traverses the left subtree until the left dial hand tree is empty} else {tmp=_stack.top ();//left dial hand tree all over the leaf node.            , take out the top node of the stack and print, access the right child visit (TMP);//If the right child is not empty, repeat the above steps, first left and then root right, if the right child is empty, then out of the stack a node, and then access the right child _stack.pop ();        tmp=tmp->right_child; }}/* recursive algorithm for pre-sequence traversal * parameter is the root node * The order of the pre-order traversal is (root node, left dial hand tree, right subtree) */void Binary_tree::p reorder_tree_walk (binary_tree_node* node        {if (node!=null) {Visit (node);        Preorder_tree_walk (Node->left_child); preorder_tRee_walk (Node->right_child); }}/* * Pre-sequence traversal non-recursive algorithm * parameter is the root node * using a stack structure, root node into the stack, traversing the left subtree, node into the stack immediately print the keyword * until the left dial hand tree is empty, out of the stack at the same time to access its right child. If the right child is not empty, repeat the steps above; if the right child is empty, continue out of Stack * */void binary_tree::p reorder_tree_walk2 (binary_tree_node* node) {Stack<binary_    Tree_node*> _stack;    Binary_tree_node *tmp = node;            while (null!=tmp | |!_stack.empty ()) {if (null!=tmp) {_stack.push (TMP);            Visit (TMP);        tmp=tmp->left_child;            } else {tmp=_stack.top ();            _stack.pop ();        tmp=tmp->right_child; }}}/* * Recursive algorithm for subsequent traversal * left subtree--right subtree--root node */void binary_tree::p ostorder_tree_walk (binary_tree_node* node) {if (node!=n        ULL) {postorder_tree_walk (node->left_child);        Postorder_tree_walk (Node->right_child);    Visit (node); }}/* * Subsequent traversal of the non-recursive algorithm * Subsequent traversal of the non-recursive algorithm is more complex, here the idea of two stacks * follow-up: Left dial hand tree--right subtree---root node, first in the first stack traverse the root node, right subtree, left dial hand tree, with the second stack reversed order */void Binary_tree::p Ostorder_tree_walk2 (binary_tree_node* node) {StAck<binary_tree_node*> _stack;    Stack<binary_tree_node*> output;    Binary_tree_node *tmp = node;    _stack.push (TMP);        while (!_stack.empty ()) {tmp=_stack.top ();        Output.push (TMP);//stack one out stack, stack binary stack _stack.pop ();        if (null!=tmp->left_child)//first left after right can not reverse _stack.push (tmp->left_child);    if (null!=tmp->right_child) _stack.push (tmp->right_child);        } while (!output.empty ()) {Binary_tree_node *tmp2 = Output.top ();        Visit (TMP2);    Output.pop (); }}/* * Print binary tree layers, each layer is not spaced, if each layer of interval, to another design algorithm * based on queue to achieve, that is, breadth first search (BFS) thought */void Binary_tree::levelorder_tree_walk (binary_    tree_node* node) {queue<binary_tree_node*> _queue;    _queue.push (node);        while (!_queue.empty ()) {Binary_tree_node *tmp = _queue.front ();        Visit (TMP);        _queue.pop ();        if (tmp->left_child!=null) _queue.push (tmp->left_child); if (tmp->right_child!=null) _queue.Push (Tmp->right_child);    }}/* * Search Element * Non-recursive */binary_tree_node* binary_tree::search (char a) {if (root==null) return NULL;        else {Binary_tree_node *tmp = root; while ((Tmp!=null) && (tmp->data)!=a) {if (a<tmp->data) tmp=tmp->left_ch            Ild        else tmp=tmp->right_child;    } return TMP;    }}/* * Recursive search for an element */binary_tree_node* Binary_tree::search2 (binary_tree_node* node, char a) {Binary_tree_node *tmp = node; if ((tmp->data==a) | | |        (Node==null))    return node;        else {if (a<tmp->data) return Search2 (Tmp->left_child, a);    else return Search2 (Tmp->right_child, a);    }}/* find the maximum element */char Binary_tree::max () {binary_tree_node* tmp = root;    while (tmp->right_child!=null) {tmp=tmp->right_child; } return tmp->data;}    /* Find minimum element */char binary_tree::min () {binary_tree_node* tmp = root; while (Tmp->left_chiLd!=null) {tmp=tmp->left_child; } return tmp->data;} /********************************************************************************* * Delete a node in the binary search tree 1. Deleted node without subtree, delete directly, and modify The pointer to the parent node is empty. For the case of only one subtree, consider its subtree as a subtree of its parent node, as to whether it is left or right, as determined by the node being deleted. The most complex is the case of two sub-numbers, which can be considered in two ways, both of which are the same thought: using the leftmost node of the left subtree of the deleted node A or the left of the right subtree of a to replace a node, and modify the corresponding leftmost or most right node of the parent node pointer, modify the method similar to 2 ********** /void Binary_tree::tree_delete (Binary_    Tree_node *node) {if (node->left_child==null)//No left subtree, directly with right subtree substitution can transplant (node,node->right_child);    else if (node->right_child==null)//No right subtree, directly with Zuozi instead of transplant (node,node->left_child);        else {Binary_tree_node *tmp=node->right_child;        while (Tmp->left_child!=null) tmp=tmp->left_child;            if (Tmp->parent!=node) {transplant (NODE,TMP);            tmp->right_child=node->right_child; Tmp->right_child->parent=tmp        } transplant (NODE,TMP);        tmp->left_child=node->left_child;    tmp->left_child->parent=tmp;    }}int Main () {Binary_tree btree (7);    Btree.create_binary_tree ();    Binary_tree_node *root = Btree.get_root ();    Btree.preorder_tree_walk (root);    cout<<endl;    Btree.inorder_tree_walk (root);    cout<<endl;    Btree.postorder_tree_walk (root);    cout<<endl;    BTREE.PREORDER_TREE_WALK2 (root);    cout<<endl;    BTREE.INORDER_TREE_WALK2 (root);    cout<<endl;    BTREE.POSTORDER_TREE_WALK2 (root);    cout<<endl;    Btree.levelorder_tree_walk (root);    cout<<endl;    Binary_tree_node *result1=btree.search (' D ');    cout<< "The Address of Result1:" <<result1->data<<endl;    Binary_tree_node *result2=btree.search2 (Root, ' D ');    cout<< "The Address of Result2:" <<result2->data<<endl;    cout<< "Max node:" <<btree.max () <<endl; cout<< "min node:" <<Btree.min () <<endl;    Btree.tree_delete (RESULT1);    cout<< "after delete:" <<endl; Btree.levelorder_tree_walk (root);}


Test Search Binary Tree:

Test results:



GitHub one-day algorithm problem: Search binary Tree Interface implementation of the cluster

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.