Download presentation
Presentation is loading. Please wait.
Published byHilary Stewart Modified over 9 years ago
1
Binary Tree
2
Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big chunks of records it is time consuming. Linked List is the solution. Linked List does not require moving data, needs to change the pointers only.
3
Linked Lists Though Linked Lists have great advantages of flexibility over the contiguous representation of data structures, but they have one weak feature: They are sequential lists; that is, they are arranged so that it is necessary to move or search through them only one position at a time.
4
Binary Trees We overcome these disadvantages by implementing trees as data structures, using the methods of pointers and linked lists for their implementation. Data structures organized as trees will prove valuable for a range of applications, especially for problems of information retrieval.
5
Binary Tree For some time, we have been drawing trees to illustrate the behavior of algorithms. We have drawn comparison trees showing the comparisons of keys in searching and sorting algorithms; we have drawn trees of subprogram calls; and we have drawn recursion trees. If, for example, we consider applying binary search to the following list of names, then the order in which comparisons will be made is shown in the comparison tree of next slide.
6
Comparison tree for binary search
7
Binary Tree In binary search, when we make a comparison with a key, we then move either left or right depending on the outcome of the comparison. It is thus important to keep the relation of left and right in the structure we build. It is also possible that the part of the tree on one side or both below a given node is empty. In the example of previous slide, the name Amy has an empty left subtree. For all the leaves, both subtrees are empty. We can now give the formal definition of a new data structure.
8
Binary Tree Definition: A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root.
9
ADT Binary Tree To specify binary trees as an abstract data type, we must state what operations can be performed on binary trees. Rather than doing so at once, we shall develop the operations as the chapter progresses. Note also that this definition makes no reference to the way in which binary trees will be implemented in memory. As we shall presently see, a linked representation is natural and easy to use, but other implementations are possible as well.
10
Empty binary tree There is one empty binary tree,
11
One node There is one binary tree with one node,
12
Two nodes There are two binary trees with two nodes: These are different from each other. We never draw any part of a binary tree to look like
13
Not a Binary Tree We never draw any part of a binary tree to look like
14
Three nodes There are five binary trees with three nodes:
15
Binary Trees with four nodes You should pause to construct all fourteen binary trees with four nodes. This exercise will further help you establish the ideas behind the definition of binary trees.
16
Traversal of Binary Trees One of the most important operations on a binary tree is traversal, moving through all the nodes of the binary tree, visiting each one in turn. As for traversal of other data structures, the action we shall take when we visit each node will depend on the application.
17
Traversal of Binary Trees For lists, the nodes came in a natural order from first to last, and traversal followed the same order. For trees, however, there are many different orders in which we could traverse all the nodes. When we write an algorithm to traverse a binary tree, we shall almost always wish to proceed so that the same rules are applied at each node, and we thereby adhere to a general pattern.
18
Traversal of Binary Trees At a given node there are three tasks to do in some order: Visit the node itself ( V); traverse its left subtree ( L); traverse its right subtree ( R). There are six ways to arrange these tasks: V L R L V R L R V V R L R V L R L V.
19
Standard Traversal Orders By standard convention, these are reduced to three by considering only the ways in which the left subtree is traversed before the right. V L R L V R L R V preorder inorder postorder
20
Traversal of Binary Trees preorder inorder postorder These three names are chosen according to the step at which the given node is visited. With preorder traversal we first visit the node, then traverse its left subtree, and then traverse its right subtree. With inorder traversal we first traverse the left subtree, then visit the node, and then traverse its right subtree. With postorder traversal we first traverse the left subtree, then traverse the right subtree, and nally visit the node.
21
Simple Examples
22
Slightly Complicated Examples
23
Preorder1, 2, 3, 4, 5 Inorder1, 4, 3, 5, 2 Postorder4, 5, 3, 2, 1
24
Expression Tree An expression tree is built up from the simple operands and operators of an (arithmetical or logical) expression by placing the simple operands as the leaves of a binary tree and the operators as the interior nodes.
25
Expression Trees For each binary operator, the left subtree contains all the simple operands and operators in the left operand of the given operator, and the right subtree contains everything in the right operand. For a unary operator, one of the two subtrees will be empty.
26
Expression Trees
29
Comparison tree:
30
Linked implementation of binary tree:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.