Insert a node in a known two-fork lookup tree, of course, the inserted node will be on the leaf, if the inserted data is the same as the original data, then do not insert, of course, if the structure of the tree to add a member representing the number of repetitions or another data structure to save the number of repetitions.
The main functions are as follows:
1BTree *insertnode (BTree *root,intdata)2 {3BTree *tmp =nullptr;4 if(Root = =nullptr)5 {6TMP = (BTree *) malloc (sizeof(BTree));7Tmp->data =data;8Tmp->left =nullptr;9Tmp->right =nullptr;TenRoot =tmp; One } A Else - { - if(Data < root->data) the { -Root->left = Insertnode (root->Left , data); - } - Else if(Data > Root->data) + { -Root->right = Insertnode (root->Right , data); + } A } at returnRoot; -}
All the source code as follows, the remainder of the previous article: Find the function: the code
1#include"iostream"2#include"stdlib.h"3 4 #definelog (s); std::cout<<s;5 6typedefstruct_btree_7 {8 intdata;9 struct_btree_ *Left ;Ten struct_btree_ *Right ; One }btree; A -BTree *createnode (intdata) - { theBTree *p = (BTree *) malloc (sizeof(BTree)); -P->data =data; -P->left =nullptr; -P->right =nullptr; + returnp; - } + ABTree *findkey (BTree *father,intdata) at { - if(Father->data <data) - returnFindKey (father->Right , data); - Else if(father->data>data) - returnFindKey (father->Left , data); - Else in returnfather; - } to +BTree *findmax (BTree *root) - { the if(Root! =nullptr) * while(Root->right! =nullptr) $root = root->Right ;Panax Notoginseng returnRoot; - } the +BTree *findmin (BTree *root) A { the if(Root->left = =nullptr) + returnRoot; - Else $ returnFindmin (root->Left ); $ } - -BTree *createtree (BTree *&root) the { -BTree *t1, *t2, *t4, *t3, *t6, *t7, *t8, *t9, *T10;WuyiT1 = CreateNode (1); theT2 = CreateNode (9); -T3 = CreateNode (7); WuT4 = CreateNode (5); -T6 = CreateNode ( the); AboutT7 = CreateNode ( One); $T8 = CreateNode ( -); -T9 = CreateNode ( A); -T10 = CreateNode ( -); -Root->left =T4; ARoot->right =T6; +T4->left =T1; theT4->right =T2; -T2->left =T3; $T6->left =T7; theT6->right =T8; theT8->left =T9; theT8->right =T10; the returnRoot; - } in the voidShowresult (BTree *node) the { About if(node = =nullptr) the return; theStd::cout << Node->data <<Std::endl; the } + -BTree *insertnode (BTree *root,intdata) the {BayiBTree *tmp =nullptr; the if(Root = =nullptr) the { -TMP = (BTree *) malloc (sizeof(BTree)); -Tmp->data =data; theTmp->left =nullptr; theTmp->right =nullptr; theRoot =tmp; the } - Else the { the if(Data < root->data) the {94Root->left = Insertnode (root->Left , data); the } the Else if(Data > Root->data) the {98Root->right = Insertnode (root->Right , data); About } - }101 returnRoot;102 }103 104 intMainvoid) the {106BTree *root =nullptr;107Root = CreateNode (Ten);108Root =Createtree (root);109Root = Insertnode (root,8); theSystem"Pause");111 return 0; the}
The result is this:
The original structure of the tree is as follows: (in Word)
It would be appreciated if there were any mistakes that the reader could point out.
Above.
Binary find tree-inserted function