Lec 6,7 BST
Lec 6,7 BST
Lec 6,7 BST
5
Root node
L child -33 17
R child
NULL
53 91 -115
Leaf node
NULL NULL NULL NULL NULL NULL
Tree Terminology A
4
▪ Siblings: nodes that have the same parent
Ex: E and F; I, J, and K; G and H; B, C and D A
▪ Path: A sequence of nodes such that any two consecutives nodes form an edge (𝐴, 𝐵, 𝐹, 𝐽)
For each node in a tree, there exists a unique path from the root node to that node.
The length of this path is the depth of the node, e.g.,
E has depth 2
L has depth 3
Each node contains a data element, and two pointers, each of which points to another node.
Alternative recursive definition: a binary tree is either
▪a tree consisting of a single node, or
▪a tree whose root has an ordered pair of children, each of which is a binary tree
Each node Implementation
struct TreeNode
{
InfoNode info ; // Data member
TreeNode* left ; // Pointer to left child
TreeNode* right ; // Pointer to right child
};
▪ Complete Binary Tree: let ℎ be the height of the tree Perfect binary
trees are balanced
▪ for 𝑖 = 0 … ℎ − 1, there are 2𝑖 nodes on level 𝑖
while linked lists
▪ at level ℎ, nodes are filled from left to right are not
▪ The order is identical to that of a breadth-first traversal
Arr = [12 13 15 2 9 17 20 1] BFS
Ex: Ex
Left Child of 9 = 2*4+1 = 9 Parent of node 12 = (i/2)-1 =-1
right Child of 9 = 2*4+2= 10 -ve value means no parent
Node 9 don’t have left and right children
Ex
Parent of node 13 = (1/2)-1 =0
Ex:
Left Child of 15 = 2*2+1 = 5
right Child of 15 = 2*4+2= 6
# of edges in tree = # of nodes -1
Operations on Binary Trees
•Searching for an item
• Adding a new item at a certain position on the tree
• Deleting an item
So pay attention!
Binary Search Trees BST
5
Binary Search Trees are a type of binary tree with specific
properties that make them very efficient to search for a 3 7
value in the tree. 2 4 9
Binary Search Tree Property:
Here’s an example BST…
The value stored at a node is greater than the
value stored at its left child and less than the
value stored at its right child
Thus, the value stored at the root of a subtree
is greater than any value in its left subtree and
less than any value in its right subtree!!
WOW!
Is this better than searching a linked list? Yes !! ---> O(logN)
Now that’s!
BST Search(find) Implementation
node* search_N(node* root, int x)
{
if(root == NULL)
{
cout<< "the key not exist";
return NULL;
}
if (root->data == x)
{cout<< "the key is :"<<x;
return root;}
}
2- Insertion
Goal:
Insert value 13
Insert value v into a binary search tree
12
Idea:
If the tree is empty 5 18
Allocate a new node and put V into it 2 9 15 19
Point the root pointer to our new node. DONE! 1 3 13 17
12 12
x
5 18 5 18
2 9 15 19 2 9 15 19
1 3 17 1 3 13 17
x = NIL
y = 15
Does the order of inserting elements
into a tree matter?
Yes, certain orders produce very unbalanced trees!!
Unbalanced trees are not desirable because search time
increases!!
There are advanced tree structures (e.g.,"red-black
trees, AVL tree") which guarantee balanced trees
Why? Because we have to first use a binary search to find where to insert our
node and binary search is O(log n).
25
BST Insertion Implementation
Goal:
If the node was found,
Delete a given node z from a binary search tree delete it from the tree,
Idea: making sure to preserve
its ordering!
Case 1: z has no children
Delete z by making the parent of z point to NulL
15 15
5 16 5 16
3 12 20 3 12 20
10 13 z 18 23 10 18 23
6 delete
6
7 7
Case 2: z has one child
Delete z by making the parent of z point to z’s child, instead of to z
15 delete 15
z
5 16 5 20
3 12 20 3 12 18 23
10 13 18 23 10
6 6
7 7
29
Case 3: z has two children
By choosing the minimum node in z’s right subtree or
By choosing the maximum node in z’s left subtree
6
15
delete z
15
5 16
6 16
3 12 20
3 12 20
10 13 18 23
10 13 18 23
6
y 7
7
BST Delete Implementation Begin from root
Postorder Traversal
Preorder Traversal
There are 2 common ways to traverse a tree.
Each technique differs in the order that each node is visited during the traversal:
We can store this in an array after a quick traversal: To insert another node while maintaining the complete-
binary-tree structure, we must insert into the next array
location
BST BFS Implementation
void BFS_level_order()
The implementation was already discussed: {
• Create a queue and push the root node if (root == NULL) return;
onto the queue queue < node*> q;
• While the queue is not empty: q.push(root);
Push all of its children of the front node
onto the queue
while(!q.empty())
Pop the front node
{
node *current= q.front();
if (current->left!= NULL)
q.push(current->left);
if (current->right!= NULL)
q.push(current->right);
cout<<current->data<<" ";
q.pop();
} cout<<endl;}
Depth-first Traversal
Inorder Traversal: Visit second
Visit the nodes in the left subtree,
then visit the root of the tree, tree
then visit the nodes in the right subtree .
(Warning:
"visit" means that the algorithm ‘J’
does something with the values
AEHJMTY
Postorder Traversal
Visit last
Visit the nodes in the left subtree first,
then visit the nodes in the right subtree, tree
then visit the root of the tree
‘J’
‘E’ ‘T’
: AH E MYTJ
Preorder Traversal:
Visit first
Visit the root of the tree first,
then visit the nodes in the left subtree, tree
then visit the nodes in the right subtree
‘J’
‘E’ ‘T’
JEAHTMY
Tree
Traversals
5
3 7
2 5 9
Inorder: 2 3 5 5 7 9
Preorder: 5 3 2 5 7 9
Postorder: 2 5 3 9 7 5
BST DFS Implementation
15 15
6 18 6 18
3 7 17 20 3 7 17 20
2 4 13 2 4 13
9 9
Minimum = 2 Maximum = 20
Finding Min & Max of a BST
And here are recursive versions for you…
Hopefully you’re getting the idea that most tree functions can be done
recursively…
Binary Search Trees - Summary
Operations on binary search trees:
SEARCH O(h)
INSERT O(h)
Running time of basic operations on BST (logn)
DELETE O(h)