binary search trees (1)

15
BINARY SEARCH TREES Fariha Tasmin Jaigirdar Assistant Professor Daffodil International University

Upload: himadri-sen-gupta

Post on 23-Feb-2017

91 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Binary search trees (1)

BINARY SEARCH TREESFariha Tasmin JaigirdarAssistant ProfessorDaffodil International University

Page 2: Binary search trees (1)

BINARY SEARCH TREESA Binary Search Tree is a binary tree, which is either empty or

satisfies the following properties :1. Every node has a value and no two nodes have the same value (i.e.,

all the values are unique) root.2. If there exists a left child or left sub tree then its value is less than

the value of the3. The value(s) in the right child or right sub tree is larger than the

value of the root node.

Page 3: Binary search trees (1)

BINARY SEARCH TREES cont.

Page 4: Binary search trees (1)

Example: Constructing BST Figure 8.12 shows a binary search tree. Notice that this tree is obtained by inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree.

Fig 8.12

Page 5: Binary search trees (1)

Operation on BST

Page 6: Binary search trees (1)

INSERTING A NODE

Page 7: Binary search trees (1)

Example: INSERTING A NODE

After inserting 55

Page 8: Binary search trees (1)

10/29/08 Shaily Kabir,Dept. of CSE, DU 8

Inserting a Node into a BST

Rules

• Examine the root R.

• If the new value is less than the root’s, then it must be inserted at the left subtree.

• Otherwise it must be inserted at the right subtree.

• This process continues until the new value is compared with a leaf node and then it is

added as that node’s (leaf node) left or right child depending on its value.

Algorithm

• Insert_BST(Root, Left, Right, Item)

1. Set NewNode.Value := Item.

2. Insert(Root, NewNode).

End.

Here,

Root = Root of BST

Left = Left Subtree

Right = Right Subtree

Item = New Item to be inserted

Page 9: Binary search trees (1)

10/29/08 Shaily Kabir,Dept. of CSE, DU 9

Insert(Node, NewNode)

1. If Node = NULL then Node := NewNode and return.

2. If NewNode.Value < Node.Value then Insert(Node.Left, NewNode).

3. Else Insert(Node.Right, NewNode).

Example:

Figure: BST with 5 Nodes

New Item: 110

FF.Value=110 (FF is the New Node address)

Steps:

1. Insert(Root, FF) Insert(AA, FF)

2. Insert(AA->Right, FF) Insert(CC, FF)

3. Insert(CC->Right, FF)

AA

BB CC

DD EE

15

13 100

12 90 110FF

Page 10: Binary search trees (1)

DELETING A NODE

Often you need to delete an element from a binary search tree. Doing so is far more complex than adding an element into a binary search tree.

To delete an element from a binary search tree, you need to first locate the node that contains the element and also its parent node. Let current point to the node that contains the element in the binary search tree and parent point to the parent of the current node. The current node may be a left child or a right child of the parent node.

Page 11: Binary search trees (1)

There are two cases to consider:

Case 1: The current node does not have a left child, as shown in Figure 1(a). Simply connect the parent with the right child of the current node, as shown in Figure 1(b).

DELETING A NODE cont.

Figure 1 Case 1: the current node has no left child.

Page 12: Binary search trees (1)

DELETING A NODE: Case 1

Figure 2 Case 1: deleting node 10 from (a) results in (b).

For example, to delete node 10 in Figure 2(a). Connect the parent of node 10 with the right child of node 10, as shown in Figure 2(b).

Page 13: Binary search trees (1)

Case 2: The current node has a left child.

Let rightMost point to the node that contains the largest element in the left subtree of the current node and parentOfRightMost point to the parent node of the rightMost node, as shown in Figure 3(a). Note that the rightMost node cannot have a right child but may have a left child. Replace the element value in the current node with the one in the rightMost node, connect the parentOfRightMost node with the left child of the rightMost node, and delete the rightMost node, as shown in Figure 3(b).

DELETING A NODE: Case 2

Page 14: Binary search trees (1)

DELETING A NODE: Case 2

Figure 3 Case 2: the current node has a left child.

Page 15: Binary search trees (1)

For example, consider deleting node 20 in Figure 4(a). The rightMost node has the element value 16. Replace the element value 20 with 16 in the current node and make node 10 the parent for node 14, as shown in Figure 4(b).

DELETING A NODE: Case 2

Figure 4 Case 2: deleting node 20 from (a) results in (b).