A binary tree can be empty if not empty, the following properties are satisfied
(1) All key values of the non-empty left subtree are less than their root node key values
(2) All key values of the non-empty right subtree are greater than their root node key values
(3) The left and right sub-tree is a two-fork search tree
Maximum and minimum elements
The largest element must be on the end node of the right-most branch
The minimum element must be on the end node of the leftmost branch
Binary Tree Construction:
First define the node, the node structure is as follows
Class Bstnode {
friend class Bst; Set to Friend Class
private:
elementtype data; Used to store data
bstnode* leftchild; Left dial hand
bstnode* rightchild;//Right Sub
void show (int i); Show current node condition
};
The show function is used to display the node number of the current node and the data contents
According to the mathematical theorem: If a node number is I then its left sub number is 2*i right sub number is 2*i+1
void bstnode::show (int i) {
cout << "the node num" << i << "data:" << data << endl;
if (Leftchild) leftchild->show (2 * i); Show left node
if (rightchild) rightchild->show (2 * i + 1); Show Right Node
}
Binary Tree insertion:
(1) First determine if the root node is empty if it is empty the root node is the insertion point
(2) If the number of nodes to be inserted is larger than the current node, traverse the right node
If the number of nodes to be inserted is smaller than the current node, traversing the left node knows that the position is the insertion point when a null pointer is encountered
Insert code as follows
BOOL Bst::binaryinsert (ElementType data) {
Bstnode *p; Traverse Pointer
bstnode *q; Traverse the pointer's previous pointer
p = root;
Q = NULL;
Find the right position
while (p) {
q = p; Leave the node
if (data = = P->data)
return false before traversing; Duplicate occurs
if else if (data < p->data) {
p = p->leftchild; Walk to left
}
else if (Data > P->data) {
p = p->rightchild; Traverse to the right
}
}
//Build a new pointer
p = new Bstnode ();
P->data = data;
P->leftchild = NULL;
P->rightchild = NULL;
if (!root) { //If it is a null node, the root is set to p
root = p;
return true;
}
if (P->data < q->data)
q->leftchild = p;
else
q->rightchild = p;
return true;
}
Binary Tree Search:
The principle of finding and inserting the same is done by iterating through an element to find the right place to insert
void Bst::binarysearch (ElementType data) {
Bstnode *p; Traverse pointer
p = root; From the root node, go through
while (p) {
if (p->data = = data) {
cout << find element data! << Endl;
return;
}
else if (data < p->data) {
p = p->leftchild; Find left Element
}
else {
p = p->rightchild; Find right Element
}
}
cout << "can not find the element" << Endl;
return;
}
Binary Tree deletion:
To delete a leaf node: directly delete and modify the parent node pointer again to NULL
The node to be deleted has only one left child or one right child: the left or right child is hung on its parent node after the node is deleted
More complex cases: all have left and right subtrees or deep layers.
Find a maximum value in the left subtree of the binary tree to replace and delete
Or find a minimum value in the right subtree of the binary tree to replace and delete
The code is as follows:
bstnode* Bst::binarydelete (bstnode* targetnode, ElementType data) {if (!targetnode) {/*) returns if the node is still empty after recursion */cout <
;< "Can not find the element!" << Endl; } else if (data < Targetnode->data) {//Does not find node then recursion continues to find node Targetnode->leftchild = Binarydelete (TARGETNODE-&G T;leftchild, data); Go to the left subtree to find the node} else if (Data > Targetnode->data) {//No node is found then recursion continues looking for node Targetnode->rightchild = Binarydelete ( Targetnode->rightchild, data);
Go to right subtree looking for node} else {bstnode* Tmp = NULL; if (targetnode->leftchild && targetnode->rightchild) {///left and right nodes are present, TMP = Binaryfindmin (targetno
De->rightchild);
Targetnode->data = tmp->data;
Targetnode->rightchild = Binarydelete (Targetnode->rightchild, targetnode->data); } else {//Only the left node exists or only the right node is Tmp = TargetNode; Pre-staged element if (targetnode->leftchild) TargetNode = targetnode->leftchild; The deleted element replaces the IF (Targetnode->rightchild) tar with its left child nodeGetNode = targetnode->rightchild; The deleted element replaces the delete Tmp with its right child node;
Delete Node}} return TargetNode;
}
Note: The deleted node is the smallest node of the subtree, so there must be only right child nodes or no child nodes