Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees CS 308 – Data Structures What is a binary tree? Property1: each node can have up to two successor nodes (children) –The predecessor.

Similar presentations


Presentation on theme: "Binary Search Trees CS 308 – Data Structures What is a binary tree? Property1: each node can have up to two successor nodes (children) –The predecessor."— Presentation transcript:

1

2 Binary Search Trees CS 308 – Data Structures

3 What is a binary tree? Property1: each node can have up to two successor nodes (children) –The predecessor node of a node is called its parent –The "beginning" node is called the root (no parent) –A node without children is called a leaf

4 3 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Tree Has a Root Node ROOT NODE

5 4 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Leaf nodes have no children LEAF NODES

6 Property2: a unique path exists from the root to every other node What is a binary tree? (cont.)

7 Some terminology Ancestor of a node: any node on the path from the root to that node Descendant of a node: any node on a path from the node to the last node in the path Level (depth) of a node: number of edges in the path from the root to that node Height of a tree: number of levels (warning: some books define it as #levels - 1)

8 7 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Tree Has Levels LEVEL 0

9 8 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Level One LEVEL 1

10 9 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Level Two LEVEL 2

11 10 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Subtree LEFT SUBTREE OF ROOT NODE

12 11 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Another Subtree RIGHT SUBTREE OF ROOT NODE

13 What is the # of nodes N of a full tree with height h? using the geometric series: The max #nodes at level is l=0l=1l=h-1

14 What is the height h of a full tree with N nodes? max height The max height of a tree with N nodes is N (same as a linked list) min height The min height of a tree with N nodes is log(N+1)

15

16 Searching a binary tree 1)(1) Start at the root 2)(2) Search the tree level by level, until you find the element you are searching for (O(N) time in worst case) Is this better than searching a linked list? No ---> O(N)

17 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 Thus, 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!!

18

19 Searching a binary search tree 1)(1) Start at the root 2)(2) Compare the value of the item you are searching for with the value stored at the root 3)(3) If the values are equal, then item found; otherwise, if it is a leaf node, then not found

20 Searching a binary search tree (cont.) 4)(4) If it is less than the value stored at the root, then search the left subtree 5)(5) If it is greater than the value stored at the root, then search the right subtree 6)(6) Repeat steps 2-6 for the root of the subtree chosen in the previous step 4 or 5 Is this better than searching a linked list? Yes !! ---> O(logN)

21 Tree node structure template struct TreeNode { ItemType info; TreeNode* left; TreeNode* right; };

22 Binary Search Tree Specification #include template struct TreeNode; enum OrderType {PRE_ORDER, IN_ORDER, POST_ORDER}; template class TreeType { public: TreeType(); ~TreeType(); TreeType(const TreeType &); void operator=(const TreeType &); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; int NumberOfNodes() const; (continues)

23 void RetrieveItem(ItemType&, bool& found); void InsertItem(ItemType); void DeleteItem(ItemType); void ResetTree(OrderType); void GetNextItem(ItemType&, OrderType, bool&); void PrintTree(ofstream&) const; private: TreeNode * root; }; }; Binary Search Tree Specification (cont.)

24 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

25 template int TreeType ::NumberOfNodes() const { return CountNodes(root); } template int CountNodes(TreeNode * tree) { if (tree == NULL) return 0; else return CountNodes(tree->left) + CountNodes(tree->right) + 1; } Function NumberOfNodes (cont.)

26 Let’s consider the first few steps:

27 Function RetrieveItem

28 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

29 template void TreeType :: RetrieveItem(ItemType& item,bool& found) { Retrieve(root, item, found); } template void Retrieve(TreeNode * tree,ItemType& item,bool& found) { if (tree == NULL) // base case 2 found = false; else if(item info) Retrieve(tree->left, item, found); else if(item > tree->info) Retrieve(tree->right, item, found); else { // base case 1 item = tree->info; found = true; } Function RetrieveItem (cont.)

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

31 Function InsertItem (cont.) Implementing insertion using recursion Insert 11

32 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.)

