Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees … From www.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.pptwww.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.ppt.

Similar presentations


Presentation on theme: "Binary Search Trees … From www.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.pptwww.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.ppt."— Presentation transcript:

1 Binary Search Trees … From www.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.pptwww.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.ppt

2 What is a binary tree? Property 1: each node can have up to two successor nodes.

3 Property 2: a unique path exists from the root to every other node What is a binary tree? (cont.) Not a valid binary tree!

4 Some terminology The successor nodes of a node are called its children The predecessor node of a node is called its parent The "beginning" node is called the root (has no parent) A node without children is called a leaf

5 Some terminology (cont’d) 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 (Warning: some books define h as #levels-1). Full tree: every node has exactly two children and all the leaves are on the same level. not full!

6 What is the max #nodes at some level l? The max #nodes at level is where l=0,1,2,...,L-1

7 What is the total #nodes N of a full tree with height h? using the geometric series: l=0l=1l=h-1

8 What is the height h of a full tree with N nodes?

9 Why is h important? Tree operations (e.g., insert, delete, retrieve etc.) are typically expressed in terms of h. So, h determines running time!

10 max heightWhat is the max height of a tree with N nodes? min heightWhat is the min height of a tree with N nodes? N (same as a linked list) log(N+1)

11 How to search a binary tree? (1) Start at the root (2) Search the tree level by level, until you find the element you are searching for or you reach a leaf. Is this better than searching a linked list? No  O(N)

12 Search Binary Search Trees (BSTs) 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

13 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! Search Binary Search Trees (BSTs)

14 Where is the smallest element? Ans: leftmost element Where is the largest element? Ans: rightmost element Search Binary Search Trees (BSTs)

15 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

16 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

17 How to search a binary search tree? Is this better than searching a linked list? Yes !! ---> O(logN)

18 Tree node structure class Node: def __init__(self, info): self.info = info self.left = None self.right = None

19 Function NumberOfNodes Recursive implementation #nodes in a tree = #nodes in left subtree + #nodes in right subtree + 1 #nodes in left subtree + #nodes in right subtree + 1 What is the size factor? Number of nodes in the tree we are examining What is the base case? The tree is empty What is the general case? CountNodes(Left(tree)) + CountNodes(Right(tree)) + 1

20 Function RetrieveItem

21 What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? 1)When the key is found 2)The tree is empty (key was not found) What is the general case? Search in the left or right subtrees

22 Function InsertItem Use the binary search tree property to insert the new item at the correct place

23 Function InsertItem (cont.) Implementing insertion resursively e.g., insert 11

24 What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? The tree is empty What is the general case? Choose the left or right subtree Function InsertItem (cont.)

25 Insert 11

26 Yes, certain orders might produce very unbalanced trees! Does the order of inserting elements into a tree matter?

27 (cont.)

28 Unbalanced trees are not desirable because search time increases! Advanced tree structures, such as red-black trees, guarantee balanced trees. Does the order of inserting elements into a tree matter? (cont’d)

29 Function DeleteItem First, find the item; then, delete it Binary search tree property must be preserved!! We need to consider three different cases: (1) Deleting a leaf (2) Deleting a node with only one child (3) Deleting a node with two children

30 (1) Deleting a leaf

31 (2) Deleting a node with only one child

32 (3) Deleting a node with two children

33 Find predecessor (i.e., rightmost node in the left subtree) Replace the data of the node to be deleted with predecessor's data Delete predecessor node (3) Deleting a node with two children (cont.)

34 What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? Key to be deleted was found What is the general case? Choose the left or right subtree Function DeleteItem (cont.)

35 Tree Traversals There are mainly three ways to traverse a tree: 1)Inorder Traversal 2)Postorder Traversal 3)Preorder Traversal

36 36 Inorder Traversal: A * B + C / D ‘+’ ‘*’ ‘A’ ‘B’ ‘/’ ‘C’‘D ’ root Visit left subtree inorder firstVisit right subtree inorder last Visit root second

37 37 Preorder Traversal: + *A B / C D ‘+’ ‘*’ ‘A’ ‘B’ ‘/’ ‘C’‘D ’ root Visit left subtree preorder secondVisit right subtree preorder last Visit root first

38 38 Postorder Traversal: AB* CD/ + ‘+’ ‘*’ ‘A’ ‘B’ ‘/’ ‘C’‘D ’ root Visit left subtree postorder firstVisit right subtree postorder second Visit root last

39 39 Inorder Traversal: A E H J M T Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Visit left subtree firstVisit right subtree last Visit second

40 Inorder Traversal Visit the nodes in the left subtree, then visit the root of the tree, then visit the nodes in the right subtree Inorder(tree) If tree is not NULL Inorder(Left(tree)) Visit Info(tree) Inorder(Right(tree)) Warning Warning: "visit" implies do something with the value at the node (e.g., print, save, update, etc.).

41 41 Preorder Traversal: J E A H T M Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Visit left subtree secondVisit right subtree last Visit first

42 Preorder Traversal Visit the root of the tree first, then visit the nodes in the left subtree, then visit the nodes in the right subtree Preorder(tree) If tree is not NULL Visit Info(tree) Preorder(Left(tree)) Preorder(Right(tree))

43 43 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Visit left subtree firstVisit right subtree second Visit last Postorder Traversal: A H E M Y T J

44 Postorder Traversal Visit the nodes in the left subtree first, then visit the nodes in the right subtree, then visit the root of the tree Postorder(tree) If tree is not NULL Postorder(Left(tree)) Postorder(Right(tree)) Visit Info(tree)

45 Tree Traversals: another example

46 Function PrintTree We use "inorder" to print out the node values. Keys will be printed out in sorted order. Hint: binary search could be used for sorting! A D J M Q R T

47 Comparing Binary Search Trees to Linear Lists Big-O Comparison Operation Binary Search Tree Array- based List Linked List ConstructorO(1) DestructorO(N)O(1)O(N) IsFullO(1) IsEmptyO(1) RetrieveItem O(logN )* O(logN)O(N) InsertItemO(logN )* O(N) DeleteItemO(logN) * O(N) *assuming h=O(logN)

48 48 ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________ Qu es ti ons? Reading from course text:


Download ppt "Binary Search Trees … From www.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.pptwww.cse.unr.edu/~bebis/CS302/Lectures/BinarySearchTrees.ppt."

Similar presentations


Ads by Google