Algorithm Series Note 3 (Binary search tree)

Source: Internet
Author: User
Tags array sort

(1) The nature of Binary search tree: set X as a node of a two-fork lookup tree. If Y is a node in the X left dial hand tree, then key[y]≤key[x]. If Y is a node in the right sub-tree of X. Then Key[x]≤key[y].

(2) In addition to the key field and satellite data, the nodes of the binary lookup tree include left, right, and p, respectively, pointing to the nodes ' sons, sons, and parents.

(3) Constructs a binary search tree The best case time complexity is O (NLGN), the worst case is O (n^2). Randomize the desired time to construct a binary lookup tree O (NLGN). The fast sorting algorithm with fast and randomized is the same comparison, but the order is different. It can be proved that the desired height of a binary search tree is O (LGN). This allows us to perform dynamic set operations in O (LGN) time, including minimum, maximum, search, predecessor (precursor), successor (successor ), insert, and delete.

(4) The middle sequence traversal of binary search tree is its order, and the time complexity of sequential traversal is O (n)

Below we will introduce the creation of the two-fork search tree, the operation of the middle sequence traversal and the dynamic set minimum, maximum, search, predecessor (precursor), successor (successor), insert and delete respectively.

1: Create and insert

There are two ways to create a binary search tree, one for normal recursion and one for inserting (non-recursive).

The code is as follows:

Create Bstvoid bstree::createbst (bstnode *p,const int &value) {if (p = = NULL) return;if (P->val > value) {if (p-> left = = NULL) {p->left = new Bstnode ();p->left->val = Value;p->left->left = Null;p->left->right = NUL L;p->left->parent = P;} Else{createbst (p->left, value);}} Else{if (P->right = = NULL) {p->right = new Bstnode ();p->right->val = Value;p->right->left = NULL;p-> right->right = Null;p->right->parent = P;} Else{createbst (p->right, value);}}}

