advertisement

Binary Tree • A data structure whose nodes contain two pointer fields. • One or both pointers can have the value NULL. • Each node in a binary tree can have 0, 1, or 2 successor nodes. Root node FOX Left subtree Right subtree HEN DOG CAT ELF HAT HOG Binary Tree • Root node => the first node in a binary tree • left subtree => the part of a tree pointed to by the left pointer of the root node. • right subtree => the part of a tree pointed to by the right pointer of the root node Root node FOX Left subtree Leaf node Right subtree HEN DOG CAT ELF HAT HOG siblings Binary Search Tree • A particular kind of binary tree. • A tree structure that stores data in such a way that they can be retrieved very efficiently. • Every item stored in a binary search tree has a unique key. • A binary search tree is either empty or has the property that the item in its root has a larger key than each item in its left subtree and a smaller key than each item in its right subtree. • Its left and right subtrees must be binary search trees. • The string stored in every node is alphabetically larger than all strings in its left subtree and alphabetically smaller than all strings in its right subtree. • • • • Binary search Tree Root node =>35 Root of left subtree => 25 Root of right subtree => 40 The number stored in every node is larger than all numbers in its left subtree and smaller than all numbers Root node in right subtree. 35 Left subtree Right subtree 40 25 15 30 38 45 siblings Searching a Binary Search Tree Algorithm: 1 if the tree is empty 2 The target key is not in the tree. else if the target key is in the root item 3 The target key is found in the root item. else if target is smaller than the root’s key 4 Search the left subtree. else 5 Search the right subtree. Building a Binary Search Tree • Before we can retrieve an item from a binary search tree, we have to build the tree. • Building a binary search tree requires that - we scan a collection of data items that is no particular order and insert each one individually - make sure that the expanded tree is a binary search tree. • A binary tree builds from the root node down, so we have to store the first data item in the root node. • To store each subsequent data item, we have to find out the appropriate node to be its parent, attach a new node to the parent, and then store that data item in the new node. Algorithm for Insertion in a Binary Search Tree 1 if the tree is empty 2 Insert the new item in the tree’s root node. else if the root’s key matches the new item’s key 3 Skip insertion - duplicate key. else if the new item’s key is smaller than the root’s key 4 Insert the new item in the root’s left subtree. else 5 Insert the new item in the root’s right subtree. Building a Binary Search Tree • Builds a tree from the list of keys: 40, 20, 10, 50, 65, 45, 30. root 40 50 20 10 30 45 65 Structure Types for a Binary Tree typedef struct tree_node_s { int key; struct tree_node_s *leftp, *rightp; } tree_node_t; Function tree_insert tree_node_t *tree_insert (tree_node_t *rootp, int new_key) { if (rootp == NULL) // simple case 1 - empty tree { rootp = (tree_node_t *) malloc(sizeof(tree_node_t)); rootp->key = new_key; rootp->leftp = NULL; rootp->rightp = NULL; } else if (new_key == root->key) // simple case 2, duplicate key, no insertion else if (new_key < rootp->key) // insert in left subtree rootp->leftp = tree_insert(rootp->leftp, new_key); else // insert in right subtree rootp->rightp = tree_insert (rootp->rightp, new_key); return (rootp); } Displaying a Binary Search Tree • To display the contents of a binary search tree so that its items are listed in order by key value, use the following algorithm: 1 if the tree is not empty 2 Display left subtree. 3 Display root item. 4 Display right subtree. • Because the algorithm displays the items in order by key value, this algorithm is called an inorder traversal. Display the Left Subtree of 40 • Trace of Tree Display Algorithm: Display left subtree of node 40. root Display left subtree of node 20. 40 Display left subtree of node 10. Tree is empty-return from 50 20 displaying left subtree of node 10. Display item with key 10. 65 Display right subtree of node 10. 10 45 30 Tree is empty - return from displaying right subtree of node 10. Display right subtree of node 30. Return from displaying left subtree Tree is empty-return from displaying of node 20. right subtree of node 30. Display item with key 20. Return from displaying right subtree Display right subtree of node 20. of node 20. Display left subtree of node 30. Return from displaying left subtree of Tree is empty- return from displaying node 40. left subtree of node 30. Display item with key 40. Display item with key 30. ……………………….. Programming Error with heap Management • Problems with heap management can cause run-time errors. • If your program gets struck in an infinite loop while creating a dynamic data structure, your program can consume all memory cells on the storage heap. • This situation can result in a heap overflow or stack overflow run-time error. • Make sure your program does not attempt to reference a list node after the node is returned to the heap. You have to be careful to return a storage block to the heap before losing all pointers to it.