LinkedList and Trees

Download as pdf or txt
Download as pdf or txt
You are on page 1of 105

Linked List and Trees

element element element element

Head

Head NULL

element element element element

NULL NULL
Define a node of a linked list

What does a node have?


1. Data
element 2. Reference
Define a node of a linked list

What does a node have?


1. Data
element
a. What type of data? ⇒ Any
type (we will use int for
simplicity)
2. Reference
a. What type of reference?
i. What is “type” of
reference?
Define a node of a linked list

What does a node have?


2. Reference
element a. What type of reference?
i. What is “type” of
reference? => Type of
reference tells what
kind of object will be
referred to by the
reference variable
Define a node of a linked list

What does a node have?


2. Reference
element a. What type of reference?
i. What is “type” of reference? =>
Type of reference tells what kind
of object will be referred to by the
reference variable
ii. String s1 (s1 is a reference that
will refer to String objects)
iii. Student s1 (s1 will refer to student
object)
Define a node of a linked list

What does a node have?


2. Reference
element a. What will be the type of
reference “next"?
i. Since next will refer to
a Node (the next node
in the list) thus it will be
of same type as the
node
Define a linked list

What does a linked list


have?
element 1. It only has reference to
head of the list (the
very first node)
2. It may have other
properties like number
of nodes in the list, etc.
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is empty (and element is
passed to add() method)

WHEN SOMEONE CREATES A


LINKED-LIST OBJECT using the class that
we have defined. . . the head should be
NULL
Because LINKED LIST does not even
have the first node. . . .
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is empty (and element is
passed to addFirst() method)

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

Create a node with element


newNode
Element and put null in
“next” variable

Beacause newly created


NULL
node is NOT referring to any
other node . . . .
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is empty (and element is
passed to add() method)

Make head of the list refer to element


newNode
that node. . .

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 Node Object


head
2
newNode

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

NULL Node Object


head
2
newNode
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is NOT empty (and
element is passed to add() method)
● Make newly created node as the
1 current head

LinkedList Object

NULL Node Object


head
2
newNode
Linked List visualizations
● Adding elements to BEGINNING OF a list when list is NOT empty (and
element is passed to add() method)

The END RESULT will
newNode
Look like this. . . .
LinkedList Object

head 1
2

Newly created Node has reference to the previous head NULL


Linked List visualizations
● Adding elements to LAST OF a list when list is empty (and element is
passed to add() method)

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

Create a node with element


newNode
Element and put null in
“next” variable

Because newly created


NULL
node is NOT referring to any
other node . . . .
Linked List visualizations
● Adding elements to LAST OF a list when list is empty (and element is
passed to add() method)

Make head of the list refer to element


newNode
that node. . .

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)

element element element element

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)

element element element element

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

NEW NODE at the end of the list


Linked List visualizations
● Adding elements to LAST OF a list when list is NOT empty (and element is
passed to add() method)
Create a new node
With data = element
and
Next = null
element
element element element
element

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

head Start with temp at head. . .


Move temp until it reaches NULL
the last node 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
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

● Adding elements to a certain position between (1) and (sizeOfList


+ 1) (say position = 2)

NULL
Linked List visualizations

● Adding elements to a certain position between (1) and (sizeOfList


+ 1)

NULL
head

NULL

Create a new node that has the element and refers to


NULL

NULL
head

NULL

To insert it at any position “p” you have to go to


(p-1)th node that is JUST BEFORE the position you
want to insert at

NULL
temp
head

NULL

Start with temp (at head) . . .then temp must jump


(p-2) times to reach the (p-1)th node

