Docsity

Binary Search Tree: Properties, Algorithm, and Deletion, Summaries of Design and Analysis of Algorithms

Partial preview of the text

Download Binary Search Tree: Properties, Algorithm, and Deletion and more Summaries Design and Analysis of Algorithms in PDF only on Docsity!

Binary Search Tree

What is a Binary Tree?

ļ‚§ Property 1: Each node can have up to two

successor nodes.

A Binary Tree

Binary Tree

*typedef struct tnode ptnode; typedef struct node { short int key; ptnode right, left; } ;

Basic Terminology

ļ‚§ (^) Nodes are organize in levels (indexed from 0). ļ‚§ (^) Level (or depth) of a node : number of edges in the path from the root to that node. ļ‚§ (^) Height of a tree h : #levels = L (some books define h as number of levels-1). ļ‚§ (^) Full tree: every node has exactly two children and all the leaves are on the same level. not full!

What is the max number of nodes at any level l? 0 ≤ 2 1 ≤ 2 2 ≤ 2 3 ≤ 2 where l=0,1,2, ...,L-

The maximum number of nodes at any level l is less

than or equal to 2

l

where l =0, 1, 2, 3,….. L-

What is the height h of a full tree with N nodes? 2 1 2 1 log( 1) (log ) h h N N h N O N āˆ’ = ⇒ = + ⇒ = + →

Why is h important?

ļ‚§ The Tree operations like insert, delete, retrieve etc.

are typically expressed in terms of the height of the

tree h.

ļ‚§ So, it can be stated that the tree height h determines

running time!

Binary Search

ļ‚§ (^) Suppose DATA is an array which is sorted in increasing numerical order and we want to find the location LOC of a given ITEM in DATA. Then there is an extremely efficient searching algorithm called Binary Search.

Binary Search

ļ‚§ (^) Consider that you want to find the location of some word in a dictionary. ļ‚§ (^) I guess you are not fool enough to search it in a linear way or in other words apply linear search. That is no one search page by page from the start to end of the book. ļ‚§ (^) Rather, I guess you will open or divide the dictionary in the middle to determine which half contains the word. ļ‚§ (^) Then consider new probable half and open or divide that half in the middle to determine which half contains the word. This process goes on. ļ‚§ (^) Eventually you will find the location of the word and thus you are reducing the number of possible locations for the word in the dictionary.

Binary Search Trees

ļ‚§ Binary Search Tree Property :

The value stored at

a node is greater than

the value stored at its

left child and less than

the value stored at its

right child

ļ‚§ In a BST, the value

stored at the root of

a subtree is greater

than any value in its

left subtree and less

than any value in its

right subtree!

Binary Search Trees

How to search a binary search tree?

1. Start at the root

2. Compare the value of

the item you are

searching for with the

value stored at the root

3. If the values are equal,

then item found ;

otherwise, if it is a leaf

node, then not found

How to search a binary search tree?

4. If it is less than the value

stored at the root, then

search the left subtree

5. If it is greater than the

value stored at the root,

then search the right

subtree

6. Repeat steps 2-6 for the

root of the subtree

chosen in the previous

step 4 or 5