0% found this document useful (0 votes)
15 views28 pages

12.1 Trees

Uploaded by

kifal535
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)
15 views28 pages

12.1 Trees

Uploaded by

kifal535
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/ 28

CS820: ADVANCED DATABASE CONCEPTS

MSIT-2K24
TREES
Trees Introduction

Data Structure Rooted Tree

Linear Non-linear Tree Binary Tree

Array, Linked List, Binary Tree


Binary
Tree, Graph Search Tree
Stack, Queue
Trees Introduction
A rooted tree in which the
Rooted Tree number of children of each
node is at most two: we
represent the children by
Tree Binary Tree
left and right. And parent is
represented with p.

Binary
Binary Tree
Search Tree

A rooted tree in which the number of children of


each node is at most some constant k: we
represent the children by child1, child2, … ,childk.
The parent is represented with p.
Trees Introduction
7
Rooted Tree A rooted tree in which the
number of children of each node 4 9
is at most two. The order is
Tree Binary Tree important i.e., the left node must 2 6 12
be less than all nodes to its right.
5 7 10
Binary
Binary Tree
Search Tree

12 6
A rooted tree in which the number of children of each
node is at most two. The order is not important i.e., 7 9 4
the left node may or may not be less than its parent or
the right node. 5 7 10
Tree Traversing

Pre-order In-order Post-order


Root, Left, Right Left, Root, Right Left, Right, Root
12453 42513 45231
1. Visit the root. 1. Traverse the 1. Traverse the left
Depth 1st 2. Traverse the left subtree. subtree.
left subtree. 2. Visit the 2. Traverse the right
3. Traverse the root. subtree.
right subtree. 3. Traverse the 3. Visit the root.
right subtree.
Breadth-1st
Level Order Traversal: 1 2 3 4 5
Binary Search Tree (BST)
A binary search tree (BST) is a rooted binary tree whose internal nodes each store a key greater
than all the keys in the node's left subtree and less than those in its right subtree.

Every node may have 0, 1, or 2 children.

BST is always ordered or sorted binary tree

The keys in a binary search tree are always stored in such a way as to satisfy the
binary-search-tree property
Binary Search Tree (BST)

Binary-search-tree property

In Figure:
 The key of the root is 6.
 The keys 2, 5, and 5 in its left subtree are no larger than 6.
 The keys 7 and 8 in its right subtree are no smaller than 6.

The same property holds for every node in the tree.


 For example, the key 5 in the root’s left child is no smaller
than the key 2 in that node’s left subtree and no larger than
the key 5 in the right subtree.
Binary Search Tree (BST)

Querying a binary search tree


Finding the key of x
SEARCH
Finding minimum key
MINIMUM
BST Query Finding maximum key
MAXIMUM
Operations
Finding key of the minimum right of x
SUCCESSOR
Finding the key of the maximum left of x
PREDECESSOR
Binary Search Tree (BST)

Querying a binary search tree


Finding the key of x
SEARCH
Finding minimum key
MINIMUM
BST Query Finding maximum key  To search for the key 13 in the tree, we follow the path
Operations MAXIMUM
15  6  7  13 from the root.
Finding key of the
SUCCESSOR minimum right of x  The minimum key in the tree is 2, which is found by
Finding the key of the
following left pointers from the root.
PREDECESSOR maximum left of x  The maximum key 20 is found by following right pointers
from the root.
 The successor of the node with key 15 is the node with
key 17, since it is the minimum key in the right subtree of
15.
 The predecessor of the node with key 6 is the node with
key 4, since it is the maximum key in the left subtree of 6.
Binary Search Tree (BST)

Searching Algorithm
Given a pointer to the root of the tree and a
key k, TREE-SEARCH returns a pointer to a
node with key k if one exists; otherwise, it
returns NIL.
Binary Search Tree (BST)
Searching Algorithm (Recursion)

Let search of node with key = 13


pointer to x k X == nil k == x.key return x k < x.key return TREE-SEARCH (x.left, k) return TREE-SEARCH (x.rigth, k)
pointer to 15 13 N N Y return TREE-SEARCH(6, 13)
pointer to 6 13 N N N return TREE-SEARCH(7, 13)
pointer to 7 13 N N N return TREE-SEARCH(13, 13)
pointer to 13 13 N Y Pointer to 13
Binary Search Tree (BST)
Searching Algorithm (Iteration)

Let search of node with key = 13


pointer to x k 𝒙 ≠ 𝒏𝒊𝒍 𝒌 ≠ 𝒙. 𝒌𝒆𝒚 𝒌 < 𝒙. 𝒌𝒆𝒚 𝒙 = 𝒙. 𝒍𝒆𝒇𝒕 𝒙 = 𝒙. 𝒓𝒊𝒈𝒉𝒕 return x

