1 / 80

C++ Programming: Program Design Including Data Structures, Third Edition

C++ Programming: Program Design Including Data Structures, Third Edition. Chapter 20: Binary Trees. Objectives. In this chapter you will: Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree

odessa
Télécharger la présentation

C++ Programming: Program Design Including Data Structures, Third Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees

  2. Objectives In this chapter you will: • Learn about binary trees • Explore various binary tree traversal algorithms • Learn how to organize data in a binary search tree • Learn how to insert and delete items in a binary search tree • Explore non-recursive binary tree traversal algorithms

  3. Binary Trees

  4. Suppose that T is a binary tree with the root node A. • Let LA denote the left subtree of A and RA denote the right subtree of A. • Suppose that B is the root node of LA and C is the root node of RA. • B is called the left child of A. • C is called the right child of A. • A is called the parent of B and C. • In the diagram of a binary tree, each node of the binary tree is represented as a circle and the circle is labeled by the node. • The root node of the binary tree is drawn at the top. • The left child of the root node (if any) is drawn below and to the left of the root node. • The right child of the root node (if any) is drawn below and to the right of the root node. • Children are connected to the parent by an arrow from the parent to the child. • An arrow is usually called a directed edge or a directed branch (or simply a branch). (See Figure 20-1.)

  5. In Figure 20-1, • The root node of this binary tree is A. • The left subtree of the root node is the set LA = {B, D, E, G}. • The right subtree of the root node is the set RA = {C, F, H}. • The root node of the left subtree of A—that is, the root node of LA—is node B. • The root node of RA is C, and so on. • LA and RA are binary trees. • Three lines at the end of an arrow mean that the subtree is empty. • The left subtree of D is empty. • For node F, the left child is H and node F has no right child.

  6. In the binary tree of Figure 20-5: • The root node of the binary tree = A. • LA = {B} RA = {C} • The root node of LA = B. • LB = empty RB = empty • The root node of RA = C. • LC = empty RC = empty

  7. Every node in a binary tree has at most two children. • Every node, other than storing its own information, must keep track of its left subtree and right subtree. • Every node has two pointers, say lLink and rLink. • The pointer lLink points to the root node of the left subtree of the node. • The pointer rLink points to the root node of the right subtree of the node.

  8. From the definition of the node it is clear that for each node, • The data is stored in info. • A pointer to the left child is stored in lLink. • A pointer to the right child is stored in rLink. • A pointer to the root node of the binary tree is stored outside the binary tree in a pointer variable, usually called the root, of type nodeType. • In general, a binary tree looks like the diagram in Figure 20-7.

  9. For simplicity, we will continue to draw binary trees as before. • Circles represent nodes and left and right arrows represent links. • Three lines at the end of an arrow mean that the subtree is empty.

  10. A node in a binary tree is called a leaf if it has no left and right children. Let U and V be two nodes in the binary tree T. U is called the parent of V if there is a branch from U to V. A path from a node X to a node Y in a binary tree is a sequence of nodes X0, X1, ..., Xn such that • X = X0, Xn = Y • Xi-1 is the parent of Xi for all i = 1, 2, ..., n. That is, there is a branch from X0 to X1, X1 to X2, ..., Xi-1 to Xi, ..., Xi-1 to Xn. • If X0, X1, ..., Xn is a path from node X to node Y, sometimes we denote it by X = X0X1 ... Xn-1 Xn = Y or simply XX1 ... Xn-1 Y. • Because the branches go only from a parent to its children, from the previous discussion it is clear that in a binary tree, there is a unique path from the root to every node in the binary tree.

  11. The length of a path in a binary tree is the number of branches on that path. • The level of a node in a binary tree is the number of branches on the path from the root to the node. • The level of the root node of a binary tree is 0, and the level of the children of the root node is 1. • The height of a binary tree is the number of nodes on the longest path from the root to a leaf.

  12. In the binary tree of Figure 20.8, • The nodes I, E, and H have no left and right children. So the nodes I, E, and H are leaves. • Node A is the parent of node B. • Node A is the parent of node C. • Node B is the parent of nodes D and E. • Node C is the parent of node F. • Node D is the parent of node G, and so on. • ABDG is a path from node A to node G. Because there are three branches on this path, the length of this path is 3. • BDGI is a path from node B to node I. • There are three leaves in this binary tree, which are I, E, and H. • The paths from root to the leaves are: ABDG-I, ABE, and ACFH. • The longest path from root to a leaf is ABDG-I. The number of nodes on this path is 5. • The height of the binary tree is 5.

  13. Height of a binary tree • If the binary tree is empty, then the height is 0. • Suppose that the binary tree is nonempty. • To find the height of the binary tree, first find the height of the left subtree and the height of the right subtree. • Take the maximum of these two heights and add 1 to find the height of the binary tree. • To find the height of the left (right) subtree, we apply the same procedure • Therefore, the general algorithm to find the height of a binary tree is as follows. Suppose height(p) denotes the height of the binary tree with root p.

  14. The listing of the nodes produced by the inorder traversal of a binary tree is called the inorder sequence. • The listing of the nodes produced by the preorder traversal is called the preorder sequence. • The listing of the nodes produced by the postorder traversal is called the postorder sequence.

  15. Inorder sequence: B D A C • Preorder sequence: A B D C • Postorder sequence: D B C A

  16. Operations that are typically performed on a binary tree. • Determine whether the binary tree is empty. • Search the binary tree for a particular item. • Insert an item in the binary tree. • Delete an item from the binary tree. • Find the height of the binary tree. • Find the number of nodes in the binary tree. • Find the number of leaves in the binary tree. • Traverse the binary tree. • Copy the binary tree.

  17. The item search, insertion, and deletion operations all require the binary tree to be traversed. • However, because the nodes of a binary tree are in no particular order, these algorithms are not very efficient on arbitrary binary trees. • No criteria exist to guide the search on these binary trees, as we will see in the next section. • We will discuss these algorithms when we discuss special binary trees.

  18. Suppose that we want to determine whether 53 is in the binary tree of Figure 20.10. • We can use any of the previous traversal algorithms to visit each node and compare the search item with the data stored in the node. • This could require us to traverse a large part of the binary tree, so the search will be slow. • The reason that we need to visit each node in the binary tree until either the item is found or we have traversed the entire binary tree is that no criteria exist to guide our search. • This case is like an arbitrary linked list where we must start our search at the first node and continue looking at each node until either the item is found or the entire list is searched.

  19. The binary tree in Figure 20-11 has some order to its nodes. • Suppose that we want to determine whether 58 is in this binary tree. • We start our search at the root node. • Compare 58 with the data in the root node. • Because 58 ≠ 60 and 58 < 60, it is guaranteed that 58 will not be in the right subtree of the root node. • If 58 is in the binary tree, then it must be in the left subtree of the root node. • We follow the left pointer of the root node and go to the node with info 50. • We now apply the same criteria at this node. • Because 58 > 50, we must follow the right pointer of this node and go to the node with info 58. At this node we find 58. • Every time we move down to a child, we eliminate one of the subtrees of the node from our search. • If the binary tree is nicely constructed, then the search is very similar to the binary search on arrays.

  20. After inserting an item in a binary search tree, the resulting binary tree must be a binary search tree. • To insert a new item, first we search the binary search tree and find the place where the new item is to be inserted. • The search algorithm is similar to the search algorithm of the function search. • Here we traverse the binary search tree with two pointers—a pointer, say current, to check the current node and a pointer, say trailCurrent, pointing to the parent of current. • Because duplicate items are not allowed, our search must end at an empty subtree. • We can then use the pointer trailCurrent to insert the new item at the proper place. • The item to be inserted, insertItem, is passed as a parameter to the function insert.

  21. The delete operation has four cases, as follows: • The node to be deleted has no left and right subtrees; that is, the node to be deleted is a leaf. • The node to be deleted has no left subtree; that is, the left subtree is empty, but it has a nonempty right subtree. • The node to be deleted has no right subtree; that is, the right subtree is empty, but it has a nonempty left subtree. • The node to be deleted has nonempty left and right subtrees.

More Related