Top Banner
ECE 3231 Algorithms & Programming Binary Search Trees
43
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 7._BinarySearchTrees

ECE 3231Algorithms & Programming

Binary Search Trees

Page 2: 7._BinarySearchTrees

A Taxonomy of Trees

General Trees – any number of children / node

Binary Trees – max 2 children / node

Binary Search Trees

Page 3: 7._BinarySearchTrees

Review :Binary Tree

A binary tree is a tree with the following properties:

Each internal node has at most two children (degree of two)The children of a node are an ordered pair

We call the children of an internal node left child and right child

Alternative recursive definition: a binary tree is either

a tree consisting of a single node, ORa tree whose root has an ordered pair of children, each of which is a binary tree

Applications:arithmetic expressionsdecision processessearching

A

B C

F GD E

H I

Page 4: 7._BinarySearchTrees

Review :Binary Tree ADT

The BinaryTree ADT extends the Tree ADT, i.e., it inherits all the methods of the Tree ADTAdditional methods:

position leftChild(p)position rightChild(p)position sibling(p)

Update methods may be defined by data structures implementing the BinaryTree ADT

Page 5: 7._BinarySearchTrees

Review : Maximum Number of Nodes in a Binary Tree

The maximum number of nodes on depth i of a binary tree is 2i, i>=0.

The maximum nubmer of nodes in a binary tree of height k is 2k+1-1, k>=0.

Prove by induction.

122 1

0

k

k

i

i

Page 6: 7._BinarySearchTrees

Full Binary Tree

A full binary tree of a given height k has 2k+1–1 nodes.

Height 3 full binary tree.

Page 7: 7._BinarySearchTrees

Labeling Nodes In A Full Binary Tree

Label the nodes 1 through 2k+1 – 1. Label by levels from top to bottom.Within a level, label from left to right.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 8: 7._BinarySearchTrees

Node Number Properties

Parent of node i is node i / 2, unless i = 1.Node 1 is the root and has no parent.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 9: 7._BinarySearchTrees

Node Number Properties

Left child of node i is node 2i, unless 2i > n, where n is the number of nodes.If 2i > n, node i has no left child.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 10: 7._BinarySearchTrees

Node Number Properties

Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes.If 2i+1 > n, node i has no right child.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 11: 7._BinarySearchTrees

Complete Binary Trees

A labeled binary tree containing the labels 1 to n with root 1, branches leading to nodes labeled 2 and 3, branches from these leading to 4, 5 and 6, 7, respectively, and so on.A binary tree with n nodes and level k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of level k.

1

2 3

75

11

4

10

6

98 15141312

Full binary tree of depth 3

1

2 3

75

9

4

8

6

Complete binary tree

Page 12: 7._BinarySearchTrees

Binary Tree Traversals

Let l, R, and r stand for moving left, visiting the node, and moving right.

There are six possible combinations of traversallRr, lrR, Rlr, Rrl, rRl, rlR

Adopt convention that we traverse left before right, only 3 traversals remain

lRr, lrR, Rlrinorder, postorder, preorder

Page 13: 7._BinarySearchTrees

Inorder Traversal

In an inorder traversal a node is visited after its left subtree and before its right subtree

Algorithm inOrder(v)if isInternal (v)

inOrder (leftChild (v))visit(v)if isInternal (v)

inOrder (rightChild (v))

3

1

2

5

6

7 9

8

4

Page 14: 7._BinarySearchTrees

Print Arithmetic Expressions

Specialization of an inorder traversal

print operand or operator when visiting nodeprint “(“ before traversing left subtreeprint “)“ after traversing right subtree

Algorithm inOrder (v)if isInternal (v){

print(“(’’)inOrder (leftChild (v))}

print(v.element ())if isInternal (v){

inOrder (rightChild (v))print (“)’’)}

2

a 1

3 b ((2 (a 1)) (3 b))

Page 15: 7._BinarySearchTrees

Evaluate Arithmetic Expressions

recursive method returning the value of a subtreewhen visiting an internal node, combine the values of the subtrees

