binary search treebinary search tree binary search tree is a binary tree in which every node...
TRANSCRIPT
![Page 1: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/1.jpg)
Binary Search Tree
![Page 2: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/2.jpg)
Binary Search Tree
Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree.
1. Everything in a node’s left subtree is smaller than the node’s value
2. Everything in a node’s right subtree is bigger than the node’s value
3 The left and right subtree each must also be a binary search tree
![Page 3: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/3.jpg)
20
4 25
21 26
20
4 25
21 263 5
20
10 25
20
10 25
8 2621
99
25 100
![Page 4: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/4.jpg)
Operations on a Binary Search Tree
1. Insertion
2. Search
3. Deletion
4. Pre-order Traversal
5. In-order Traversal
6. Post-order Traversal
![Page 5: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/5.jpg)
Binary Search Tree Creation
13, 9, 5, 12, 16, 19
13
Construct a Binary Search Tree (BST) for the following sequence of numbers-
Step 1:
Root
13
Step 2:
Root
Insert or add 9 : 9 < 13, so insert 9 to the left of 13.
9
Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree.
![Page 6: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/6.jpg)
13
Step 3:
Root
• Insert or add 5 : As 5 < 13, so insert 5 to the left of 13.• As 5 < 9, so insert 5 to the left of 9.•
9
13, 9, 5, 12, 16, 19
5 13
Step 4:
Root
• Insert or add 12 : • As 12 < 13, so insert 12 to the left of 13.• As 12 >9, so insert 12 to the right of 9.•
9
13, 9, 5, 12, 16, 19
5 12
![Page 7: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/7.jpg)
13
Step 5:
Root
• Insert or add 16 :• As 16> 13, so insert 16 to the right of 13.•
9
13, 9, 5, 12, 16, 19
5 13
Step 6:
Root
• Insert or add 19 :• As 19 >13, so insert 19 to the right of 13.• As 19 >16, so insert 19 to the right of 16.•
9
13, 9, 5, 12, 16, 19
5 12
12
16
16
19
![Page 8: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/8.jpg)
6
3 9
1 4 118
6, 3, 9,1, 4, 8,11
3<6
1<6
9>6
11>98<94>3
Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree.
![Page 9: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/9.jpg)
Searching element in Binary Search tree
• Compare the element with the root of the tree.
• If the item is matched then return node value.
• Otherwise check if item is less than the element present on root, if so then move to the left sub-tree.
• If not, then move to the right sub-tree.
• Repeat this procedure recursively until match found.
• If element is not found then return NULL.20
4 25
21 263 5
![Page 10: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/10.jpg)
20
4 25
21 263 5
root=20
Search 21
Step 1: • Compare 21 with the root• 21>20 so move to the right sub-tree.
![Page 11: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/11.jpg)
root=25
20
4 25
21 263 5
Step 2: Now in right sub tree 25 is a rootCompare 21 with the root(25)21<25 so move to the left sub-tree.
![Page 12: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/12.jpg)
root=21
20
4 25
21 263 5
Step 3: Now in left sub tree 21 is a rootCompare 21 with the root(21)21 == 21 Match(found) return 21
NULL
![Page 13: Binary Search TreeBinary Search Tree Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree and only larger values in its right subtree](https://reader036.vdocuments.net/reader036/viewer/2022081419/5fcd2384d326756fef555988/html5/thumbnails/13.jpg)
20
4 25
21 373 5
30 43
29 35
root=20 Search 30
30>20(search in right sub tree ) In Right sub tree root=25
Search 30 30>25
In Right sub tree root=37 Search 30
30<37 In Left sub tree root=30
Search 30 30=30(found)