Binary search tree, or an empty tree, or:
1) If its left subtree is not empty, then the value of all nodes on the left subtree is less than the value of its root node;
2) If its right subtree is not empty, then 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 of binary search tree are also two-fork search trees respectively.
Search binary tree-related algorithm implementation:
1) Search binary tree creation and conversion to double-linked list implementation:
1#include"stdafx.h"2#include <iostream>3 using namespacestd;4 5 /*Two no find tree structure definition*/6typedefstructBstreenode7 {8 intM_nvalue;9Bstreenode *M_pleft;TenBstreenode *M_pright; One A}bstreenode, *Bstree; - -Bstreenode *m_phead = NULL;//points to the Loop Queue header node theBstreenode *m_ppre = NULL;//point to a previous node - - voidAddbstreenode (Bstreenode *&m_proot,intvalue); - voidInorderbstree (Bstreenode *m_proot); + voidCovertbstree (Bstreenode *m_proot); - + /**************** Create a two-fork search tree *****************/ A voidAddbstreenode (Bstreenode *&m_proot,intvalue) { at if(M_proot = =NULL) - { -Bstreenode *m_pbstree =NewBstreenode (); -M_pbstree->m_nvalue =value; -M_pbstree->m_pleft =NULL; -M_pbstree->m_pright =NULL; inM_proot =M_pbstree; - } to Else if(m_proot->m_nvalue<value) + { -Addbstreenode (m_proot->m_pright, value); the } * Else if(m_proot->m_nvalue>value) $ {Panax NotoginsengAddbstreenode (m_proot->m_pleft, value); - } the } + A /**************** binary search tree **************** in sequential traversal*/ the voidInorderbstree (Bstreenode *m_proot) + { - if(NULL = =m_proot) $ { $ return; - } - if(NULL! = m_proot->m_pleft) the { -Inorderbstree (m_proot->m_pleft);Wuyi } the Covertbstree (m_proot); - if(NULL! = m_proot->m_pright) Wu { -Inorderbstree (m_proot->m_pright); About } $ } - - /**************** Adjust the pointer position *****************/ - voidCovertbstree (Bstreenode *m_proot) A { +M_proot->m_pleft = M_ppre;//make the left pointer of the current node point to the last node in the doubly linked list the if(NULL = =m_ppre) - { $M_phead =M_proot; the } the Else the { theM_ppre->m_pright =M_proot; - } inM_ppre =M_proot; thecout << M_proot->m_nvalue <<"\ t"; the } About the intMain () the { theBstreenode *m_proot =NULL; +Addbstreenode (M_proot,Ten); -Addbstreenode (M_proot,7); theAddbstreenode (M_proot, the);BayiAddbstreenode (M_proot,5); theAddbstreenode (M_proot,9); theAddbstreenode (M_proot, A); -Addbstreenode (M_proot,1); - Inorderbstree (m_proot); the return 0; the}
2) Determine if an array is implemented as a binary search tree:
1#include"stdafx.h"2#include <iostream>3 using namespacestd;4 5 BOOLVertbst (intArr[],intlength)6 {7 if(ARR = = NULL | | length <=0)8 {9 return false;Ten } One introot = Arr[length-1]; A //find the small root node in the left sub-tree - inti =0; - for(; i < length-1; ++i) the { - if(arr[i]>root) - { - Break; + } - } + //find more than the root node in the right sub-tree A intj =i; at for(; J < length-1; ++j) - { - if(arr[j]<root) - { - return false; - } in } - //determine if the left subtree is a binary search tree to BOOLleft =true; + if(i>0) - { theleft =Vertbst (ARR, i); * } $ //Judging right subtree is not binary search treePanax Notoginseng BOOLright =true; - if(i<length-1) the { +right = Vertbst (Arr + i, length-i-1); A } the return(left&&Right ); + } - voidMain () $ { $ inta[7] = {3, 6, 5, 4, one, 9, 8}; - BOOLresult; -result = Vertbst (A,7); thecout << Result <<Endl; -}
The optimization of the two-fork search tree is generally implemented by the balanced binary tree.
Binary Tree Learning 2:2-fork Search Tree