Lect6 - Binary Search Trees (BSTs) 1 Binary Search Trees...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Binary Search Trees (BSTs)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Binary Search Trees (BSTs) Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes O(n) time in a binary tree. In a binary tree of 10 6 nodes 10 6 steps required at least. In a BST this operation can be performed very efficiently: O(log 2 n). • A binary search tree of 10 6 nodes log 2 (10 6 ) 2245 20 steps only are required.
Background image of page 2
3 Binary Search Trees (BSTs) A binary search tree is a binary tree such that for each node, say N, the following statements are true: 1. If L is any node in the left subtree of N, then L is less than N. 2. If R is any node in the right subtree of N, then R is greater than N.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 BST: Example. 91 65 73 53 14 35 93 50 33 0 40 44 56 55 58 81 80 71 87 69
Background image of page 4
5 BST: Searching The search operation in a binary search tree can be carried out as: While (the target element is not found and there is more tree to search) do if the target element is “less than” the current element then search the left subtree else search the right subtree.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ADT Binary Search Tree Elements: The elements are nodes (BSTNode), each node contains the following data type: Type Structure: hierarchical structure; each node can have two children: left or right child; there is a root node and a current node. If N is any node in the tree, nodes in the left subtree < N and nodes in the right subtree > N. Domain:
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 23

Lect6 - Binary Search Trees (BSTs) 1 Binary Search Trees...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online