LinkedList and Trees
LinkedList and Trees
LinkedList and Trees
Head
Head NULL
NULL NULL
Define a node of a linked list
LinkedList Object
element
head = null
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is empty (and element is
passed to add() method)
Node Object
LinkedList Object
head NULL
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is NOT empty (and
element is passed to add() method)
●
element
LinkedList Object
head
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is NOT empty (and
element is passed to add() method)
●
1 Create a node with
Element and put null in
LinkedList Object next variable
NULL
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is NOT empty (and
element is passed to add() method)
● Make the new node refer to
1 current head of the list
(Note: head is still referring to
that node)
LinkedList Object
LinkedList Object
head 1
2
NULL
LinkedList Object
head
Linked List visualizations
● Adding elements to LAST OF a list when list is empty (and element is
passed to add() method)
Node Object
●
LinkedList Object
head NULL
Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
LinkedList Object
head
NULL
Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
LinkedList Object
head
NULL
Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method) [What is the end result that we expect]
element
element
element element element
LinkedList Object
head NULL
LinkedList Object
head
NULL
NULL
Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element element element
element
LinkedList Object
head
The question is:
How will we reach the LAST NODE? (we don’t have any direct
NULL
reference to it. . . .SO WE WILL HAVE TO USE THE head to
reach the last node
Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element element element
element
LinkedList Object
element
element element element
element
LinkedList Object
head
The question is:
NULL
How will we know that it is the last node?
NULL
temp => The last node has next = null
Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element element element
element
LinkedList Object
head
NULL
NULL
temp
Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Make the last node refer to
the newly created node
element
element
element element element
LinkedList Object
head
NULL
temp
Linked List visualizations
NULL
Linked List visualizations
NULL
head
NULL
NULL
head
NULL
NULL
temp
head
NULL
NULL
Say p = 4 (I want to add a node at 4th position0 temp
head
NULL
head
NULL
Then make
next of temp . .
.refer to the
new node
NULL
head
Tree Implementations (Representation of a Binary NODE)
int data
left right
Could Could
Be null or could refer to some other Be null or could refer to some other
node (which will be the left child of this node (which will be the rightchild of this
node node
Tree Implementations (Representation of a Tree)
null null
null null
Make it root. . . .
Tree Implementations add(2)
root 1 2
root 1
null
2
null null
root 1 3
null null
Tree Implementations add(3)
root 1
2 3
null null
null null
4
root 1
null null
2 3
null null
null null
4
root 1
leftOf null null
Root
2 3
null null
null null
root 1
leftOf
Root
2 3
null null
4 null
root 1
rightOfRoot
2 3
null null
4 null
null null
Tree Implementations add(6),
add(7)
2 3
4 5 6 7
Tree Implementations
DFS 2 BFS 3
4 5 6 7
1. Left after right
Inorder: Left - Root - Right
Tree Implementations Preorder: Root - Left - Right
Postorder: Left - Right - Root
DFS 2 3
4 5 6 7
1. Left after right
Inorder: Left - Root - Right
Tree Implementations Preorder: Root - Left - Right
Postorder: Left - Right - Root
1 Inorder : 4, 2, 5, 1, 6, 3, 7
Preorder : 1, 2, 4, 5, 3, 6, 7
Postorder : 4, 5 , 2, 6, 7, 3, 1
DFS 2 3
4 5 6 7
DFS Variants
Inorder : 4, 2, 5, 1, 6, 3, 7
Preorder : [1], [2, 4, 5], [3, 6, 7]
Postorder : [4, 5 , 2], [6, 7, 3],
[1]
BFS Traversal Using Queue
1
1
2 3
4 5 6 7
BFS Order:
BFS Traversal Using QUeue
1 1
2 3
4 5 6 7
BFS Order:
BFS Traversal Using QUeue
1 1
2 3
4 5 6 7
BFS Order: 1
BFS Traversal Using QUeue
1 1
2
2 3
4 5 6 7
BFS Order: 1
BFS Traversal Using QUeue
1 1
2 3
2 3
4 5 6 7
BFS Order: 1
BFS Traversal Using QUeue
1 2
3
2 3
4 5 6 7
BFS Order: 1
BFS Traversal Using QUeue
1 2
3
2 3
4 5 6 7
BFS Order: 1 2
BFS Traversal Using QUeue
1 2
3 4 5
2 3
4 5 6 7
BFS Order: 1 2
BFS Traversal Using QUeue
2 3
4 5 6 7
BFS Order: 1 2 3 4 5 6 7
BFS Traversal Using Recursion
4 5 6 7
BFS Order:
BFS Traversal Using Recursion
Compute the height of the tree = 3
Or printNodesAtLevel(root, i, 1)
BFS Order: Read: print nodes at level i starting
from root which is at level 1
Code for printNodesAtLevel()
BFS Traversal Using Recursion
printNodesAtLevel(root, 1, 1)
1
level is equal to currentLevel
2 3 So the root node gets printed
4 5 6 7
BFS Order: 1
BFS Traversal Using Recursion
printNodesAtLevel(root, 2, 1)
1
Level is not equal to current
2 3 level and node is not null
either . . .
So a call to
4 5 6 7 printNodesAtLevel(node(2),2, 2)
BFS Order: 1
BFS Traversal Using Recursion
printNodesAtLevel(node(2), 2,
1 2)
4 5 6 7
BFS Order: 1 2
BFS Traversal Using Recursion
4 5 6 7
BFS Order: 1 2 3
Tree Implementations
Create a
Node
Tree Implementations
root
Make it
root
Tree Implementations
root
Tree Implementations
root
Tree Implementations : Deletion in a binary search tree
delete (12)
10
5 15
3 6 12 16
Tree Implementations : Deletion in a binary search tree
delete (12)
10
findNode(root, 12)
3 6 12 16
Tree Implementations : Deletion in a binary search tree
delete (12)
10
The node reference is
then passed to
deleteNode()
node
5 15
deleteNode(node)
3 6 12 16
Tree Implementations : Deletion in a binary search tree
delete (12)
root
10 deleteNode(node)
delete (12)
root
10
left = null
right = null
node
5 15 node is not equal to
root
and
It is a LEAF NODE
3 6 12 16
Tree Implementations : Deletion in a binary search tree
5 15
12
rightOfParent
3 6 null 16
Tree Implementations : Deletion in a binary search tree
5 15
3 6 null 16
Tree Implementations : Deletion in a binary search tree
3 6 null 16
Tree Implementations : Deletion in a binary search tree
5 15 deleteNode(node)
3 6 null 16
Tree Implementations : Deletion in a binary search tree
right
3 6 null 16
Tree Implementations : Deletion in a binary search tree
5 15
right
3 6 null 16
Tree Implementations : Deletion in a binary search tree
5 15
right
3 6 null 16
Tree Implementations : Deletion in a binary search tree
5 15
right
3 6 null 16
Tree Implementations : Deletion in a binary search tree
5 15 parent.setRight(right)
right
3 6 null 16
Tree Implementations : Deletion in a binary search tree
5 parent.setRight(right)
right
3 6 16
Tree Implementations : Deletion in a binary search tree
5 16 parent.setRight(right)
3 6
Tree Implementations : Deletion in a binary search tree
minSuccessor
5 15
3 6 12 16
Tree Implementations : Deletion in a binary search tree
minSuccessor
12
5 15
Temp = 10 (get data from the node to
be deleted)
3 6 16
Tree Implementations : Deletion in a binary search tree
minSuccessor
10
5 15
Temp = 10 (get data from the node to
be deleted)
3 6 16
Tree Implementations
Tree Implementations
Tree Implementations
Tree Implementations