Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node...

53
Chapter 8 Binary Search Tree 1 Fall 2010

Transcript of Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node...

Page 1: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Chapter 8Binary Search Tree

1

Fall 2010

Page 2: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Trees

2

A structure with:

i) a unique starting node (the root), in which

ii) each node has up to two child nodes, and

iii) a unique path exists from the root to every other node

Root

Top node of a tree structure; a node with no parent

Leaf Node

A tree node that has no children

Page 3: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Trees: level and height

3

• Level: distance of a node from the root• Height: the maximum level

Page 4: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Trees

4

Why is this not atree?

A tree is a structure with i) a unique starting node (the root), in which ii) each node can have up to two child nodes and iii) a unique path exists from the root to every other node

Page 5: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Descendants

5

Q

V

T

K S

A E

L

How many descendants does Q have?

Descendant of a node is a child of the node, and any child of the children, etc.

Page 6: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Ancestors

6

Q

V

T

K S

A E

L

How many ancestors does S have?

Ancestor of a node: a parent of the node, the parent of the parent, etc.

Page 7: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Trees Come in Different Shapes

7

How many structurally different binary trees can be madefrom 2 nodes? 4 nodes? 6 nodes?

Page 8: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Tree Facts• Max. number of nodes at Nth level : 2N.

– 0th level: root node: 1– 1st level: 2– Double as level increases by 1

• Suppose f(n) denotes the maximum number of nodes at the nth level:– f(0)=1– f(n)=2*f(n-1) for n>=1

• A recursive formula! • f(n)=2n

8

Page 9: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Tree Facts• Max. total number of nodes in a tree of height N

– All levels are full, so add the max. number of nodes at each level together:

– 20+21+22+…+2N= 2N+1-1

9

Page 10: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Tree Facts

• Given a binary tree with N nodes, what is the min. number of levels? – Try to fill in each level– The answer is: log2N + 1

• Max. number of levels with N nodes ?– One node at each level => degenerates to a

linked list– The answer is: N

10

Page 11: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Search Trees (BST)

11

A BST is a binary tree with a search property.

A binary tree in which, for each node:

• key value in the node is greater than the key value in its left child and any of the left child’s descendents (left sub-tree)

• key value in the node is less than the key value in its right child and any of the right child’s descendents (right sub-tree)

Page 12: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Search Trees

12

Each node is the root of a subtree rooted at the node

Page 13: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Binary Search Tree ADT• Application level: same as List• Logic Level

– void MakeEmpty()– bool IsEmpty()– bool IsFull()– int GetLength()– RetrieveItem(ItemType &item, bool &found)– InsertItem (ItemType item)– DeleteItem (ItemType item)– Print(ofstream &outFile)– ResetTree(OrderType order)– GetNextItem (ItemType &item, OrderType order,bool &finished)

Page 14: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Tree node in BST

14

Can you define a structure to represent a binary tree node ?

Page 15: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Count

15

Let’s start by counting the number of nodes in a tree:

Size?

Base cases(s)?

General case(s)?

Page 16: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Count: version 1

16

if (Left(tree) is NULL) AND (Right(tree) is NULL)

return 1

else

return Count(Left(tree)) + Count(Right(tree)) + 1

Apply tothese trees:

Page 17: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Count: version 2

17

if (Left(tree) is NULL) AND (Right(tree) is NULL)return 1

else if (Left(tree) is NULL)return Count(Right(tree)) + 1

else if (Right(tree) is NULL)return Count(Left(tree)) + 1

else return Count(Left(tree)) + Count(Right(tree)) + 1

Apply to an empty tree

Page 18: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Count: version 3

18

if (tree is NULL) return 0if (Left(tree) is NULL) AND (Right(tree) is NULL)

return 1else if (Left(tree) is NULL)

return Count(Right(tree)) + 1else if (Right(tree) is NULL)

return Count(Left(tree)) + 1else return Count(Left(tree)) + Count(Right(tree)) + 1

Page 19: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Count: version 4

19

if (tree is NULL)return 0

elsereturn Count(Left(tree)) + Count(Right(tree)) + 1

Page 20: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Count: implementation

20

int TreeType::GetLength() const

{

return Count(root);

)

int TreeType::Count(TreeNode* tree) const

{

if (tree == NULL)

return 0;

else

return Count(tree->left) + Count(tree->right) + 1;

}

Why do we need two functions?

Page 21: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Search

21

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’

‘V’

‘P’ ‘Z’‘D’

‘Q’‘L’‘B’

‘S’Are ‘D’, ‘Q’, and ‘N’ in the tree?

Page 22: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Search

22

Retrieve(tree, item, found)

Size?

Base case(s)?

General case(s)?