Algorithm evalExpr(v)if isExternal (v)

return v.element ()else

x evalExpr(leftChild (v))

y evalExpr(rightChild (v))

operator stored at vreturn x y

2

5 1

3 2

Page 16: 7._BinarySearchTrees

Binary search Trees BST

Binary search treeEvery element has a unique key.The keys in a nonempty left subtree (right subtree) are smaller (larger) than the key in the root of subtree.The left and right subtrees are also binary search trees.

Page 17: 7._BinarySearchTrees

Binary Search Trees

Binary Search Trees 17

6

92

41 8

Page 18: 7._BinarySearchTrees

Binary Search Trees (BST) are a type of Binary Trees with a special organization of data.

This data organization leads to O(log n) complexity for searches, insertions and deletions in certain types of the BST (balanced trees).

O(h) in general

Binary Search Trees

Page 19: 7._BinarySearchTrees

34 41 56 63 72 89 950 1 2 3 4 5 6

34 41 560 1 2

72 89 954 5 6

34 560 2

72 954 6

Binary Search algorithm of an array of sorted items reduces the search space by one half after each comparison

Binary Search Algorithm

Page 20: 7._BinarySearchTrees

63

41 89

34 56 72 95

• the values in all nodes in the left subtree of a node are less than the node value

• the values in all nodes in the right subtree of a node are greater than the node values

Organization Rule for BST

Page 21: 7._BinarySearchTrees

Binary Tree

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

} ;

sample binary search tree code

Page 22: 7._BinarySearchTrees

Searching in the BST

method search(key)

• implements the binary search based on comparison of the items in the tree

• the items in the BST must be comparable (e.g integers, string,

etc.)

The search starts at the root. It probes down, comparing the

values in each node with the target, till it finds the first item equal

to the target. Returns this item or null if there is none.

BST Operations: Search

Page 23: 7._BinarySearchTrees

if the tree is empty return NULL

else if the item in the node equals the targetreturn the node value

else if the item in the node is greater than the targetreturn the result of searching the left subtree

else if the item in the node is smaller than the targetreturn the result of searching the right subtree

Search in BST - Pseudocode

Page 24: 7._BinarySearchTrees

Search in a BST: C code

Ptnode search(ptnode root, int key)

{/* return a pointer to the node that contains key. If there is no such node, return NULL */

if (!root) return NULL; if (key == root->key) return root; if (key < root->key) return search(root->left,key); return search(root->right,key);}

Page 25: 7._BinarySearchTrees

method insert(key) places a new item near the frontier of the BST while retaining its organization of data:

starting at the root it probes down the tree till it finds a node whose left or right pointer is empty and is a logical place for the new valueuses a binary search to locate the insertion pointis based on comparisons of the new item and values of nodes in the BST

Elements in nodes must be comparable!

BST Operations: Insertion

Page 26: 7._BinarySearchTrees

9

7

5

4 6 8

Case 1: The Tree is EmptySet the root to a new node containing the item

Case 2: The Tree is Not EmptyCall a recursive helper method to insert the item 10

10 > 7

10 > 9

10

Page 27: 7._BinarySearchTrees

if tree is empty

create a root node with the new keyelse

compare key with the top node if key = node key

replace the node with the new valueelse if key > node key

compare key with the right subtree: if subtree is empty create a leaf node else add key in right subtree

else key < node key compare key with the left subtree:

if the subtree is empty create a leaf node else add key to the left subtree

Insertion in BST - Pseudocode

Page 28: 7._BinarySearchTrees

Insertion into a BST: C code

void insert (ptnode *node, int key){ ptnode ptr, temp = search(*node, key); if (temp || !(*node)) { ptr = (ptnode) malloc(sizeof(tnode)); if (IS_FULL(ptr)) { fprintf(stderr, “The memory is full\n”); exit(1); } ptr->key = key; ptr->left = ptr->right = NULL; if (*node) if (key<temp->key) temp->left=ptr; else temp->right = ptr; else *node = ptr; }}

Page 29: 7._BinarySearchTrees

