CSE 2151 Lecture 6
CSE 2151 Lecture 6
CSE 2151 Lecture 6
Course No.: 0714 09 CSE 2151, Course Title: Data Structures and Algorithms
Electronics and Communication Engineering Discipline, Khulna University, Khulna
Md. Farhan Sadique
Email: [email protected]
This lecture is not a study material. Use this lecture as an outline. Follow the outline to study from the mentioned sources
after each section header. The sections of this lecture have been prepared from the mentioned sources.
Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child
relationship that satisfies the following properties:
• If T is nonempty, it has a special node, called the root of T, that has no parent.
• Each node v of T different from the root has a unique parent node w; every node with parent w is a
child of w.
Two nodes that are children of the same parent are siblings. A node v is external if v has no children. A node
v is internal if it has one or more children. External nodes are also known as leaves.
An edge of tree T is a pair of nodes (u, v) such that u is the parent of v, or vice versa. A path of T is a sequence
of nodes such that any two consecutive nodes in the sequence form an edge.
A tree is ordered if there is a meaningful linear order among the children of each node; that is, we purposefully
identify the children of a node as being the first, second, third, and so on. Such an order is usually visualized
by arranging siblings left to right, according to their order.
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
Let p be a position within tree T. The depth of p is the number of ancestors of p, other than p itself. We next
define the height of a tree to be equal to the maximum of the depths of its positions (or zero, if the tree is
empty).
2. Binary Trees (Goodrich et al.: 8.2, https://www.geeksforgeeks.org/complete-binary-tree/)
A binary tree is an ordered tree with the following properties:
• Every node has at most two children.
• Each child node is labeled as being either a left child or a right child.
• A left child precedes a right child in the order of children of a node.
The subtree rooted at a left or right child of an internal node v is called a left subtree or right subtree,
respectively, of v. A binary tree is proper if each node has either zero or two children. Some people also refer
to such trees as being full binary trees. Thus, in a proper binary tree, every internal node has exactly two
children. A binary tree that is not proper is improper. A complete binary tree is a special type of binary tree
where all the levels of the tree are filled completely except the lowest level nodes which are filled from as left
as possible.
2
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
3. Implementing Binary Trees (Goodrich et al.: 8.3.1)
A natural way to realize a binary tree T is to use a linked structure.
• addRoot(e): Creates a root for an empty tree, storing e as the element, and returns the root.
• addLeft(p, e): Creates a left child of node p, storing element e, and returns the new node.
• addRight(p, e): Creates a right child of node p, storing element e, and returns the new node.
• set(p, e): Replaces the element stored at node p with element e, and returns the updated node.
• attach(p, T1, T2): Attaches the internal structure of trees T1 and T2 as the respective left and right
subtrees of leaf node p and resets T1 and T2 to empty trees.
• remove(p): Removes the node p, replacing it with its child (if any), and returns the node that is now at
the position of removed node p.
3
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
4
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
5
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
6
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
4. Tree Traversal Algorithms (Goodrich et al.: 8.4, https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-
postorder/)
7
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
8
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
9
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
5. Binary Search Trees (Md. Rafiqul Islam and M. A. Mottalib: 7.2, https://www.geeksforgeeks.org/construct-bst-given-
level-order-traversal/ )
A binary search tree is a binary tree where all the node values of the left sub-tree are smaller than the node
value of the root of the tree, and all the node values of the right sub-tree are greater than the node value of
the root.
Construct a BST:
Construct a BST with the following list of values:
50, 15, 10, 13, 20, 22, 55, 60, 42, 57
Follow the steps below to solve the problem:
1) First, pick the first element of the array and make it root.
2) Pick the second element, if its value is smaller than the root node value, make it left child,
3) Else make it right child
4) Now recursively call step (2) and step (3) to make a BST from its level Order Traversal.
10
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
Heap Creation:
11
Data Structures and Algorithms – Lecture 6 Md. Farhan Sadique
Bibliography
• Book: Data Structures and Algorithms in Java, Sixth Edition - Michael T. Goodrich, Roberto Tamassia and
Michael H. Goldwasser
• Book: Data Structures Fundamentals, Second Edition – Md. Rafiqul Islam and M. A. Mottalib
• Website: https://www.geeksforgeeks.org/complete-binary-tree/
• Website: https://www.geeksforgeeks.org/construct-bst-given-level-order-traversal/
• Website: https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/
12