Binary Tree. Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big...

Post on 04-Jan-2016

218 views 1 download

transcript

Binary Tree

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.

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.

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.

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.

Comparison tree for binary search

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.

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.

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.

Empty binary tree

• There is one empty binary tree,

One node

• There is one binary tree with one node,

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

Not a Binary Tree

• We never draw any part of a binary tree to look like

Three nodes

• There are five binary trees with three nodes:

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.

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.

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.

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.

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 Vpreorder inorder postorder

Traversal of Binary Treespreorder 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.

Simple Examples

Slightly Complicated Examples

Slightly Complicated Examples

• Preorder 1, 2, 3, 4, 5

• Inorder 1, 4, 3, 5, 2

• Postorder 4, 5, 3, 2, 1

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.

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.

Expression Trees

Expression Trees

Expression Trees

Comparison tree:

Linked implementation of binary tree: