Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO BINARY TREES P. 651 - 659. SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,

Similar presentations


Presentation on theme: "INTRODUCTION TO BINARY TREES P. 651 - 659. SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,"— Presentation transcript:

1 INTRODUCTION TO BINARY TREES P. 651 - 659

2 SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element, returning that index. –Otherwise, -1 is returned worst case: O(n) –occurs if item not in list; then, all items must be compared – if list is ordered, runs a little faster on average, since whole list may not have to be searched; worst still O(n), if item is greater than all list items –can modify linear search to represent a linked list:

3 Linear Search with Linked List  int LinkedList :: Search(const Item &myItem) { int pos_num = 0; Node *position; position = head; while (position != 0) { if (myItem == position->data) return pos_num; else { position = position->link; pos_num++; } return -1; }

4 Review of Binary Search - start with midde item, compare that; if match, return middle - otherwise, if item < middle, repeat BS on first half - otherwise, repeat BS on second half - eventually list split down to 1 element, and if that doesn't match, return -1 - runs in O(log n) time, due to number of splits made based on size of list -if size 4, 2 splits; 8 gives 3, 16 gives 4, etc; this is a log function) - binary search does not work with linked lists, because finding the middle element is too cumbersome - can use a different structure to store the contents of the ordered list to make it easy to apply algorithms like binary search

5 Binary Search Review (cont.) - how? -make the initial middle point the root -then make all subsequent middles elements branching off the root -example: List: 1 4 28 39 56 70 100 -middle: 39 -middle of first half: 4 -middle of second half: 70 -can express as a diagram -this structure is called a binary tree or binary search tree 39 704 1285610 0

6 BINARY TREE DEFINITIONS Binary Tree: non-linear linked list where each node may point to up to two other nodes Empty Tree: a tree with no nodes arcs: connectors between nodes subtree: an entire branch of a tree, from one particular node down root node (or root): the first node of the tree; only one in each tree children: nodes that are branched off from a particular node

7 B-TREES (Definitions cont.) parent: - node that has children (nodes branching off it); all nodes can have only 1 parent left child: represents all nodes to left of that parent right child: all the values greater than that parent leaf node: a node with no children siblings: all nodes on the same level with same parent

8 B-TREE EXAMPLES Example of a valid B- Tree: Why Valid? one root at most 2 children from any node values of left children less than parent values of right children greater than parent Data: 15 Data: 1 Data: 22 NULL

9 B-TREE EXAMPLES (cont.) EXAMPLE OF AN INVALID B-TREE : WHY INVALID? -child 12 has a left child greater than it -child 72 has a right child less than it Data: 25 Data: 12Data: 72 NULL Data: 17Data: 22Data: 49 NULL

10 IMPLEMENTATION OF BINARY SEARCH TREE will use structure similar to linked list, but with some modifications to account for two links typedef int Item; struct TreeNode { Item Value; //data TreeNode *left; //pointer to left child TreeNode *right; //pointer to right child };

11 BINARY TREE IMPLEMENTATION (CONT.) then we can define a new class called a Binary Tree: class BinaryTree { public: BinaryTree(); ~BinaryTree(); void Display(); void Insert(const Item &anItem); bool Search(const Item &anItem); bool Delete(const Item &anItem); private: TreeNode *root; TreeNode *InsertTree(TreeNode *root, TreeNode *node); void DisplayTree(TreeNode root); bool SearchTree(TreeNode root, const Item &anItem); void DeleteCompleteTree(); bool DeleteTreeValue(TreeNode &root, const Item &anItem); };

12 B-TREE MEMBER FUNCTIONS - Constructor -sets root to null, indicating the tree is empty BinaryTree::BinaryTree() { root = 0; } - Insertion - use the divide-and-conquer approach to solving this: -using recursion, decide to work on either right or left of current node -reduces the problem in half - how does insertion work? -find the location where the new item goes (start at root and recursively move through nodes) -user should ONLY know about how to use the function, not the internals of the tree structure -that is why we need two functions: the public insert (called by the user) and the private InsertTree (called internally, that handles the recursive calls)

13 B-TREE: Insert Function //the public function void BinaryTree::Insert(const Item &anItem) { node = new TreeNode; node -> left = null; node -> right = null; node->value = anItem; root = InsertTree(root, node); } TreeNode BinaryTree::InsertTree(TreeNode *root,TreeNode *node) { if (root == 0) { //tree is empty, or we have reached a leaf node root = node; return root; } //handle case if this item is a duplicate if (node -> value == root->Value) ` return root; //search the left side if (node->value Value) root -> left = InsertTree(root->left, node); else //search the right side root->right = InsertTree(root->right, node); }

14 B-TREES – Display Function Display Function same as insert; two functions, one public (called display) and one private (called DisplayTree) //public function void BinaryTree::Display() { DisplayTree(root); cout << endl; } //private function void BinaryTree::DisplayTree(TreeNode root) { if (root != 0) { DisplayTree(root->left); cout Value << " "; DisplayTree(root->right); }

15 B-TREES – Search Function - Search is a lot like Display; has both a public and private function - traverses the tree checking to see if the value matches what we are looking for //the public function bool BinaryTree::Search(const Item &anItem) { return (SearchTree(root, anItem)); } //the private function bool BinaryTree::SearchTree(TreeNode root, const Item &anItem) { if (root == 0) return false; if (root->Value == anItem) return true; if (anItem Value) return(SearchTree(root->left, anItem)); else return(SearchTree(root->right, anItem)); }

16 SAMPLE PROGRAM void main() { BinaryTree TheTree; TheTree.Insert(5); TheTree.Insert(20); TheTree.Insert(1); TheTree.Insert(17); TheTree.Display(); if (TheTree.Search(100)) cout << "Found 100" << endl; if (TheTree.Search(1)) cout << "Found 1" << endl; cout << endl; }

17 B-TREES (other functions) The Deconstructor - traverses the left and right nodes, and upon returning, deletes them from memory //the public function void BinaryTree::~BinaryTree() { DeleteCompleteTree(root); } //the private function void BinaryTree::DeleteCompleteTree(TreeNode root) { if (root != 0) { DeleteCompleteTree(root->left); DeleteCompleteTree(root->right); delete root; }

18 QUESTIONS? Food for Thought: “How would you write a function that just deletes a single node from the tree?” NEXT TOPIC: AVL Trees (Ch. 15 – P. 839 - 854)


Download ppt "INTRODUCTION TO BINARY TREES P. 651 - 659. SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,"

Similar presentations


Ads by Google