A dynamic lookup tablean ideal  data structure  .Binary sort tree  is defined by  :binary sort  tree T  is a tree  ,ugg outlet online,or it  is empty  ,or have  about  three properties  :(  1)  ,if  the root node in the T  of the  left subtree  is not empty,  the  left subtree  node all  &  #  20540  ;were less than  the root node in the T  &  #  20540  ;(  2)  ,if  the root node in the T  right subtree  is non-empty,  its right subtree  node all  &  #  20540  ;are greater than  the root node in the T  &  #  (  20540  ;3  )  ,the T root  nodes around  the sub-trees are  binary sort tree  below is the code  :file "  ;tree.
h"  ;#  include<  ;iostream>  ;stack>  ;#  #  include<  ;include<  ;queue>  ;using  namespace  STD  ;define  MAX_NODE_NUM  #  20  /  /  tree node of  maximum value class  Bin_Sort_Tree  ;/  /  class  BSTnode  {int  tag  tree node  ;/  /  after the preorder  as visiting  mark int  data  ;BSTnode  lchild  ;BSTnode  * rchild  ;friend  class  Bin_Sort_Tree  ;}  ;/  /  binary sort  tree class  Bin_Sort_Tree  {public:  int  Get_data  (  BSTnode  * P  )  {return  p->  ;data  bool  ;}  Search_BST  (  BSTnode  * T  ,int  a  ,BSTnode  * &  ;F  ,BSTnode  * &  ;P  )  {  /  *  position  in the  tree  to find the value of a nodes  ,find  /  ,P  save the  node address  ,* f  / P  The parent node  /  position  * / P  = T  ;while  (  P  )  {if  (  p->  ;data = =  a  )  return  else  if  (  true  ;p->  ;data>  ;a  )  {f  = p  ;P =  p->  ;lchild  ;}  else  {f  = p  ;P =  p->  ;rchild  return  false  ;}  }  ;}  /  /  a value for the  node is inserted in  the tree,  if the value  already exists  ,do not  insert the void  Insert_BST_1  (  BSTnode  * &  ;T  ,int  a  )  {BSTnode  * f  = NULL  ;BSTnode *  P =  NULL  ;if  (  Search_BST  (  T  ,a  ,F  ,P,  return  )  )  ;/  /  the tree  has  the same value  node  ,not  into the else  {BSTnode  * s  = new  BSTnode  ;s->  ;data =  a  ;s->  ;lchild =  s->  ;rchild =  NULL  ;if  (  s->  ;data>  ;f->  ;data  )  f->  ;rchild  = s;  elsef->  ;lchild =  s  ;}  }  void  Insert_BST_2  (  BSTnode  * &  T  ,int  a  )  ;/  /  insert algorithm  recursive  {if  (  !T  )  {cout<  ;<  ;"  ;<  ;the tree is empty  "  ;<  ;endl  ;return  ;}  if  (  T->  ;data>  ;a  )  {if  (  !T->  ;lchild  )  {T->  lchild  = new  BSTnode  ;T->  ;;lchild->  ;data =  a  ;T->  ;lchild->  ;lchild =  NULL  ;T->  ;lchild->  ;rchild =  NULL  ;return  ;}  elseInsert_BST_2  (  T->  ;lchild  ,a  )  ;}  if  (  T->  ;data<  ;a  )  {if  (  !T->  ;rchild  )  {T->  rchild  = New  ;BSTnode  ;T->  ;rchild->  ;data =  a  ;T->  ;rchild->  ;lchild =  NULL  ;T->  ;rchild->  ;rchild =  NULL  ;}  elseInsert_BST_2  (  return  ;T->  ;rchild  ,a  )  ;}  }  void  Create_BSTree  (  BSTnode  * &  ;T  )  /  /  set  {cout<  ;<  ;"  ;input  binary sort tree  elements  ,input  - 1  represented the end of  input  :"  ;num  ;int       ;int  a  ,I = 0  ;while  (  cin>  ;>  ;a  &  ;&  ;a  != - 1  )  {num      = a  ;I +  +  ;}  if  (  Num       =  =  1  )  {cout<  ;<  ;"  ;sort tree  is an empty "  ;<  ;<  ;endl  ;return  ;}  int  k =  I  ;T =  new  BSTnode  ;T->  ;data =  num       ;T->  ;lchild =  T->  ;rchild =  NULL  ;for  (  I = 1  ;i<  ;K  ;I +  +  )  Insert_BST_1  (  T  ,Num      )  ;cout<  ;<  ;"  ;_  _  _  _  achievements  completed  _  _  _  _  "  ;<  ;<  ;endl  ;}  void  Delete_BST  (  BSTnode  * &  ;T  ,int  a  )  /  /  delete node  value for the a  node  {  /  *  ---------------------------------------------------------  / removed from the tree  at a node  ,to ensure that the  deleted after the  tree or  a binary sort tree  ,Moncler Outlet,/  removed before  ,first in the  tree  to find whether there is  the  node  to the  node  ,using P  /  F  P  ,point to the  parent node  ,Dr Dre Headphones,the  node  position in the tree  has  the following four cases  :/  /  1:  if  P pointing to the  node  is a  leaf node  ,then  directly to the f  pointer to the  left subtree  or  /  right subtree  .
Empty  ,and then delete the P  node  ./  /  2:  if  P pointing to the  node  is  only the left  subtree  or  the right subtree  ,then only need to  make  the P node  /  original  position at f  (  the left subtree  or  the right subtree  )  with P  subtrees  in place  .
/  /  3:  if P  by pointing to the  node  is the root node  ,Men Nike Running Shoes,then  directly to the  root node  set null  /  /  4:  if P  by pointing to the  node  or  subtree is  not empty  ,in order to remove the P  after the original  sequence of  cis /  sequence  unchanged  ,it  needs in the original  sequence  to identify the  P  direct  precursor  (  or directly  subsequent  )  /  node  with  the  node values  instead of P  node values  ,and then  delete  the  direct anterior  /  drive  (  or  subsequent  )  node  .
/  in the  traversal  sequence  for  nodes of the  direct  precursor  method is  the  node of the  children left /  right  chain  domain began  ,until the  node  right child  is empty  ./  ---------------------------------------------------------  * / BSTnode  * f  = NULL  ;BSTnode  * P  = NULL  ;BSTnode *  BSTnode  *  q  = NULL  ;s  = NULL  ;if  (  Search_BST  (  T  ,a  ,F  ,P  )  )  {if  (  p->  ;lchild  &  ;&  ;p->  ;rchild  )  {q  = p  ;s =  p->  ;lchild  ;while  (  s->  ;rchild  )  {q  = s  ;s =  s->  ;rchild  p->  ;}  ;data =  s->  ;data  ;/  / s  to  delete node  direct  precursor  ,Moncler UK,and S is  not  the right subtree  of the if  (  q  != P  )  q->  ;rchild =  s->  ;lchild  ;elseq->  ;lchild =  s->  ;lchild  ;/  /  this  is  ,Q left  subtree of the  right subtree  is empty when the  delete  s  ;cout<  ;<  ;"  ;<  ;node deletion  successful "  ;<  ;endl  ;return  ;}  else  {if  (  !P->  ;lchild  )  /  /  left subtree  is null  {q  = p  ;P =  p->  ;rchild  else  ;}  /  /  right subtree  is null  {q  = p  ;P =  p->  ;lchild  ;}  /  /  P  by pointing to the  subtree  below will  connect to the f  refers to  (  deleted  nodes of the  parent  node  .
)  if  (  !T  )  /  /  deleted  node  as the root  node T  = p  ;else  if  (  q =  = f->  ;lchild  )  f->  ;lchild  = p;  elsef->  ;rchild =  p  ;delete  Q  ;cout<  ;<  ;"  ;<  ;node deletion  successful "  ;<  ;endl  ;return  ;}  }  else  {cout<  ;<  ;"  ;the node you want to delete  not  the presence of "  ;<  ;<  ;endl  ;return  ;}  }  /  /  here is  a binary tree  traversal of the four  ,all  Non - recursive  method of void  PreOrder_Traverse  (  BSTnode  * T  )  /  /  preorder traversal  {stack<  ;BSTnode  * >  *  p  ;s  ;BSTnode  ;P =  T  ;while  (  s.
empty  (  P  !)  )  {if  (  P  )  {cout<  ;<  ;p->  ;data<  ;<  ;"  ;"  ;s.push  ;(  P  )  ;P =  p->  ;lchild  ;}  else  {P  = s.top  (  )  ;s.pop  (  )  ;P  = p->  ;rchild  ;}  }  }  void  InOrder_Traverse  (  BSTnode  * T  )  /  /  traversal  {stack<  ;BSTnode  * >  ;s  ;BSTnode *  P =  T  ;while  (  s.
empty  P  !(  )  )  {if  (  P  )  {s.push  (  P  )  ;P =  p->  ;lchild  ;}  else  {P  = s.top  (  )  ;s.pop  (  )  ;cout<  ;<  ;p->  ;data<  ;<  ;"  ;"  ;p->  ;P  =  rchild  ;}  ;}  }  void  PostOrder_Traverse  (  BSTnode  * T  )  /  /  after the preorder  {stack<  ;BSTnode  * >  ;s  ;BSTnode *  P =  T  ;while  (  s.
empty  (  P  !)  )  {if  (  P  )  {p->  ;tag = 0  ;s.push  (  P  )  ;P =  p->  ;lchild  ;}  else  {P  = s.top  (  )  ;if  (  p->  ;tag  )  {cout<  ;<  ;p->  ;data<  ;<  ;"  ;"  ;s.
pop  ;(  )  ;P =  NULL  ;}  else  {p->  ;tag = 1  ;P =  p->  ;rchild  ;}  }  }  }  void  LevelOrder_Traverse  (  BSTnode  * T  )  /  /  hierarchy traversal  {queue<  ;BSTnode  * >  ;Q  ;BSTnode *  P =  T  ;q.
push  (  P  )  ;while  (  !Q.empty  (  )  )  {P  = q.front  (  )  ;q.pop  (  )  ;cout<  ;<  ;p->  ;data<  ;<  ;"  ;"  ;if  ;(  p->  ;lchild  )  q.push  (  p->  ;lchild  )  ;if  (  p->  ;rchild  )  q.
push  (  p->  ;rchild  )  ;}  }  }  ;The main function  of "  ;main.cpp"  ;include"  tree.h"  ;int  main  #  ;(  )  {Bin_Sort_Tree  tree  ;BSTnode  * root  = NULL  ;cout<  ;<  ;"  ;_  _  _  _  _  binary sort tree  _  _  _  _  "  ;<  ;<  ;endl  ;tree.
Create_BSTree  (  root  )  ;cout<  ;<  ;"  ;before  binary tree traversal  : "  ;tree.PreOrder_Traverse  ;(  root  )  ;cout<  ;<  ;endl  ;cout<  ;<  ;"  ;traversal in binary  tree  : "  ;tree.
InOrder_Traverse  ;(  root  )  ;cout<  ;<  ;endl  ;cout<  ;<  ;"  ;after the traversal  binary tree is  :"  ;tree.PostOrder_Traverse  ;(  root  )  ;cout<  ;<  ;endl  ;cout<  ;<  ;"  ;the level of binary tree traversal  : "  ;tree.
LevelOrder_Traverse  ;(  root  )  ;cout<  ;<  ;endl  ;int  data  ;BSTnode  * f  = NULL  ;BSTnode  * P  = NULL  ;cout<  ;<  ;"  ;you have to input  the search  nodes  value  :"  ;>  ;cin>  ;data  ;if  ;(  tree.
Search_BST  (  root  ,data  ,F  ,P  )  )  {cout<  ;<  ;"  ;your search  node address  : "  ;<  ;<  ;p<  ;<  ;endl  ;cout<  ;<  ;"  ;his  parent node  value is:  "  ;<  ;<  ;tree.
Get_data  (  <  f  )  ;<  endl  ;}  ;elsecout<  ;<  ;"  ;tree  no you want to  node "  ;<  ;<  ;endl  ;cout<  ;<  ;"  ;input  you want to delete.  Node values  :"  ;cin>  ;>  ;data  ;tree.
Delete_BST  ;(  root  ,data  )  ;cout<  ;<  ;"  ;after removal of the  binary  tree traversal sequence  : "  ;tree.InOrder_Traverse  ;(  root  )  ;cout<  ;<  ;endl  ;cout<  ;<  ;"  ;input  you want to insert  in the tree  value  :"  ;cin>  ;>  ;data  ;tree.
Insert_BST_2  ;(  root  ,data  )  ;cout<  ;<  ;"  ;after insertion,  tree traversal sequence  : "  ;tree.InOrder_Traverse  ;(  root  )  ;cout<  ;<  ;endl  ;return 0  ;}  here is the  test 
results  _  _  _  _  _  binary sort  tree  _  _  _  _  input  binary sort tree  elements  ,input  - 1  represented the end of  input  :57329488  110  - 1  _  _  _  _  achievements  completed  _  _  _  
_  preorder traversal of  binary tree is  :53214798  10  traversal in binary  tree  is: 12345789  10  after the traversal of  binary tree is  :124381097  5  level of binary tree traversal  : 53724918  
10 input  you want to search  the  node values  :7  your search  node address  : 00380D18  his  parent node  value is:  5 input  you want to delete the  node values  :10  node deletion  was successfully 
removed after the  binary  tree traversal sequence  : 12345789  input  you want to insert  the values in the  tree  :  6  after insertion,  tree traversal sequence  : 12345678  9Press  any  key  to  continue 
Related articles:
 
没有评论:
发表评论