pointer to 15 13 Y Y Y 𝒙=𝟔
pointer to 6 13 Y Y N 𝒙=𝟕
pointer to 7 13 Y Y N 𝒙 = 𝟏𝟑
pointer to 13 13 Y N Pointer to 13
Binary Search Tree (BST)
MINIMUM Algorithm

pointer to x 𝒙. 𝒍𝒆𝒇𝒕 ≠ 𝑵𝑰𝑳 𝒙 = 𝒙. 𝒍𝒆𝒇𝒕 return x

pointer to 15 N 𝒙=𝟔
pointer to 6 N 𝒙=𝟑
pointer to 3 N 𝒙=𝟐
pointer to 2 Y Pointer to 2
Binary Search Tree (BST)
MAXIMUM Algorithm

pointer to x 𝒙. 𝒓𝒊𝒈𝒉𝒕 ≠ 𝑵𝑰𝑳 𝒙 = 𝒙. 𝒓𝒊𝒈𝒉𝒕 return x

pointer to 15 N 𝒙 = 𝟏𝟖
pointer to 18 N 𝒙 = 𝟐𝟎
pointer to 20 Y Pointer to 20
BST CONSTRUCTION
Binary Search Tree (BST)
A binary search tree (BST) is a rooted binary tree whose internal nodes each store a key greater
than all the keys in the node's left subtree and less than those in its right subtree.

Every node may have 0, 1, or 2 children.

BST is always ordered or sorted binary tree

Binary-search-tree property
Binary Search Tree (BST)
 Construction of BST for the following items
12, 5, 14, 3, 30, 50, 20, 8 12

5 14

12 12
3 30
12
12 12 5 14 5 14 20 50
12 14
5
5 5 14 3 30 3 30
3
50 12

5 14

3 8 30

20 50
Binary Search Tree (BST)
 Insertion
 Let we want to insert a node with value = 40.
 First, we must find the right location for 40.
 Move the value 50 to the right to create place for 40.
 Insert 40 by creating a new node.

12
12
5 14
5 14
3 8 30
3 8 30

20 40
20 50
50
Binary Search Tree (BST)
Insertion Algorithm Node z for which z.key = v , z.left = NIL, and z.right = NIL.

 Inserting an item with key 13.


 Red lines indicate the path from root down to the value to
be inserted.
 The dashed line indicates the link in the tree that is added to
insert the item.
Binary Search Tree (BST)
Insertion Algorithm

y x 𝐱 ≠ 𝑵𝑰𝑳 y=x 𝒛. 𝒌𝒆𝒚 < 𝒙. 𝒌𝒆𝒚 𝒙 = 𝒙. 𝒍𝒆𝒇𝒕 𝒙 = 𝒙. 𝒓𝒊𝒈𝒉𝒕 z.p = y y == NIL T.Root = z 𝒛. 𝒌𝒆𝒚 < 𝒚. 𝒌𝒆𝒚 𝒚. 𝒍𝒆𝒇𝒕 = 𝒛 𝒚. 𝒓𝒊𝒈𝒉𝒕 = 𝒛

NIL 12 True Y = 12 13 < 12 (False) x = 18

True Y = 18 13 < 18 (True) x = 15

True Y = 15 13 < 15 (True) X = NIL

False z.p = 15 False 13 < 15 y.left = 13


BST DELETION
Binary Search Tree (BST)
 Deletion:
 Deleting a node create three situations
 Deleting a node with 0 child
 Just delete the element, don’t do anything else

12
 Deleting a node with 1 child
 Replace the element with its child 5 14

 Deleting a node with 2 children 3 8 30


 Inorder predecessor
 Inorder successor 40
20

50
Binary Search Tree (BST)
 Deletion:
 Deleting a node with 0 child.
 Let delete a node with 0 child i.e. 50.

12 12

5 14 5 14

3 8 30 3 8 30

20 40 20 40

50
Binary Search Tree (BST)
 Deletion:
 Deleting a node with 1 child
 Let delete a node with 1 child i.e. 14.

12
12
5 30
5 14
3 8
3 8 30 40
20

20 40
Binary Search Tree (BST)
 Deletion (Inorder predecessor):
 Deleting a node with 2 children
 Let delete a node with 2 children i.e. 12.

8
12
5 30
5 30
3
3 8 Inorder predecessor 20 40
20 40  Replace the node to
be deleted with the
node of maximum
element in the left
tree.
Binary Search Tree (BST)
 Deletion (Inorder Successor):
 Deleting a node with 2 children
 Let delete a node with 2 children i.e. 12.

20
12
5 30
5 30
3 8
3 8 Inorder successor 40
20 40  Replace the node to
be deleted with the
node of minimum
element in the right
tree.
THANK YOU

You might also like