NULL
Say p = 4 (I want to add a node at 4th position0 temp

head

NULL

After reaching the 3rd position . . .


Make newNode refer to the the next of temp
Say p = 4 (I want to add a node at 4th position0 temp

head

NULL

Then make
next of temp . .
.refer to the
new node
NULL

The final result will be. . .


NULL
NULL
Linked List visualizations
● Adding elements to a list when list is empty (and element is passed to add()
method)

Create a node with element


Element and put null in next
variable
Linked List visualizations
● Adding elements to a list when list is empty (and element is passed to add()
method)

Make head of the list refer to element


that node. . .

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)

A tree is an object that will only contain 1


reference, which is the reference to the root of the
tree

That root is of type BinaryTreeNode

When we create this tree object, the root is null


(That means the tree is EMPTY)
Tree Implementations
add(1)
1
root = null

null null

Create a new node. . . .


Tree Implementations
add(1)
root 1

null null

Make it root. . . .
Tree Implementations add(2)

root 1 2

null null null


null

Create a new node. . . .


Tree Implementations add(2)

root 1

null
2

null null

Make it left child of root. . . .


Tree Implementations add(3)

root 1 3

Create a new node. . . . null


null null
2

null null
Tree Implementations add(3)

root 1

2 3

null null
null null

Make it right child of root. . .


.
Tree Implementations add(4)

4
root 1
null null

2 3

null null
null null

Create new node. . . .


Tree Implementations add(4)

4
root 1
leftOf null null

Root
2 3

null null
null null

Make it left child of [left child of


root]. . . .
Tree Implementations add(4)

root 1
leftOf
Root
2 3

null null
4 null

Make it left child of [left child of


null root]. . . .
null
Tree Implementations add(5)

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

1 Compute the height of the


tree = 3
2 3

4 5 6 7

BFS Order:
BFS Traversal Using Recursion
Compute the height of the tree = 3

1 For loop will call


printNodesAtLevel() in the following
manner :
2 3
printNodesAtLevel(root, 1, 1)
printNodesAtLevel(root, 2, 1)
4 5 6 7 printNodesAtLevel(root, 3, 1)

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)

Node(2) is at the current level


2 3 . . .so it will be printed

4 5 6 7

BFS Order: 1 2
BFS Traversal Using Recursion

After returning from printing


1 the left child. . .

Right child will be printed . .


2 3 .in the similar manner . .

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)

Will find the node that contains


the element to be deleted, i.e
node 12
5 15

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)

Find the left and right


children of the “node”
node
5 15
left = null
right = null

node is not equal to


3 6 12 16
root
Tree Implementations : Deletion in a binary search tree

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

root delete (12)


10
currentNode
getParent(root,
node)
node
5 15 Starting from root
as the current
node,it will find
the parent of the
node object (2nd
3 6 12 16
parameter)
Tree Implementations : Deletion in a binary search tree

root delete (12)


10
currentNode getParent(root,
node)
node
5 15 Here 12 is the left
child of
currentNode. .. so
recursion will start
returning and 15
3 6 12 16
will be returned as
the parent
Tree Implementations : Deletion in a binary search tree

root delete (12)


10
parent getParent(root,
node)
node
5 15 Here 12 is the left
child of
currentNode. .. so
recursion will start
returning and 15
3 6 12 16
will be returned as
the parent
Tree Implementations : Deletion in a binary search tree
If node is the left child of
root delete (12) parent then make left child
10 as null node
parent
leftOfParent

5 15
12
rightOfParent

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


10

5 15

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (16)


10
findNode(root, 15)
node
Will find the node that contains
the element to be deleted, i.e
15
5 15

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


10 The node reference is
node then passed to
deleteNode()

5 15 deleteNode(node)

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15) deleteNode(node)

10 Find the left and right children of


the “node”
node
left = null
right = Node[16]

5 15 node is not equal to root

right

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


10
parent =
node getParent(root,node[15])

5 15

right

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


left = null
10 right = Node[16]
parent
Here the node to be deleted
node
has only RIGHT CHILD

5 15

right

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


left = null
10 right = Node[16]
parent
node Node is right of
parent

5 15

right

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


10 left = null
parent right = Node[16]
node
Node is right of parent

5 15 parent.setRight(right)

right

3 6 null 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


10 left = null
parent right = Node[16]

Node is right of parent

5 parent.setRight(right)

right

3 6 16
Tree Implementations : Deletion in a binary search tree

root delete (15)


10 left = null
parent right = Node[16]
right
Node is right of parent

5 16 parent.setRight(right)

3 6
Tree Implementations : Deletion in a binary search tree

Node (Assume: Not


10 the root node)

minSuccessor

5 15

3 6 12 16
Tree Implementations : Deletion in a binary search tree

Node (Assume: Not


12 the root node)

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

Node (Assume: Not


12 the root node)

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

You might also like