65
Binary Search Tree SWE2016-44

Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

  • Upload
    others

  • View
    21

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Binary Search Tree

SWE2016-44

Page 2: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

BST

Binary Search Tree is a node-based binary tree data structure.

2

Properties

1) The left subtree of a node contains nodes with keys less than the node’s key and the right subtree with keys greater than node’s key.

2) The left and right subtree each must also be a binary search tree and there must be no duplicate nodes.

Page 3: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

BST

1) Insertion

2) Searching

3) Delete

3

Page 4: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

While doing insertion in BST the new key is always inserted at leaf.

4

Page 5: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

While doing insertion in BST the new key is always inserted at leaf.

We start searching a key from root till we hit a leaf node.

5

Page 6: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

While doing insertion in BST the new key is always inserted at leaf.

We start searching a key from root till we hit a leaf node.

Once a leaf node is found, the new node is added as a child of the leaf node.

6

Page 7: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

For example, let’s create a BST by inserting multiple keys into it.

7

3

1

10

6

14

4

7

13

8

Page 8: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

For example, let’s create a BST by inserting multiple keys into it.

8

3

1

10

6

14

4

7

13

8

Page 9: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

For example, let’s create a BST by inserting multiple keys into it.

9

3

1

10

6

14

4

7

13

8

Page 10: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

For example, let’s create a BST by inserting multiple keys into it.

10

1

10

6

14

4

7

13

8

3

Page 11: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

11

1

10

6

14

4

7

13

Make sure the keys greater than the node’s key are added to the right and the smaller ones are added to the left.

8

3

Page 12: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

12

1

10

6

14

4

7

13

Make sure the keys greater than the node’s key are added to the right and the smaller ones are added to the left.

8

3

Page 13: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Make sure the keys greater than the node’s key are added to the right and the smaller ones are added to the left.

13

10

6

14

4

7

13

1

8

3

Page 14: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Make sure the keys greater than the node’s key are added to the right and the smaller ones are added to the left.

14

10

6

14

4

7

13

8

3

1

Page 15: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Keep adding the nodes according to the previous rule until no nodes are left.

15

10

6

14

4

7

13

8

3

1

Page 16: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Keep adding the nodes according to the previous rule until no nodes are left.

16

10

6

14

4

7

13

8

3

1

Page 17: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Keep adding the nodes according to the previous rule until no nodes are left.

17

6

14

4

7

13

8

3

1

10

Page 18: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Keep adding the nodes according to the previous rule until no nodes are left.

18

6

14

4

7

13

8

3

1

10

Page 19: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Keep adding the nodes according to the previous rule until no nodes are left.

19

6

14

4

7

13

8

3

1

10

Page 20: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Keep adding the nodes according to the previous rule until no nodes are left.

20

6

14

4

7

13

8

3

1

10

Page 21: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Keep adding the nodes according to the previous rule until no nodes are left.

21

14

4

7

13

8

3

1

10

6

Page 22: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Similarly add the rest of the nodes and you will get the BST needed.

22

7

13

4

8

3

1

10

6 14

Page 23: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Similarly add the rest of the nodes and you will get the BST needed.

23

7

13

8

3

1

10

6 14

4

Page 24: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Similarly add the rest of the nodes and you will get the BST needed.

24

13

8

3

1

10

6 14

4 7

Page 25: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion in a BST

Similarly add the rest of the nodes and you will get the BST needed.

25

8

3

1

10

6 14

4 7 13

Page 26: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

To search a given key in Binary Search Tree, we first compare it with root.

26

Page 27: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

To search a given key in Binary Search Tree, we first compare it with root.

If the key is present at root, we return root. If key is greater than root’s key, we recur for right subtree of root node.

27

Page 28: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Let’s search the key ‘4’ from the BST we made earlier.

28

8

3

1

10

6 14

4 7 13

Page 29: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Starting from the root, keep moving downwards until you find the key required.

29

8

3

1

10

6 14

4 7 13

Page 30: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Move to the right child if the key is greater than the selected node and left if it is smaller.

30

8

3

1

10

6 14

4 7 13

Page 31: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Move to the right child if the key is greater than the selected node and left if it is smaller.

31

8

3

1

10

6 14

4 7 13

Page 32: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Move to the right child if the key is greater than the selected node and left if it is smaller.

32

8

3

1

10

6 14

4 7 13

Page 33: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Move to the right child if the key is greater than the selected node and left if it is smaller.

33

8

3

1

10

6 14

4 7 13

Page 34: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Move to the right child if the key is greater than the selected node and left if it is smaller.

34

8

3

1

10

6 14

4 7 13

Page 35: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Move to the right child if the key is greater than the selected node and left if it is smaller.

35

8

3

1

10

6 14

4 7 13

