Professional Documents
Culture Documents
Trees are used in applications in which the relation between data elements
needs to be represented in a hierarchy.
A tree is a nonlinear data structure that represent a hierarchical relationship
among the various data elements
B C D
E F G H I J K
L M
Defining Trees (Contd.)
B C D
E F G H I J K
L M node
Defining Trees (Contd.)
B C D
E F G H I J K
L M node
A tree is a collection of nodes connected by edges
A node contains
Data (e.g. an Object)
References (edges) to two or more subtrees or
children
Trees are hierarchical
A node is said to be the parent of its children
(subtrees)
There is a single unique root node that has no
parent
Nodes with no children are called leaf nodes
A tree with no nodes is said to be empty
Tree Terminology
A
Nodes E, F, G, H, I, J, L,
and M are leaf nodes.
B C D
E F G H I J K
L M
Tree Terminology (Contd.)
E F G H I J K
L M
E, F, G, and H are
children of node B. B is
the parent of these
nodes.
Tree Terminology (Contd.)
E F G H I J K
L M
Tree Terminology (Contd.)
E F G H I J K
L M
Tree Terminology (Contd.)
Internal node: It refers to any node between the root and a
leaf node.
Level of a node: It refers to the distance (in number of
nodes) of a node from the root. Root always lies at level 0.
As you move down the tree, the level increases by one.
Level 0
A
Nodes B, C, D, and K are
internal nodes. Level 1
B C D
E F G H I J K Level 2
L M Level 3
Tree Terminology (Contd.)
Level 0
A
Level 1
B C D
E F G H I J K Level 2
L M Level 3
Tree Terminology (Contd.)
Level 0
A
Level 1
B C D
E F G H I J K Level 2
L M Level 3
Just a minute
D E F G
H I
Just a minute
Answer:
a. 4
b. D and E
c. C
d. 2
e. H does not have any siblings
f. The only sibling of D is E
g. F, G, H, and I
Defining Binary Trees
Binary
Strictlytree is atree:
binary specific type of tree in which each node can
haveAat mosttree
binary twoinchildren namely
which every node,left child
except forand right
the leaf child.has
nodes,
non-empty
There left and
are various right
types children.
of binary trees:
Strictly binary tree
Full binary tree
Complete binary tree
Defining Binary Trees (Contd.)
A
Depth = 3
Total number of
3
nodes = 2 – 1 = 7
B C
D E F G
Defining Binary Trees (Contd.)
0 0 0
A A A
1 B 2 C 1 B 2 C 1 B 2 C
3 4 5 6 3 4 5 3 4 6
D E F G D E F D E G
555 555
555 555 555 555
555
501 701 501 701
501 501 701 501 701 501 701
555 501 701 358 513 561 797 345 490 724
21
Representing a Binary Tree (Contd.)
Data
Node
Representing a Binary Tree (Contd.)
root root
. 52 .
52
36 68 . 36 . 68 .
24 59 72 24 59 . 72 .
70 80 . 70 80
B is not NULL.
The left subtree of node A
Therefore, move to node BD to
to traverse
traverse the
the left
left
B.
subtree of A.
root
B C
D E F G
I
H
Inorder Traversal (Contd.)
B C
D E F G
I
H
D
Inorder Traversal (Contd.)
B C
D E F G
I
H
D
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C I
Inorder Traversal (Contd.)
B C
D E F G
I
H
D H B E A F C I
Inorder Traversal (Contd.)
Visit node G.
root
B C
D E F G
I
H
D H B E A F C I G
Inorder Traversal (Contd.)
root
B C
Traversal complete
D E F G
I
H
D H B E A F C I G
Preorder Traversal
B C
D E F G
I
H
Preorder Traversal: A B D H E C F G I
Postorder Traversal
B C
D E F G
I
H
Postorder Traversal: H D E B F I G C A
Just a minute
Answer:
Preorder
Implementing a Binary Search Tree
Which datayou
Therefore, structure
need towill you use to
implement storestructure
a data the id of that
the
customers?
provides the advantages of both arrays as well as linked lists.
Can you
A binary implement
search an array? the advantages of both arrays
tree combines
and linked lists.
Search operation in an array is fast.
However, insertion and deletion in an array is complex in nature.
In this case, the total number of customer ids to be stored is very
large. Therefore, insertion and deletion will be very time
consuming.
Can you implement a linked list?
Insert and delete operation in a linked is fast.
However, linked lists allow only sequential search.
If you need to access a particular customer id, which is located near
the end of the list, then it would require you to visit all the
preceding nodes, which again can be very time consuming.
Defining a Binary Search Tree
The following
Binary is anisexample
search tree a binaryoftree
a binary search
in which tree.
every node satisfies
the following conditions:
All values in the left subtree of a node are less than the value of
the node. 52
All values in the right subtree of a node are greater than the value
of the node.
36 68
24 44 59 72
40 55
Defining a Binary Search Tree (Contd.)
Search
To operation
search refersvalue,
for a specific to theyou
process
needoftosearching for a
perform the
specified value
following steps:in the tree.
1. Make currentNode point to the root node
2. If currentNode is null:
a. Display “Not Found”
b. Exit
3. Compare the value to be searched with the value of currentNode.
Depending on the result of the comparison, there can be three
possibilities:
a. If the value is equal to the value of currentNode:
i. Display “Found”
ii. Exit
b. If the value is less than the value of currentNode:
i. Make currentNode point to its left child
ii. Go to step 2
c. If the value is greater than the value of currentNode:
i. Make currentNode point to its right child
ii. Go to step 2
Just a minute
Answer:
smaller
Inserting Nodes in a Binary Search Tree
Beforean
Write inserting
algorithma node in a binary
to locate searchof
the position tree, younode
a new first to
need
be
to check in
inserted whether thesearch
a binary tree istree.
empty or not.
If the tree is empty, make the new node as root.
If the tree is not empty, you need to locate the appropriate
position for the new node to be inserted.
This requires you to locate the parent of the new node to be
inserted.
Once the parent is located, the new node is inserted as the left
child or right child of the parent.
To locate the parent of the new node to be inserted, you need
to implement a search operation in the tree.
Inserting Nodes in a Binary Search Tree (Contd.)
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
currentNode . 52 . currentNode:
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
currentNode . 52 . currentNode:
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
currentNode . 52 . currentNode:
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
currentNode . 52 . currentNode:
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
currentNode . 52 . currentNode:
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
currentNode . 52 . currentNode:
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
currentNode . 52 . currentNode:
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode
currentNode = NULL
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
Locate the position of a new node 55. 2. Make parent point to NULL
24 59 . 72 .
currentNode = NULL
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
24 59 . 72 .
currentNode = NULL
. 70 80
Inserting Nodes in a Binary Search Tree (Contd.)
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 59 . 72 . b. Exit
24 . 59 . 72 . b. Exit
24 59 . 72 . b. Exit
Deletean
Write operation in to
algorithm a binary
locate search tree refers
the position of thetonode
the process
to of
deletingfrom
deleted the specified node from
a binary search tree.the tree.
Before implementing a delete operation, you first need to
locate the position of the node to be deleted and its parent.
To locate the position of the node to be deleted and its parent,
you need to implement a search operation.
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
69
Deleting Nodes from a Binary Search Tree (Contd.)
currentNode
69
Deleting Nodes from a Binary Search Tree (Contd.)
currentNode
69
Deleting Nodes from a Binary Search Tree (Contd.)
node from the binary tree. 2. If currentNode is the root node: // If parent is
// NULL
69
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
currentNode
Deleting Nodes from a Binary Search Tree (Contd.)
parent
69
Delete operation complete
currentNode
Deleting Nodes from a Binary Search Tree (Contd.)
75
Deleting Nodes from a Binary Search Tree (Contd.)
75
Deleting Nodes from a Binary Search Tree (Contd.)
75
Deleting Nodes from a Binary Search Tree (Contd.)
Inorder_suc
Deleting Nodes from a Binary Search Tree (Contd.)
24 59 . 72
75 . 4. If the node marked Inorder_suc is a leaf node:
a. Delete the node marked Inorder_suc by
using the algorithm for Case I.
75
Inorder_suc
Deleting Nodes from a Binary Search Tree (Contd.)
75
Inorder_suc
Deleting Nodes from a Binary Search Tree (Contd.)
75
Delete operation complete
Inorder_suc
Activity: Implementing a Binary Search Tree
Problem Statement:
Write a program to implement insert and traverse operations on
a binary search tree that contains the words in a dictionary.
Summary
B C
D E F G
H K I M