Page 23: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Searchvoid TreeType::Retrieve(TreeNode* tree, ItemType& item, bool& found) const{ if (tree == NULL) found = false; //base case else if (item < tree->info) Retrieve(tree->left, item, found); else if (item > tree->info) Retrieve(tree->right, item, found); else //base case { item = tree->info; found = true; }}

23

Page 24: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Shape of BST• Shape depends on the order of item insertion• Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order• The first value inserted is always put in the root

24

‘J’

Page 25: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Shape of BST• Thereafter, each value to be inserted:

– compares itself to the value in the root node– moves left it is less or – moves right if it is greater

• When does the process stop?

25

‘J’

‘E’

Page 26: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Shape of BST• Trace path to insert ‘F’

26

‘J’

‘E’

‘F’

Page 27: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Shape of BST

• Trace path to insert ‘T’

27

‘J’

‘E’

‘F’

‘T’

Page 28: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Shape of BST

• Trace path to insert ‘A’

28

‘J’

‘E’

‘F’

‘T’

‘A’

Page 29: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Shape of BST• Now build tree by inserting ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order

29

And the moral is?

Page 30: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Insertion

30

Insert an item into a tree

Where does each new node get inserted?

Insert(tree, item)if (tree is NULL)

Get a new node and insert at this locationSet right and left to NULLSet info to item

else if (item is larger than tree->info) Insert(tree->right, item)else Insert(tree->left, item)

Page 31: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Insertion

31

Page 32: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Insertion

32

Insert item 12

Page 33: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Insertion

33

How mustthe tree

bepassed?

Page 34: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Insertionvoid TreeType::Insert(TreeNode* & tree, ItemType item){ if (tree == NULL) { // Insertion place found. tree = new TreeNode; tree->right = NULL; tree->left = NULL; tree->info = item; } else if (item < tree->info) Insert(tree->left, item); else Insert(tree->right, item); }

34

Page 35: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Deleting a Leaf Node

35

Page 36: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Deleting a Node with One Child

36

Page 37: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Deleting a Node with Two Children

37

Page 38: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Delete an existing item from BST

38

Can you summarize the three deletion cases?

1.Deleting a leaf node.

2.Deleting a node with only one child.

3.Deleting a node with two children.

Page 39: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Predecessor

• Predecessor: the element whose key immediately precedes (less than) the key of item– If the item node has a left child, the largest

element in the left subtree (right-most child)– If the item has no left child, …

39

Page 40: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Successor• Successor: the element whose key

immediately follows (greater than) the key of item– If the item node has two children, the smallest

element in the right subtree (left-most child) – If the item node has no right child, …

40

Page 41: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive DeletionDeleteItem(tree, item)if (Left(tree) is NULL) AND (Right(tree) is NULL) // delete ‘Z’

Set tree to NULLelse if (Left(tree) is NULL AND (Right(tree)) is not NULL) delete ‘R’ Set tree to Right(tree)else if (Right(tree) is NULL AND (Left(tree)) is not NULL)

Set tree to Left(tree)else // delete ‘Q’, maintain a binary search tree

Find predecessorSet Info(tree) to Info(predecessor)Delete predecessor

41

Page 42: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Deletion• TreeType::DeleteItem (ItemType item)

– deletes item from the current object (a tree object)

• void Delete( TreeNode*& tree, ItemType item) – deletes item from a tree rooted at tree

• void DeleteNode( TreeNode*& tree)

– deletes node pointed to by tree from a BST• void GetPredecessor( TreeNode* tree, ItemType&

data)

– finds data’s predecessor – the largest item in data’s left subtree and saves the info into data.

42

Page 43: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Deletionvoid TreeType::DeleteItem(ItemType item){ Delete(root, item); } //Calls the recursive function Delete to//delete item from tree.

43

Page 44: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Deletion// first, find which node should be deleted.void TreeType::Delete(TreeNode*& tree, ItemType item){ if (item < tree->info) Delete(tree->left, item); else if (item > tree->info) Delete(tree->right, item); else DeleteNode(tree); // Node found}

44

Page 45: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Deletionvoid TreeType::DeleteNode(TreeNode*& tree){ ItemType data; TreeNode* tempPtr; tempPtr = tree; if ( tree->left == NULL) { tree = tree->right; delete tempPtr; } else if (tree->right == NULL){ tree = tree->left; delete tempPtr; }else{ GetPredecessor(tree->left, data); tree->info = data; Delete(tree->left, data);

}}

45Tracing this function using various examples…

Page 46: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Find Predecessorvoid TreeType::GetPredecessor( TreeNode* tree, ItemType& data){ //the largest item is located in its rightmost node. while (tree->right != NULL) tree = tree->right; data = tree->info;}

• This function could be named GetLargestItem() as it returns the largest item stored in tree rooted at tree.

46

Page 47: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Recursive Deletion

47

Page 48: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Traversals

• Tree Traversal : visiting all the nodes of a tree– Depth-First Traversal, Breadth-First Traversal

• Depth-First Traversal– Inorder Traversal– Preorder Traversal– Postorder Traversal

48

Page 49: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Inorder Traversal

• Inorder traversal visits the root in between visiting the left and right subtrees:

– Inorder traversal only makes sense for binary trees.

• Inorder(tree) if tree is not NULL

1. Inorder(Left(tree))2. Visit Info(tree)3. Inorder(Right(tree))

49

 

A B C D E F G

Page 50: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Preorder Traversal

• Preorder traversal visits the root first.

• PreOrder(tree)

if tree is not NULL1. Visit Info(tree)

2. Preorder(Left(tree))

3. Preorder(Right(tree)) D B A C F E G

50

Page 51: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Postorder Traversal

• Postorder traversal visits the root last. • PostOrder(tree)

if tree is not NULL1. Postorder(Left(tree))

2. Postorder(Right(tree))

3. Visit Info(tree)

51

A C B E G F D

Page 52: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Traversals

52

Page 53: Chapter 8 Binary Search Tree 1 Fall 2010. Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.

Printing the Tree

53

Traversal Algorithm : Inorder traversal