Page 36: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Searching in a BST

Move to the right child if the key is greater than the selected node and left if it is smaller.

36

8

3

1

10

6 14

4 7 13

Page 37: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion and Searching in a BST

37

Complexity:

h is height of Binary Search Tree.

Page 38: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion and Searching in a BST

38

Complexity:

The worst case time complexity: O(h)

h is height of Binary Search Tree.

Page 39: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion and Searching in a BST

39

Complexity:

The worst case time complexity: O(h)

h is height of Binary Search Tree.

If the height of a skewed tree becomes n, the time complexity is O(n).

Page 40: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Insertion and Searching in a BST

40

Complexity:

The worst case time complexity: O(h)

h is height of Binary Search Tree.

If the height of a skewed tree becomes n, the time complexity is O(n).

On average, h ≈ log n ➔ O(log n)

Page 41: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

Deletion in BST has been divided into 3 cases.

1) Node to be deleted is leaf.

2) Node to be deleted has only one child.

3) Node to be deleted has two children.

41

Page 42: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

42

Case 1: Node to be deleted is leaf: Let’s delete the node with value '20'

50

30

20

70

40 8060

Page 43: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

43

To delete a leaf node simply remove it from the tree.

50

30

20

70

40 8060

Page 44: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

44

50

30 70

40 8060

To delete a leaf node simply remove it from the tree.

Page 45: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

45

Since it was a leaf node, we deleted it from the tree without making any other changes.

50

30 70

40 8060

Page 46: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

46

Case 2: Node to be deleted has only one child: Let’s delete the node with value '30'

50

30 70

40 8060

Page 47: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

47

To delete this node, copy the child to the node and delete the child.

50

30 70

40 8060

Page 48: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

48

To delete this node, copy the child to the node and delete the child.

50

70

40 8060

Page 49: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

49

To delete this node, copy the child to the node and delete the child.

50

40 70

8060

Page 50: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

50

The node had only one child so we copied its child’s value to it and deleted its child.

50

40 70

8060

Page 51: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

51

Case 3: Node to be deleted has two children: Let’s delete the root node with value '50'

50

40 70

8060

Page 52: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

52

First, find inorder successor of the node.

50

40 70

8060

Page 53: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

53

First, find inorder successor of the node.

50

40 70

8060

40 50 60 70 80: Inorder Traversal

Page 54: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

54

First, find inorder successor of the node.

50

40 70

8060

40 50 60 70 80: Inorder Traversal

Page 55: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

55

Now copy contents of the inorder successor to the node and delete the inorder successor.

50

40 70

8060

Page 56: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

56

Now copy contents of the inorder successor to the node and delete the inorder successor.

60

40 70

80

Page 57: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

57

The inorder predecessor can also be used in the same manner.

60

40 70

80

Page 58: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

58

Complexity:

h is height of Binary Search Tree.

Page 59: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Deletion in a BST

59

Complexity:

The worst case time complexity: O(h)

h is height of Binary Search Tree.

If the height of a skewed tree becomes n, the time complexity is O(n).

On average, h ≈ log n ➔ O(log n)

Page 60: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

BST vs Hash Table

60

Hash Table supports following operations in 𝚯 𝟏 time:

1. Search

2. Insert

3. Delete

For a Self Balancing Binary Search Tree the time

complexity for these oprations is:

𝐎 𝐥𝐨𝐠 𝐧

Page 61: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Advantages of BST over Hash Table

61

1. Can get all keys in sorted order by just doing Inorder

Traversal of BST

Page 62: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Advantages of BST over Hash Table

62

1. Can get all keys in sorted order by just doing Inorder

Traversal of BST

2. Doing order statistics, finding closest lower and

greater elements, doing range queries are easy to do

with BSTs

Page 63: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Advantages of BST over Hash Table

63

1. Can get all keys in sorted order by just doing Inorder

Traversal of BST

2. Doing order statistics, finding closest lower and

greater elements, doing range queries are easy to do

with BSTs

3. BSTs are easy to implement compared to hashing

Page 64: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Advantages of BST over Hash Table

64

1. Can get all keys in sorted order by just doing Inorder

Traversal of BST

2. Doing order statistics, finding closest lower and

greater elements, doing range queries are easy to do

with BSTs

3. BSTs are easy to implement compared to hashing

4. With Self Balancing BSTs, all operations are

guaranteed to work in 𝐎 𝐥𝐨𝐠 𝐧 time

Page 65: Binary Search Tree - SKKU · Binary Search Tree is a node-based binary tree data structure. 2 Properties 1) The left subtree of a node contains nodes with keys less than the node’s

Reference

• Charles Leiserson and Piotr Indyk, “Introduction to Algorithms”, September 29, 2004

• https://www.geeksforgeeks.org