+ All Categories
Home > Documents > Binary Tree. Contiguous versus Linked List Insertion in Contiguous list needs a lot of move. For big...

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

Date post: 04-Jan-2016
Category:
Upload: hilary-stewart
View: 218 times
Download: 1 times
Share this document with a friend
30
Binary Tree
Transcript
Page 1: 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.

Binary Tree

Page 2: 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.

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.

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

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.

Page 4: 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.

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.

Page 5: 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.

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.

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

Comparison tree for binary search

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

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.

Page 8: 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.

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.

Page 9: 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.

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.

Page 10: 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.

Empty binary tree

• There is one empty binary tree,

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

One node

• There is one binary tree with one node,

Page 12: 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.

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

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

Not a Binary Tree

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

Page 14: 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.

Three nodes

• There are five binary trees with three nodes:

Page 15: 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.

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.

Page 16: 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.

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.

Page 17: 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.

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.

Page 18: 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.

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.

Page 19: 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.

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

Page 20: 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.

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.

Page 21: 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.

Simple Examples

Page 22: 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.

Slightly Complicated Examples

Page 23: 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.

Slightly Complicated Examples

• Preorder 1, 2, 3, 4, 5

• Inorder 1, 4, 3, 5, 2

• Postorder 4, 5, 3, 2, 1

Page 24: 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.

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.

Page 25: 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.

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.

Page 26: 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.

Expression Trees

Page 27: 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.

Expression Trees

Page 28: 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.

Expression Trees

Page 29: 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.

Comparison tree:

Page 30: 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.

Linked implementation of binary tree:


Recommended