Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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. FOX CAT ELF HEN DOG HAT HOG Root node Left subtree Right subtree

2 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 FOX CAT ELF HEN DOG HAT HOG Root node Left subtree Right subtree siblings Leaf node

3 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.

4 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 in right subtree. 35 15 30 40 25 38 45 Root node Left subtree Right subtree siblings

5 Searching a Binary Search Tree Algorithm: 1if 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.

6 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.

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

8 Building a Binary Search Tree Builds a tree from the list of keys: 40, 20, 10, 50, 65, 45, 30. 40 65 45 30 10 20 50 root

9 Structure Types for a Binary Tree typedef struct tree_node_s { int key; struct tree_node_s *leftp, *rightp; } tree_node_t;

10 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 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); }

11 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.

12 Display the Left Subtree of 40 Trace of Tree Display Algorithm: 40 65 45 30 10 20 50 root Display left subtree of node 40. Display left subtree of node 20. Display left subtree of node 10. Tree is empty-return from displaying left subtree of node 10. Display item with key 10. Display right subtree of node 10. Tree is empty - return from displaying right subtree of node 10. Return from displaying left subtree of node 20. Display item with key 20. Display right subtree of node 20. Display left subtree of node 30. Tree is empty- return from displaying left subtree of node 30. Display item with key 30. ……………………….. Display right subtree of node 30. Tree is empty-return from displaying right subtree of node 30. Return from displaying right subtree of node 20. Return from displaying left subtree of node 40. Display item with key 40.

13 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.

14


Download ppt "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."

Similar presentations


Ads by Google