Binary Search Tree
Tree
Tree Data Structure
Tree
A
nonlinear data
structure consisting of
nodes, each of which
contains data and
pointers to other
nodes.
Each node has only
one predecessor
(parent), except the
root node, which has
none.
Ω
Ω
Ω Ω Ω
ΩΩ Ω
Ω Ω
Ω Ω Ω
Terminology
Node
Root Node
Contains
data and
pointers to other
nodes (called child
nodes)
Parent Node
Each
node has at
most one node.
Root node has no
parent
Leaf
Node
with no
children
Ω Ω Ω
Leaf Node
Ω
Ω Ω Ω
Leaf Node
ΩΩ Ω
Leaf Node
Ω Ω
Ω Ω Ω
Leaf Node
Terminology
Root Node
Height of Tree
Longest
path to a
leaf
Subtree
A
node and all its
children
ΩΩ Ω
Ω Ω
Edge (or link)
A
path from a node
to its child
Ω
Null pointer (Ω)
Points
to no node
(in C++, 0)
Ω Ω Ω
Ω Ω Ω
Ω Ω Ω
Binary Tree
Root Node
Binary Tree
is a nonlinear linked
structure in which
each node may point
to two other nodes,
and every node but
the root node has a
single predecessor.
Left Subtree
Right Subtree
Binary Search Tree
Binary Search Tree
Binary tree in which a
data item is inserted
in such a way that
Root Node
30
25
all items less than the
item in a node are in
the left subtree
All items greater than
the item in a node are
in the right subtree
50
15
5
Left Subtree
60
20
55
Right Subtree
Traversals
Any process for visiting the nodes in some
order is called a traversal.
E.g.,
printing, searching.
Traversals
Preorder traversal:
Visit
the root, then visit its left and right
subtrees (children)
Postorder traversal:
Visit
the left and right subtrees (children), then
visit the root
Inorder traversal:
Visit
the left subtree, then the root, then the
right subtree.
Preorder Traversal
1
2
3
Postorder Traversal
3
1
2
Inorder Traversal
2
1
3
Representing Node
30
struct TreeNode {
elemType value; // value in node
TreeNode *left; // pointer to left child
TreeNode *right; // pointer to right child
};
TreeNode *root;
// pointer to root node
Note the recursive form of the declaration.
BST Operations (Public)
typedef int elemType;
class BinTree {
public:
BinTree();
void insertNode(elemType item);
bool search(elemType item);
void remove(elemType item);
void displayInOrder();
void displayPreOrder();
void displayPostOrder();
private:
. . . Private declarations
};
Private Methods
class BinTree {
public:
. . . Public methods
private:
struct TreeNode {
elemType value;
// value in node
TreeNode *left;
// pointer to left child node
TreeNode *right; // pointer to right child node
};
TreeNode *root;
// pointer to the root node
void
void
void
void
void
void
};
insert(TreeNode *&nodePtr, TreeNode *&newNode);
removeNode(elemType item , TreeNode *&nodePtr);
makeRemoval(TreeNode *&nodePtr);
displayInOrder(TreeNode *nodePtr);
displayPreOrder(TreeNode *nodePtr);
displayPostOrder(TreeNode *nodePtr);
BST Constructor
#define NULL 0
BinTree::BinTree() {
root = NULL;
// NULL pointer
}
BST InsertNode Operation
To DisplayInOrder
BST InsertNode Operation
void BinTree::insertNode(elemType item) {
TreeNode *newNode; // pointer to new node
// Create new node and store item
newNode = new TreeNode;
newNode->value = item;
newNode->left = newNode->right = NULL;
// Insert the node
insert(root, newNode);
}
BST Insert Method (Private)
void BinTree::insert(TreeNode *&nodePtr,
TreeNode *&newNode) {
if (nodePtr == NULL)
// Insert the node
nodePtr = newNode;
else if (newNode->value < nodePtr->value)
// Attach on left subtree
insert(nodePtr->left, newNode);
else
// Attach on right subtree
insert(nodePtr->right, newNode);
}
TreeNode *&NodePtr — nodePtr is reference to actual argument
Order of Insertion Affects
Tree Height
120, 42, 7, 42, 32, 2, 37, 40, 24
37, 24, 32, 42, 40, 42, 7, 120, 2
Which Structure Allows More
Efficient Search?
2
7
Key Value: 42
24
32
37
24
37
42
40
7
2
32
40
42
42
120
42
120
BST DisplayInOrder Operation
void BinTree::displayInOrder() {
displayInOrder(root);
}
BST DisplayInOrder Method
(Private)
void BinTree::displayInOrder(
TreeNode *nodePtr) {
if (nodePtr) {
displayInOrder(nodePtr->left);
cout << nodePtr->value << endl;
displayInOrder(nodePtr->right);
}
}
To BST diagram
BST Remove Operation
When removing a node from a tree, there
are 4 cases to consider
The node is a leaf
2. The node has no right child
3. The node has no left child
4. The node has both children
1.
BST Remove Operation
(Node is a leaf)
This node to be
deleted
root
37
42
24
Before
Ω Ω
7
Ω
Ω
37
55
Ω
After
42
24
Ω
Ω Ω
7
Ω
Ω
55
Ω
Ω
BST Remove Operation
(Node has no right child)
This node to be
deleted
root
37
Before
42
24
Ω Ω
7
Ω
Ω
37
55
Ω
Ω
Ω Ω
7
Ω
Ω
After
42
24
55
Ω
Ω
BST Remove Operation
(Node has no left child)
This node to be
deleted
root
Before
37
42
24
Ω Ω
7
Ω
Ω
Ω
After
37
55
Ω
24
Ω Ω
7
Ω
42
Ω
55
Ω
Ω
BST Remove Operation
(Node has both children)
root
This node to be
deleted
37
37
24
Ω
7
24
42
40
Ω
7
55
42
40
55
40
Ω
Ω
Ω
Ω Ω
Ω
Ω
37
Before
Ω
24
Ω
7
Ω
Ω
55
After
Ω
Ω
Ω
Ω Ω
Ω
BST remove Operation
void BinTree::remove(elemType item) {
removeNode(item, root);
}
BST removeNode Method (Private)
void BinTree::removeNode(elemType item ,
TreeNode *&nodePtr) {
if (item == nodePtr->value)
makeRemoval(nodePtr);
else if (item < nodePtr->value)
removeNode(item, nodePtr->left);
else // item > nodePtr->value
removeNode(item, nodePtr->right);
}
BST makeRemoval Method (Private)
void BinTree::makeRemoval(TreeNode *&nodePtr) {
TreeNode *tempPtr;
if (nodePtr == NULL)
cout << "Cannot remove empty node.\n";
else if (nodePtr->right == NULL) { // no right child
tempPtr = nodePtr;
nodePtr = nodePtr->left;
// reattach left child
delete tempPtr;
}
else if (nodePtr->left == NULL) { // no left child
tempPtr = nodePtr;
nodePtr = nodePtr->right;
// reattach right child
delete tempPtr;
}
...
This is where *&nodePtr becomes important.
BST makeRemoval Method (cont.)
else {
// node has right and left children
// check the right subtree
tempPtr = nodePtr->right;
// got to end of left subtree
while (tempPtr->left)
tempPtr = tempPtr->left;
// reattach left subtree
tempPtr->left = nodePtr->left;
tempPtr = nodePtr;
// reattach right subtree
nodePtr = nodePtr->right; delete tempPtr;
}
}
© Copyright 2026 Paperzz