Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.

Similar presentations


Presentation on theme: "CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees."— Presentation transcript:

1 CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees

2 Objectives for Today Understand Trees/Terminology
Understand binary search trees Construct and use binary search trees Reading - K+W Chap 8

3 Tree Terminology root, leaf parent, child, sibling subtree
external, internal node ancestor, descendant depth, height

4 Binary Trees Each node has 0, 1, or 2 children

5 Binary Search Trees Binary search trees
Elements in left subtree < element in subtree root Elements in right subtree > element in subtree root Both the left and right subtrees are binary search trees

6 Binary Search Tree (Example)
Is this a binary search tree?

7 Binary Search Tree (Example)

8 Searching a BST Search algorithm: if the tree is empty, return NULL
if target equals to root node, return that data if target < root node, return search(left subtree) otherwise return search(right subtree)

9 find(7) find(12) find(0) find(14)

10 Inserting to a Binary Search Tree
if root is NULL replace empty tree with new data leaf; else if item < root->data return insert(left subtree, item) else return insert(right subtree, item)

11 Inserting to a Binary Search Tree

12 Removing from a Binary Search Tree
Item not present: do nothing Item present in leaf: remove leaf (change to null) Item in non-leaf with one child: Replace current node with that child Item in non-leaf with two children? Find largest item in the left subtree Recursively remove it Use it as the parent of the two subtrees (Could use smallest item in right subtree)

13 Removing from a Binary Search Tree

14 Order of operations?


Download ppt "CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees."

Similar presentations


Ads by Google