Insert value void Bstree::insertbst (Bstnode *p, const int &value) {Bstnode *y = NULL; Bstnode *in = new Bstnode (); in->left = Null;in->right = Null;in->val = Value;in->parent = NULL;while (P! = NULL {y = p;if (P->val > In->val) p = p->left;else p = p->right;} if (y = = NULL) p = in;else{in->parent = Y;if (Y->val > In->val) y->left = In;else y->right = In;}} <strong></strong>

2: Middle Sequence traversal

Recursive Zuozi, output node value, and recursive right subtree are all possible.

The code is as follows:

  Middle order Traversal bstvoid Bstree::inorderbst (Bstnode *p) {if (p = = null) return;if (p->left! = null) Inorderbst (p->left); cout << p->val << ""; if (p->right! = NULL) Inorderbst (p->right);}

3:minimum and Maximum

The minimum is the leftmost node and the maximum value is the rightmost node.

The code is as follows:

The maximum and minimum values bstnode* bstree::minbst (Bstnode *p) {while (p->left! = NULL) p = P->left;return p;} bstnode* Bstree::maxbst (Bstnode *p) {while (p->right! = NULL) p = P->right;return p;}

4: Find

There are two types of recursion and iteration.

The code is as follows:

Find bstbstnode* Bstree::searchbst (bstnode *p, const int &value) {if (p = = NULL | | p->val = value) return P;if (value < P->val) return Searchbst (P->left, value), Else return Searchbst (p->right, value);} Iteration bstnode* Bstree::itersearchbst (bstnode *p, const int &value) {while (P! = NULL && P->val! = value) {if (VA Lue < P->val) p = p->left;else p = p->right;} return p;}

5:predecessor (precursor) and successor (successor)

precursor: if p p p y Span style= "color:red" >p y is not empty, set y y p y p

successor: If P the right subtree is not empty, the P the minimum value of the right subtree is P the subsequent; y to be P The parent node, if y not empty, set y for its successor, y is a P the lowest ancestor node, y The left child is also P 's ancestors.

The code is as follows:

Successor with precursor bstnode* Bstree::successor (Bstnode *p) {if (p->right! = NULL) return Minbst (p->right);  if its right subtree is not empty bstnode *y = P->parent;while (Y! = NULL && y->right = = p) {       //If Y is not set to P successor, Y is the lowest ancestor node of P, y The left child is also P's ancestor p = y;y = P->parent;} return y;} bstnode* bstree::p redecessor (Bstnode *p) {if (p->left! = NULL) return Maxbst (P->left); Bstnode* y = p->parent;while (Y! = NULL && y->left = = p) {   //If Y is not set to its predecessor, Y is the lowest ancestor of P, Y's right child is also the ancestor of P   if y ->right = = P then y is the precursor p = y;y = P->left;} return y;}

6:delete

To be continued ....

7: Destroy the binary tree

The code is as follows:

Destroy Bstvoid Bstree::d estroybst (Bstnode *p) {if (p = = null) return;if (p->left! = null) {DESTROYBST (p->left);} if (p->right! = NULL) {destroybst (p->right);} Delete p;}

Full code:

BSTree.h

#ifndef bstree#define bstree#include <iostream>using namespace Std;class bstnode{public:bstnode *left, *right; Bstnode *parent;int Val;//friend class Bstree;}; Class bstree{public://initializes the root node bstree (int rootval) {root = new Bstnode (); root->val = Rootval;root->left = null;root-& Gt;right = Null;root->parent = NULL;} Create two fork lookup tree void Createbst (Bstnode *p, const int &value);//sequence traversal array sort void Inorderbst (Bstnode *p); void Destroybst (Bstnod e *p);//Find bstnode* Searchbst (bstnode *p, const int &value); bstnode* Itersearchbst (bstnode *p, const int &value);//min and Max bstnode* Minbst (Bstnode *p); bstnode* Maxbst (Bstnode *p);//  successor and precursor Bstnode *successor (Bstnode *p); Bstnode *predecessor (Bstnode *p);//Insert value void Insertbst (Bstnode *p, const int &value); Bstnode *root;}; #endif

BSTree.cpp

#include "BSTree.h" #include <iostream>using namespace std;//create Bstvoid bstree::createbst (bstnode *p,const int &value) {if (p = = null) return;if (P->val > value) {if (P->left = = null) {P->left = new Bstnode ();p->left-& Gt;val = Value;p->left->left = Null;p->left->right = Null;p->left->parent = P;} Else{createbst (p->left, value);}} Else{if (P->right = = NULL) {p->right = new Bstnode ();p->right->val = Value;p->right->left = NULL;p-> right->right = Null;p->right->parent = P;} Else{createbst (p->right, value);}}} Middle order Traversal bstvoid Bstree::inorderbst (Bstnode *p) {if (p = = null) return;if (p->left! = null) Inorderbst (p->left); cout &L t;< p->val << ""; if (p->right! = NULL) Inorderbst (p->right);} Destroy Bstvoid Bstree::d estroybst (Bstnode *p) {if (p = = null) return;if (p->left! = null) {DESTROYBST (p->left);} if (p->right! = NULL) {destroybst (p->right);} Delete p;} Find bstbstnode* Bstree::searchbst (bstnode *p, constint &value) {if (p = = NULL | | p->val = = value) return P;if (Value < P->val) return Searchbst (P->left, value); else return Searchbst (p->right, value);} Iteration bstnode* Bstree::itersearchbst (bstnode *p, const int &value) {while (P! = NULL && P->val! = value) {if (VA Lue < P->val) p = p->left;else p = p->right;} return p;} The maximum and minimum values bstnode* bstree::minbst (Bstnode *p) {while (p->left! = NULL) p = P->left;return p;} bstnode* Bstree::maxbst (Bstnode *p) {while (p->right! = NULL) p = P->right;return p;}   Successor with precursor bstnode* Bstree::successor (Bstnode *p) {if (p->right! = NULL) return Minbst (p->right); If its right subtree is not empty bstnode *y = P->parent;while (Y! = NULL && y->right = = p) {//If Y is not set to P successor, Y is the lowest ancestor node of P, y The left child is also P's ancestor p = y;y = P->parent;} return y;} bstnode* bstree::p redecessor (Bstnode *p) {if (p->left! = NULL) return Maxbst (P->left); Bstnode* y = p->parent;while (Y! = NULL && y->left = = p) {//If Y is not set to its predecessor, Y is the lowest ancestor of P, Y's right childAlso the ancestor of P if y->right = = P then y is the precursor p = y;y = P->left;} return y;} Insert value void Bstree::insertbst (Bstnode *p, const int &value) {Bstnode *y = NULL; Bstnode *in = new Bstnode (); in->left = Null;in->right = Null;in->val = Value;in->parent = NULL;while (P! = NULL {y = p;if (P->val > In->val) p = p->left;else p = p->right;} if (y = = NULL) p = in;else{in->parent = Y;if (Y->val > In->val) y->left = In;else y->right = In;}}

Main.cpp

binary find tree int a[10] = {5,4,6, 7,2,4, 1, 8, 5, 10};  Bstree BST (A[0]); for (int i = 1; i <; i++) Bst.insertbst (Bst.root, a[i]);  Build a binary find tree by inserting values//bst.createbst (Bst.root, a[i]); Recursive operation to create a binary search tree//sequence traversal output bst.inorderbst (bst.root); cout << Endl;  Bstnode *s = Bst.itersearchbst (Bst.root, 5); cout << s->val << "left Child:" << s->left->val << "Right Child:" << s->right->val <<endl;//Find minimum Bstnode *min = Bst.minbst (bst.root); cout << "Minimum:" &L t;< min->val << endl;//Find maximum Bstnode *max = Bst.maxbst (bst.root); cout << Max: << Max->val & lt;< endl;//successor Bstnode *suc = Bst.successor (s); cout << "successor:" << suc->val << endl;//precursor Bstnode *PR E = Bst.predecessor (s); cout << "precursor:" << pre->val << endl;//Insert 6bst.insertbst (bst.root, 6); cout <& Lt "Insert element after sequence traversal:" << endl;bst.inorderbst (bst.root); cout << Endl;bst.destroybst (bst.root); return 0;

Result:


Algorithm Series Note 3 (Binary search tree)

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.