Data Structure BASICS (17), data structure basics 17

Source: Internet
Author: User

Data Structure BASICS (17), data structure basics 17

Features of binary sorting tree

The binary sorting tree is either an empty tree or a binary tree with the following features:

1. Each element has a key value, which cannot be repeated;

2. If its left subtree is not empty, the values of all nodes on the left subtree are smaller than the values of the root node;

3. If its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of the root node;

4. Its left and right subtree are also Binary Decision Trees.



Structure of elements stored in the binary sorting tree

Template <typename Type> class Element {public: Element (const Type & _ key): key (_ key) {} Element (): key (0) {} Type key; // here you can easily add more data // facilitate Element Extension };

Design and Implementation of binary sorting Tree nodes

Template <typename Type> class BstNode {friend class BsTree <Type>; public: BstNode (const Element <Type> & _ data = 0, BstNode * _ leftChild = NULL, bstNode * _ rightChild = NULL): data (_ data), leftChild (_ leftChild), rightChild (_ rightChild) {} const Type & getData () const {return data. key;} private: // The Element <Type> data; BstNode * leftChild; BstNode * rightChild; void display (int I);} is stored in the Node );};
// Traverse a binary tree in the middle order: // This binary tree element can be printed in ascending order. template <typename Type> void BstNode <Type >:: display (int I) {// first access the left subtree if (leftChild! = NULL) leftChild-> display (2 * I); // access the intermediate node // Number indicates the Number of std :: cout <"Number:" <I <", data. key = "<data. key <std: endl; // access the right subtree if (rightChild! = NULL) rightChild-> display (2 * I + 1 );}

Construction of binary sorting tree

Template <typename Type> class BsTree {public: // structure and structure BsTree (BstNode <Type> * init = NULL): root (init ){}~ BsTree () {if (! IsEmpty () makeEmpty (root);} // three main nodes of the Binary Search Tree: insert, delete, and search (an iterative search is added) // insert bool insert (const Element <Type> & item); // Delete void remove (const Element <Type> & item) {remove (item, root );} // recursive search for const BstNode <Type> * search (const Element <Type> & item) {return search (item, root );} // iteratively search const BstNode <Type> * searchByIter (const Element <Type> & item); // utility function void display () const {if (root! = NULL) root-> display (1);} void visit (BstNode <Type> * currentNode) const {std: cout <"data. key = "<currentNode-> data. key <std: endl;} bool isEmpty () const {return root = NULL;} void makeEmpty (BstNode <Type> * subTree ); // traverse void levelOrder () const in the middle order; private: const BstNode <Type> * search (const Element <Type> & item, const BstNode <Type> * currentNode ); void remove (const Element <Type> & item, BstNode <Type> * javastnode); private: BstNode <Type> * root ;};

Binary sorting Tree Insertion Algorithm

According to the definition of the dynamic search table, the insert operation is performed only when the search fails. If the binary sorting tree is an empty tree, the newly inserted node is a new root node. Otherwise, the newly inserted node must be a new leaf node, and Its Insertion Location is obtained by the search process.

// Implementation and parsing of binary sorting Tree Insertion template <typename Type> bool BsTree <Type >:: insert (const Element <Type> & item) {// if this is the first newly inserted node if (root = NULL) {root = new BstNode <Type> (item ); root-> leftChild = root-> rightChild = NULL; return true;} BstNode <Type> * parentNode = NULL; // The parent node BstNode to be inserted <Type> * currentNode = root; // The position to be inserted while (currentNode! = NULL) {// if the binary tree already contains this element, the insert error if (item. key = currentNode-> data. key) return false; parentNode = currentNode; // if the element to be inserted is greater than the current element if (item. key <currentNode-> data. key) currentNode = currentNode-> leftChild; // search for else currentNode = currentNode-> rightChild from the left; // search right} // a suitable insert position has been found. if (item. key <parentNode-> data. key) parentNode-> leftChild = new BstNode <Type> (item); else parentNode-> rightChild = new BstNode <Type> (item); return true ;}

Binary sorting tree search algorithm

If the binary sorting tree is empty, the query fails. otherwise:

1. If the given value is equal to the keyword of the root node, the search is successful;

2. If the given value is less than the keyword of the root node, continue searching on the left subtree;

3. If the given value is greater than the keyword of the root node, continue searching on the right subtree.

// Design and Implementation of binary sorting tree search // recursive search template <typename Type> const BstNode <Type> * BsTree <Type>: search (const Element <Type> & item, const BstNode <Type> * currentNode) {if (currentNode = NULL) return NULL; if (currentNode-> data. key = item. key) return currentNode; if (item. key <currentNode-> data. key) return search (item, currentNode-> leftChild); else return search (item, currentNode-> rightChild );}
// Iterative search template <typename Type> const BstNode <Type> * BsTree <Type>: searchByIter (const Element <Type> & item) {for (BstNode <Type> * searchNode = root; searchNode! = NULL;/* empty */) {if (item. key = searchNode-> data. key) return searchNode; if (item. key <searchNode-> data. key) searchNode = searchNode-> leftChild; else searchNode = searchNode-> rightChild;} return NULL ;}

Binary Tree deletion algorithm

In contrast to insertion, deletion is performed after the query is successful, and the feature of the binary sorting tree is maintained after a node in the binary sorting tree is deleted.

 

Deletion can be divided into three situations:

1. The deleted node is a leaf node. The value of the corresponding pointer field in the parent node is changed to "null" and the node is deleted;

2. the deleted node only has the left or right subtree. The value of the corresponding pointer field of the parent node is changed to "pointing to the left or right subtree of the deleted node ", delete the node;

3. The deleted node has both the left and right Subtrees: it is replaced by its precursor and then deleted;

// Implementation and resolution of Node Deletion for Binary sorting tree: template <typename Type> void BsTree <Type >:: remove (const Element <Type> & item, bstNode <Type> * effectnode) {if (currentNode! = NULL) {// if the element to be deleted is smaller than the current element if (item. key <currentNode-> data. key) remove (item, currentNode-> leftChild); // search left to delete // if the element to be deleted is greater than the current element else if (item. key> currentNode-> data. key) remove (item, currentNode-> rightChild); // search for Delete to the right // If the element to be deleted is equal to the current element (the element to be deleted is found) // else if (currentNode-> leftChild! = NULL) & (currentNode-> rightChild! = NULL) {// starts from the right child node of the current node, // keeps searching for the left, find the first node that is sequentially traversed from the current node. // The node is located in the current subtree, greater than the BstNode of the first node to be deleted <Type> * tmp = currentNode-> rightChild; while (tmp-> leftChild! = NULL) tmp = tmp-> leftChild; // use the searched node value to overwrite the value of the node to be deleted currentNode-> data. key = tmp-> data. key; // Delete the searched node remove (currentNode-> data, currentNode-> rightChild );} // if the current node is the node to be deleted // and Its left child (and/or) Right child is empty // by default, the left and right children are empty at the same time: // if true else {BstNode <Type> * tmp = currentNode; // if the left child is empty if (currentNode-> leftChild = NULL) // use his right child node to replace his position currentNode = currentNode-> rightChild; // if the right child is empty else // use his left child node to replace his position currentNode = currentNode-> leftChild; // release the node delete tmp ;}}}

Some practical operations on the binary search tree

// Clear the binary tree template <typename Type> void BsTree <Type >:: makeEmpty (BstNode <Type> * subTree) {if (subTree! = NULL) {if (subTree-> leftChild! = NULL) makeEmpty (subTree-> leftChild); if (subTree-> rightChild! = NULL) makeEmpty (subTree-> rightChild); delete subTree ;}}
// Layered traversal template of the Binary Search Tree <typename Type> void BsTree <Type >:: levelOrder () const {std: queue <BstNode <Type> *> queue; queue. push (root); while (! Queue. empty () {BstNode <Type> * currentNode = queue. front (); queue. pop (); visit (currentNode); if (currentNode-> leftChild! = NULL) queue. push (currentNode-> leftChild); if (currentNode-> rightChild! = NULL) queue. push (currentNode-> rightChild );}}

Performance Analysis of binary sorting tree

For each specific binary sorting tree, the ASL value of the tree can be obtained according to the definition of the average search length. Obviously, by the n keywords with the same value, the average length of the binary sorting tree in different forms obtained by the construction is different, and may even be very different (if the Binary Search Tree degrades into a linked list, the insertion/deletion/search performance degrades to O (N )).

However, in a random case, the average time cost for searching, inserting, and deleting binary sorting trees is O (logN );

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.