首页 > 代码库 > 二叉查找树

二叉查找树

二叉查找树的结构和普通二叉树相同。它要么是空树,要么满足:对任意结点,如果左子树不为空,则左子树上所有结点的权值都小于该结点的权值;如果右子树不为空,则右子树上所有结点的权值都大于该结点的权值。在二叉查找树中,任意结点的左子树和右子树都是一棵二叉查找树。一般而言,二叉树上结点的权值都是唯一的。

基本操作:

  1 //二叉查找树
  2 //结点定义
  3 template <typename Type> class Node {
  4 public:
  5     Type data;
  6     Node *lchild,*rchild,*parent;
  7     //parent is NULL by default
  8     Node(Type _data, Node<Type> *_parent=NULL) {
  9         data=http://www.mamicode.com/_data;
 10         lchild=NULL;
 11         rchild=NULL;
 12         parent=_parent;
 13     }
 14     //no need to delete parent because all nodes are already deleted by delete method recursively
 15     ~Node() {
 16         if(lchild!=NULL){
 17             delete lchild;
 18         }
 19         if(rchild!=NULL){
 20             delete rchild;
 21         }
 22     }
 23 
 24 
 25     void insert(Type value) {
 26         //do not allow repeated element
 27         if(data=http://www.mamicode.com/=value){
 28             return;
 29         }
 30         else if(value>data){
 31             if(rchild==NULL){
 32                 rchild=new Node<Type>(value,this);
 33             }
 34             else{
 35                 rchild->insert(value);
 36             }
 37         }
 38         //value<data
 39         else{
 40             if(lchild==NULL){
 41                 lchild=new Node<Type>(value,this);
 42             }
 43             else{
 44                 lchild->insert(value);
 45             }
 46         }
 47     }
 48 
 49 
 50     Node* search(Type value) {
 51         if(data=http://www.mamicode.com/=value){
 52             return this;
 53         }
 54         else if(value>data){
 55             if(rchild==NULL){
 56                 return NULL;
 57             }
 58             else{
 59                 rchild->search(value);
 60             }
 61         }
 62         //value<data
 63         else{
 64             if(lchild==NULL){
 65                 return NULL;
 66             }
 67             else{
 68                 lchild->search(value);
 69             }
 70         }
 71     }
 72 
 73     //inorder_print
 74     void print() {
 75         if(lchild!=NULL){
 76             lchild->print();
 77         }
 78         cout<<data<<" ";
 79         if(rchild!=NULL){
 80             rchild->print();
 81         }
 82     }
 83 
     //only for nodes with children 84 Node<Type> * predecessor() { 85 Node<Type> *temp = lchild; 86 //the most right element in its left child tree 87 while (temp != NULL && temp->rchild != NULL) { 88 temp = temp->rchild; 89 } 90 return temp; 91 } 92
     //only for nodes with children 93 Node<Type> * successor() { 94 Node<Type> *temp = rchild; 95 //the most left elemnent in its right child tree 96 while (temp != NULL && temp->lchild != NULL) { 97 temp = temp->lchild; 98 } 99 return temp; 100 } 101 102 //remove_node for leaf nodes or nodes with only one child 103 void remove_node(Node<Type> *delete_node) { 104 Node<Type> *temp = NULL; 105 if (delete_node->lchild != NULL) { 106 temp = delete_node->lchild; 107 //the successor of delete_node is the father of delete_node 108 temp->father = delete_node->father; 109 } 110 if (delete_node->rchild != NULL) { 111 temp = delete_node->rchild; 112 //the predecessor of delete_node is the father of delete_node 113 temp->father = delete_node->father; 114 } 115 //if delete_node is the left child 116 if (delete_node->father->lchild == delete_node) { 117 delete_node->father->lchild = temp; 118 } else { 119 delete_node->father->rchild = temp; 120 } 121 //set them to NULL to prevent deleting the entire tree 122 delete_node->lchild = NULL; 123 delete_node->rchild = NULL; 124 delete delete_node; 125 } 126 127 //the complete version of deleting a node 128 //make use of the remove_node method 129 bool delete_node(Type value){ 130 Node<Type> *delete_node,*current_node; 131 //find the target node 132 current_node=search(value); 133 if(current_node==NULL){ 134 return false; 135 } 136 //replacing the current_node with either its predecessor or successor 137 if(current_node->lchild!=NULL){ 138 delete_node=current_node->predecessor(); 139 } 140 else if(current_node->rchild!=NULL){ 141 delete_node=current_node->successor(); 142 } 143 //else it must be a leaf node, which we can just remove it using remove_node 144 else{ 145 delete_node=current_node; 146 } 147 //replace value and delete the predecessor/successor 148 current_node->data=http://www.mamicode.com/delete_node->data; 149 remove_node(delete_node); 150 return true; 151 } 152 }; 153 154 155 template <typename Type> class BinaryTree { 156 private: 157 Node<Type> *root; 158 public: 159 BinaryTree() { 160 root=NULL; 161 } 162 ~BinaryTree() { 163 delete root; 164 } 165 void insert(Type value) { 166 if(root==NULL){ 167 root=new Node<Type>(value); 168 } 169 else root->insert(value); 170 } 171 bool find(Type value) { 172 if(root==NULL){ 173 return false; 174 } 175 else{ 176 if(root->search(value)==NULL){ 177 return false; 178 } 179 else{ 180 return true; 181 } 182 } 183 } 184 void print() { 185 if(root!=NULL){ 186 root->print(); 187 } 188 } 189 190 bool delete_node(Type value){ 191 if(root==NULL){ 192 return false; 193 } 194 return root->delete_node(value); 195 } 196 };

 

二叉查找树