Binary sort tree Implementation (C + + encapsulation)

Source: Internet
Author: User
Tags truncated

Design ideas

Design a class, the root node can only be read, with the construction of two fork tree, insert nodes, delete nodes, find, find the maximum value, find the minimum, find the first and successor of the specified node and other functional interfaces.

Binary Sorting Tree Concept

It is either an empty tree or a two-fork tree with the following properties:
(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than the value of its root node, (2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(3) The left and right sub-trees are also two-fork sorting trees respectively.

Various operations of binary sort tree

Insert new node
This is a recursive operation, recursive design to find the most source, in order to get the most simple design. A design is to determine the leaf node, the new node as a leaf node of the child inserted; one is always inserted as the root, the insertion node is always the root of the current subtree! Look at the code:

//Root is a level two pointer because if the tree is empty, you need to return the root modificationBOOLBinarytree::insertnode (Pnode * curoot,intdata, Pnode self) {   //recursive design to find the most source, to get the most simple design    if(*curoot = =nullptr) {pnode node=NewNode; if(node = =nullptr)return false; Node->data =data; Node->lchild = Node->rchild = Node->parent =nullptr; (*curoot) =node; Node->parent =Self ; return true; }    if(Data > (*curoot),data) Insertnode (& (*curoot)->rchild, data, *curoot); ElseInsertnode (& (*curoot)->lchild, data, *curoot); return true;}

constructor function
There are two overloaded functions: one without parameters, and one accepts an array directly constructs a two-fork sort tree using the Insert function.

Binarytree::binarytree (intint  len) {    = nullptr;      for (int0; i < Len; i++)        Insertnode (&Root, datum[i], root);} Binarytree::binarytree () {    = nullptr;}

Find function
This is also a recursive operation, in order to hide the root (root node), so a private function is written to perform a real lookup operation.

//the real lookup functionBinaryTree::p node Binarytree::_searchkey (pnode root,intkey) {    if(Root = =nullptr)returnnullptr; if(Root->data = = key)//found the        returnRoot; Else if(Root->data > key)//The value is small, to Zuozi find        return_searchkey (root->lchild, key); Else                      //The value is too large to find the right subtree.        return_searchkey (root->rchild, key);}//external InterfaceBinaryTree::p node Binarytree::searchkey (intkey) {    return_searchkey (root, key);}

Find the Precursor node
Either the largest in the left dial hand tree, or the parent node chain, the first parent node of the right child of its parent node, is the one that is being asked.

BinaryTree::p node Binarytree::searchpredecessor (pnode node) {if(node = =nullptr)returnnullptr; Else if(Node->lchild! =nullptr)returnSearchmaxnode (node->lchild); Else    {        if(Node->parent = =nullptr)returnnullptr;  while(node) {if(Node->parent->rchild = =node) Break; Node= node->parent; }        returnNode->parent; }}

To find the successor node
is basically similar to the finding precursor node. Either the smallest person in the right subtree, or the parent node chain, the first parent of its parent node, is the one that is being asked.

BinaryTree::p node Binarytree::searchsuccessor (pnode node) {if(node = =nullptr)returnnullptr; Else if(Node->rchild! =nullptr)returnSearchminnode (node->rchild); Else    {        if(Node->parent = =nullptr)returnnullptr;  while(node) {if(Node->parent->lchild = =node) Break; Node= node->parent; }        returnNode->parent; }}

Find Minimum value

BinaryTree::p node Binarytree::searchminnode (pnode curnode) {    if (curnode = = nullptr)         return  nullptr;     // always find the left dial hand tree is empty node, that is, the minimum value     while (Curnode->lchild! = nullptr        ) = curnode->lchild;     return Curnode;}

Find the maximum value

BinaryTree::p node Binarytree::searchmaxnode (pnode curnode) {    if (curnode = = nullptr)         return  nullptr;     // always find the node with the right subtree empty, which is the maximum value     while (Curnode->rchild! = nullptr        ) = curnode->rchild;     return Curnode;}

Middle Sequence traversal

void binarytree::_visitmiddle (Pnode root) {    if (root! = nullptr) {        _visitmiddle (root- >lchild);        printf ("%d; ", root->data);        _visitmiddle (Root-rchild);}    } void Binarytree::visitmiddle () {    _visitmiddle (root);}

Delete a node
This is the most troublesome operation, divided into four cases respectively, the most troublesome is the deletion of the left and right sub-tree is the case, this will be truncated to its successor content, delete its successor (recursion).

BOOLBinaryTree::D Eletenode (intkey) {    //return _deletenode (root, key);Pnode node =Searchkey (key); if(!node)return false; //to be truncated to a leaf knot.    if(Node->lchild = = nullptr && Node->rchild = =nullptr) {        if(Node->parent = =nullptr) {Root=nullptr; }        Else        {            if(Node->parent->lchild = =node) node->parent->lchild =nullptr; Elsenode->parent->rchild =nullptr;    } delete node; }    //only the left dial hand tree was abridged.    Else if(Node->lchild! = nullptr && Node->rchild = =nullptr) {        //pointing the father of the left child to the abridged Father .Node->lchild->parent = node->parent; //be truncated to root, modify root node        if(Node->parent = =nullptr) Root= node->Lchild; Else if(Node->parent->lchild = =node) node->parent->lchild = node->Lchild; Elsenode->parent->rchild = node->Lchild;    Delete node; }    //only the right sub-tree is truncated.    Else if(Node->lchild = = nullptr && Node->rchild! =nullptr) {        //pointing the father of the right child to the abridged Father .Node->rchild->parent = node->parent; //be truncated to root, modify root node        if(Node->parent = =nullptr) Root= node->Rchild; Else if(Node->parent->lchild = =node) node->parent->lchild = node->Rchild; Elsenode->parent->rchild = node->Rchild;    Delete node; }    //The left and right sub-trees have been truncated.    Else{//Replace the delete node with the successor node and delete the successorPnode successor =searchsuccessor (node); inttemp = successor->data;        Deletenode (temp); Node->data =temp; }}

Tuo structure function
Cleanup consumes memory when the function goes out of scope.

binarytree::~BinaryTree () {    _delallnode (root);} void Binarytree::_delallnode (Pnode root) {    if (root! = nullptr && root!=NULL) {        _ Delallnode (Root,lchild);        _delallnode (Root,rchild);              Deletenode (Root,data);}    }
Definition of Class (header file)
#pragmaOnce#include<stdio.h>#include<stdlib.h>classbinarytree{Private: typedefstructnode{structNode *parent; structNode *Lchild; structNode *Rchild; intdata; }*Pnode;    Pnode Root; void_visitmiddle (pnode root); Pnode _searchkey (pnode root,intkey); void_delallnode (Pnode root); Public: BinaryTree (); BinaryTree (int* Datum,intLen);    Pnode Searchmaxnode (pnode node);    Pnode Searchminnode (pnode node);    Pnode Getroot (); Pnode Searchkey (intkey); BOOLDeletenode (intkey);    Pnode Searchpredecessor (pnode node);    Pnode Searchsuccessor (pnode node); voidVisitmiddle (); BOOLInsertnode (Pnode * curoot,intdata, pnode self); ~BinaryTree ();};
Invoke Example
#include <conio.h>#include"BinaryTree.h"intMain () {intArrs[] = { at, $, A,3,8, the, -, +, the, the,345, A }; intLen =sizeof(ARRS)/sizeof(arrs[0]);    BinaryTree BTree (Arrs,len); Btree.deletenode ( -);    Btree.visitmiddle ();    Getch (); return 0;}

Binary sort tree Implementation (C + + encapsulation)

Related Article

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.