1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| bool BinarySearchTree::insert(int data) { Node *p = root, *newNode = new Node(data); Node *temp = p; if (!root) { root = newNode; } else { while (p) { temp = p; if (data < p->data) p = p->lchild; else if (data > p->data) p = p->rchild; else return false; } newNode->parent = temp; if (newNode->data > temp->data) temp->rchild = newNode; else temp->lchild = newNode; } return true; }
|