0% found this document useful (0 votes)
6 views

Lecture#07, DAA, Trees (1)

The document covers the design and analysis of algorithms focusing on trees and binary search trees (BST). It explains key concepts such as tree structure, types of tree traversals, and procedures for constructing and searching within a BST. Additionally, it includes sample problems and questions to reinforce understanding of BST properties and operations.

Uploaded by

areebababar1612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture#07, DAA, Trees (1)

The document covers the design and analysis of algorithms focusing on trees and binary search trees (BST). It explains key concepts such as tree structure, types of tree traversals, and procedures for constructing and searching within a BST. Additionally, it includes sample problems and questions to reinforce understanding of BST properties and operations.

Uploaded by

areebababar1612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Design & Analysis of Algorithms

Lecture#07
Tree, BST
Lecture Contents
✓Tree: What is it?
✓Binary Search Tree (BST)
✓Construct Tree (Add Node)
✓Tree Traversals (Pre-order, In-order, Post-order, Level-order)
✓BST Searching
✓Sample Problems
Tree … What is it?
Tree
A tree is a hierarchical data structure that stores data
values (called nodes) using links (node references).
Root
The node at the top of tree is known as root node.
Child/Parent Node
If x, y are the nodes in a tree and y lies directly under
the x, y is called the child node of x.
In this scenario, x is called the parent of y.
Height
Number of levels in a tree is called the height of tree
Binary Tree … What is it?
Binary Tree (B Tree)
A binary tree is a tree in which every node can have at most two child nodes.
First child is called the left child and other one is called the right child.
Number of Nodes in Binary Tree
Maximum number of nodes in a binary tree with height h are 2h+1-1
Number of Nodes in Binary Tree
Maximum number of nodes in a binary tree with height h are 2h+1-1
Full Binary Tree
Complete Binary Tree
Binary Search Tree(BST)
Binary Search Tree
A binary search tree is a binary tree that stores its nodes in a way that keys of
all nodes satisfy the binary-search-tree property.
Binary Search Tree Property
Let x be an arbitrary node in a binary tree search tree where y be any node in
left sub tree of x and z be any node in right subtree of x then
y.key ≤ x.key AND z.key > x.key
Complete Binary Search Tree
BST Traversals
Tree Traversal
Pre-Order Traversal
In-Order Traversal
Post-Order Traversal
Level-Order Traversal
BST Traversals … Pre-Order Traversal
proc pre-order-tree-walk(x)
if x = null then
return
else
print x.key
pre-order-tree-walk(x.left)
pre-order-tree-walk(x.right)
end if
end proc
BST Traversals … Pre-Order Traversal … Example
proc pre-order-tree-walk(x)
if x = null then
return
else
print x.key
pre-order-tree-walk(x.left)
pre-order-tree-walk(x.right)
end if
end proc
BST Traversals … In-Order Traversal
proc in-order-tree-walk(x)
if x = null then
return
else
in-order-tree-walk(x.left)
print x.key
in-order-tree-walk(x.right)
end if
end proc
BST Traversals … In-Order Traversal … Example
proc in-order-tree-walk(x)
if x = null then
return
else
in-order-tree-walk(x.left)
print x.key
in-order-tree-walk(x.right)
end if
end proc
BST Traversals … Post-Order Traversal
proc post-order-tree-walk(x)
if x = null then
return
else
post-order-tree-walk(x.left)
post-order-tree-walk(x.right)
print x.key
end if
end proc
BST Traversals … Post-Order Traversal … Example
proc post-order-tree-walk(x)
if x = null then
return
else
post-order-tree-walk(x.left)
post-order-tree-walk(x.right)
print x.key
end if
end proc
BST Traversals … Level-Order Traversal
proc level-order-tree-walk(x)
if x = null then
return
else
que as queue
que.enque(x)
while que.is_empty() = false do
n  que.deque()
print n
if x.left != null then
que.enque(x.left)
if x.right != null then
que.enque(x.right)
next
end if
end proc
BST Traversals … Level-Order Traversal … Example
proc level-order-tree-walk(x)
if x = null then
return
else
que as queue
que.enque(x)
while que.is_empty() = false do
n  que.deque()
print n
if x.left != null then
que.enque(x.left)
if x.right != null then
que.enque(x.right)
next
end if
end proc
BST Construction … Building a BST
proc insert_node(n)
if root = null then
root  n
else
return add_node(n, root)
end if proc add_node(node, parent)
if node.key > parent.key then
end proc if parent.right = null then
parent.right  node
else
return add_node(node, parent.right)
else
if parent.left = null then
parent.left  node
else
return add_node(node, parent.left)
end if
end proc
BST Construction … Building a BST … Example
proc insert_node(n)
if root = null then
root  n
else
add_node(n, root)
end if
proc add_node(node, parent)
end proc if node.key > parent.key then
if parent.right = null then
parent.right  node
else
return add_node(node, parent.right)
else
if parent.left = null then
parent.left  node
else
return add_node(node, parent.left)
end if
end proc
BST … Search Node
proc search_node(n, r)
if r = null OR r.key = n.key then
return r
else if n.key > r.key then
return search_node(n, r.right)
else
return search_node(n, r.left)
end proc
BST … Search Node … Example

proc search_node(n, r)
if r = null OR r.key = n.key then
return r
else if n.key > r.key then
return search_node(n, r.right)
else
return search_node(n, r.left)
end proc
BST … Sample Questions
Values: 43, 12, 8, 57, 90, 7, 15, 36, 24, 61
Construct a BST by adding above list of values one by one from left to right
BST … Sample Questions
Values: 43, 12, 8, 57, 90, 7, 15, 36, 24, 61
Let a BST was build using above data in given order. Which value will be printed
at end when traversed using
i. Pre-Order Traversal
ii. In-Order Traversal
iii. Post-Order Traversal
iv. Level-Order Traversal
BST … Sample Questions
Values: 43, 12, 8, 57, 90, 7, 15, 36, 24, 61
Let a BST was build using above data in given order. Which value will be printed
first when traversed using
i. Pre-Order Traversal
ii. In-Order Traversal
iii. Post-Order Traversal
iv. Level-Order Traversal
BST … Sample Questions
For the given set of key values as {6, 11, 4, 20, 2, 18, 8}, draw a BST of height
i. 2
ii. 3
iii. 4
iv. 5
v. 6
BST … Sample Questions
Suppose that a BST has all nodes with key values between 1 and 1000. If we
are searching a node with key value 664, does the following sequence of key
values validly represent the nodes visited?
525, 867, 610, 777, 690, 643, 662, 680, 671, 664
BST … Sample Questions
Write a procedure that receives the root of a binary tree as parameter and
returns whether or not the given binary tree is a BST.
Binary Search Trees … Sample Problems
1. What is binary search tree property?
2. When will print the largest value of a BST using in-order traversal?
3. When will print the largest value of a BST using pre-order traversal?
4. For A = {3, 7, 2, 18, 17, 20, 6}, can we build a BST of height 2, 3, 4, 5, 6, 7? If
yes, draw one to illustrate.
5. Write down in-order() function that does not use recursive calls.
6. Suppose that we have numbers between 1 and 1000 in a binary search
tree, and we want to search for the number 363. Which of the following
sequences could not be the sequence of nodes examined?
✓2, 252, 401, 398, 330, 344, 397, 363
✓924, 220, 911, 244, 898, 258, 362, 363
✓925, 202, 911, 240, 912, 245, 363
✓2, 399, 387, 219, 266, 382, 381, 278, 363
✓935, 278, 347, 621, 299, 392, 358, 363

You might also like