The order of supplying the data determines where it is placed in the BST , which determines the shape of the BST

Create BSTs from the same set of data presented each time in a different order:

a) 17 4 14 19 15 7 9 3 16 10

b) 9 10 17 4 3 7 14 16 15 19

c) 19 17 16 15 14 10 9 7 4 3 can you guess this shape?

BST Shapes

Page 30: 7._BinarySearchTrees

removes a specified item from the BST and adjusts the tree

uses a binary search to locate the target item: starting at the root it probes down the tree till it finds the

target or reaches a leaf node (target not in the tree)

removal of a node must not leave a ‘gap’ in the tree,

BST Operations: Removal

Page 31: 7._BinarySearchTrees

method remove (key)I if the tree is empty return falseII Attempt to locate the node containing the target using the

binary search algorithmif the target is not found return falseelse the target is found, so remove its node:

Case 1: if the node has 2 empty subtrees replace the link in the parent with null

Case 2: if the node has a left and a right subtree - replace the node's value with the max value in the left subtree - delete the max node in the left subtree

Removal in BST - Pseudocode

Page 32: 7._BinarySearchTrees

Case 3: if the node has no left child - link the parent of the node - to the right (non-empty) subtree

Case 4: if the node has no right child - link the parent of the target - to the left (non-empty) subtree

Removal in BST - Pseudocode

Page 33: 7._BinarySearchTrees

9

7

5

64 8 10

9

7

5

6 8 10

Case 1: removing a node with 2 EMPTY SUBTREES

parent

cursor

Removal in BST: Example

Removing 4replace the link in the parent with null

Page 34: 7._BinarySearchTrees

Case 2: removing a node with 2 SUBTREES

9

7

5

6 8 10

9

6

5

8 10

cursorcursor

- replace the node's value with the max value in the left subtree- delete the max node in the left subtree

44

Removing 7

Removal in BST: Example

What other elementcan be used as replacement?

Page 35: 7._BinarySearchTrees

9

7

5

6 8 10

9

7

5

6 8 10

cursor

cursor

parentparent

the node has no left child:link the parent of the node to the right (non-empty) subtree

Case 3: removing a node with 1 EMPTY SUBTREE

Removal in BST: Example

Page 36: 7._BinarySearchTrees

9

7

5

8 10

9

7

5

8 10

cursorcursor

parentparent

the node has no right child:link the parent of the node to the left (non-empty) subtree

Case 4: removing a node with 1 EMPTY SUBTREE

Removing 5

4 4

Removal in BST: Example

Page 37: 7._BinarySearchTrees

Removal in BST: C code

Page 38: 7._BinarySearchTrees

The complexity of operations get, insert and remove in BST is O(h) , where h is the height.

O(log n) when the tree is balanced. The updating operations cause the tree to become unbalanced.

The tree can degenerate to a linear shape and the operations will become O (n)

Analysis of BST Operations

Page 39: 7._BinarySearchTrees

BST tree = new BST();

tree.insert ("E");tree.insert ("C");tree.insert ("D");tree.insert ("A");tree.insert ("H");tree.insert ("F");tree.insert ("K");

>>>> Items in advantageous order:K

HF

ED

CA

Output:

Best Case

Page 40: 7._BinarySearchTrees

BST tree = new BST();for (int i = 1; i <= 8; i++) tree.insert (i);

>>>> Items in worst order:

8

7

6

5

4

3

2

1

Output:

Worst Case

Page 41: 7._BinarySearchTrees

tree = new BST ();for (int i = 1; i <= 8; i++) tree.insert(random());

>>>> Items in random order:

X

U

P

O

H

F

B

Output:

Random Case

Page 42: 7._BinarySearchTrees

Applications for BST

Sorting with binary search treesInput: unsorted arrayOutput: sorted array

Algorithm ?Running time ?

Page 43: 7._BinarySearchTrees

Prevent the degeneration of the BST :A BST can be set up to maintain balance during updating operations (insertions and removals)Types of ST which maintain the optimal performance:

splay treesAVL trees2-4 TreesRed-Black treesB-trees

Better Search Trees