33 template void TreeType ::InsertItem(ItemType item) { Insert(root, item); } template void Insert(TreeNode *& tree, ItemType item) { if(tree == NULL) { // base case tree = new TreeNode ; tree->right = NULL; tree->left = NULL; tree->info = item; } else if(item info) Insert(tree->left, item); else Insert(tree->right, item); } Function InsertItem (cont.)

34 Insert 11

35 Yes, certain orders produce very unbalanced trees!! Unbalanced trees are not desirable because search time increases!! There are advanced tree structures (e.g.,"red-black trees") which guarantee balanced trees Does the order of inserting elements into a tree matter?

36 (cont.)

37 Function DeleteItem First, find the item; then, delete it Important: 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

38 (1) Deleting a leaf

39 (2) Deleting a node with only one child

40 (3) Deleting a node with two children

41 Find predecessor (it is the 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.)

42 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.)

43 template void TreeType ::DeleteItem(ItemType item) { Delete(root, item); } template void Delete(TreeNode *& tree, ItemType item) { if(item info) Delete(tree->left, item); else if(item > tree->info) Delete(tree->right, item); else DeleteNode(tree); } Function DeleteItem (cont.)

44 template void DeleteNode(TreeNode *& tree) { ItemType data; TreeNode * tempPtr; tempPtr = tree; if(tree->left == NULL) { //right child tree = tree->right; delete tempPtr; } else if(tree->right == NULL) { // left child tree = tree->left; delete tempPtr; } else { GetPredecessor(tree->left, data); tree->info = data; Delete(tree->left, data); } Function DeleteItem (cont.) 2 children 0 or 1 child

45 template void GetPredecessor(TreeNode * tree, ItemType& data) { while(tree->right != NULL) tree = tree->right; data = tree->info; } Function DeleteItem (cont.)

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

47 46 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

48 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" means that the algorithm does something with the values in the node, e.g., print the value)

49 48 ‘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

50 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)

51 50 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

52 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))

53 Tree Traversals

54 Function PrintTree We use "inorder" to print out the node values Why?? (keys are printed out in ascending order!!) Hint: use binary search trees for sorting !! A D J M Q R T

55 Function PrintTree (cont.) void TreeType::PrintTree(ofstream& outFile) { Print(root, outFile); } template void Print(TreeNode * tree, ofstream& outFile) { if(tree != NULL) { Print(tree->left, outFile); outFile info; Print(tree->right, outFile); } (see textbook for overloading >)

56 Class Constructor template TreeType ::TreeType() { root = NULL; }

57 Class Destructor How should we delete the nodes of a tree?

58 Class Destructor (cont’d) Delete the tree in a "bottom-up" fashion Postorder traversal is appropriate for this !! TreeType::~TreeType() { Destroy(root); } void Destroy(TreeNode *& tree) { if(tree != NULL) { Destroy(tree->left); Destroy(tree->right); delete tree; }

59 Copy Constructor How should we create a copy of a tree?

60 Copy Constructor (cont’d) template TreeType ::TreeType(const TreeType & originalTree) { CopyTree(root, originalTree.root); } template<class ItemType) void CopyTree(TreeNode *& copy, TreeNode * originalTree) { if(originalTree == NULL) copy = NULL; else { copy = new TreeNode ; copy->info = originalTree->info; CopyTree(copy->left, originalTree->left); CopyTree(copy->right, originalTree->right); } preorder

61 ResetTree and GetNextItem The user is allowed to specify the tree traversal order For efficiency, ResetTree stores in a queue the results of the specified tree traversal Then, GetNextItem, dequeues the node values from the queue

62 ResetTree and GetNextItem (cont.) (specification file) enum OrderType {PRE_ORDER, IN_ORDER, POST_ORDER}; template class TreeType { public: // same as before private: TreeNode * root; QueType preQue; QueType inQue; QueType postQue; }; new private data

63 template void PreOrder(TreeNode *, QueType &); template void InOrder(TreeNode *, QueType &); template void PostOrder(TreeNode *, QueType &); ResetTree and GetNextItem (cont.)

64 template void PreOrder(TreeNode tree, QueType & preQue) { if(tree != NULL) { preQue.Enqueue(tree->info); PreOrder(tree->left, preQue); PreOrder(tree->right, preQue); } ResetTree and GetNextItem (cont.)

65 template void InOrder(TreeNode tree, QueType & inQue) { if(tree != NULL) { InOrder(tree->left, inQue); inQue.Enqueue(tree->info); InOrder(tree->right, inQue); }

66 template void PostOrder(TreeNode tree, QueType & postQue) { if(tree != NULL) { PostOrder(tree->left, postQue); PostOrder(tree->right, postQue); postQue.Enqueue(tree->info); } ResetTree and GetNextItem (cont.)

67 The function ResetTree template void TreeType ::ResetTree(OrderType order) { switch(order) { case PRE_ORDER: PreOrder(root, preQue); break; case IN_ORDER: InOrder(root, inQue); break; case POST_ORDER: PostOrder(root, postQue); break; }

68 The function GetNextItem template void TreeType ::GetNextItem(ItemType& item, OrderType order, bool& finished) { finished = false; switch(order) { case PRE_ORDER: preQue.Dequeue(item); if(preQue.IsEmpty()) finished = true; break; case IN_ORDER: inQue.Dequeue(item); if(inQue.IsEmpty()) finished = true; break; case POST_ORDER: postQue.Dequeue(item); if(postQue.IsEmpty()) finished = true; break; }

69 Iterative Insertion and Deletion See textbook

70 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(N) InsertItemO(logN)O(N) DeleteItemO(logN)O(N)

71 Exercises 1-3, 8-18, 21, 22, 29-32


Download ppt "Binary Search Trees CS 308 – Data Structures What is a binary tree? Property1: each node can have up to two successor nodes (children) –The predecessor."

Similar presentations


Ads by Google