+ All Categories
Home > Documents > Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially...

Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially...

Date post: 23-Dec-2015
Category:
Upload: mercy-carr
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
45
Binary and Other Trees CSE, POSTECH
Transcript
Page 1: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

Binary and Other Trees

CSE, POSTECH

Page 2: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

2 2

Linear Lists and Trees

Linear lists are useful for serially ordered data– (e1,e2,e3,…,en)

– Days of week– Months in a year– Students in a class

Trees are useful for hierarchically ordered data– Joe’s descendants– Corporate structure– Government Subdivisions– Software structure

Read Examples 11.1-11.4

Page 3: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

3 3

Joe’s Descendants

What are other examples of hierarchically ordered data?

Page 4: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

4 4

Definition of Tree

A tree t is a finite nonempty set of elements One of these elements is called the root The remaining elements, if any, are partitioned

into trees, which are called the subtrees of t.

Page 5: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

5 5

Subtrees

Page 6: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

6 6

Tree Terminology

The element at the top of the hierarchy is the root.

Elements next in the hierarchy are the children of the root.

Elements next in the hierarchy are the grandchildren of the root, and so on.

Elements at the lowest level of the hierarchy are the leaves.

Page 7: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

7 7

Other Definitions

Leaves, Parent, Grandparent, Siblings,Ancestors, Descendents

Leaves = {Mike,AI,Sue,Chris}

Parent(Mary) = Joe

Grandparent(Sue) = Mary

Siblings(Mary) = {Ann,John}

Ancestors(Mike) = {Ann,Joe}

Descendents(Mary)={Mark,Sue}

Page 8: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

8 8

Levels and Height

Root is at level 1 and its children are at level 2. Height = depth = number of levels

level 1

level 2

level 3

level 4

Page 9: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

9 9

Node Degree

Node degree is the number of children it has

Page 10: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

10 10

Tree Degree

Tree degree is the maximum of node degrees

tree degree = 3

Do Exercises 3

Page 11: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

11 11

Binary Tree

A finite (possibly empty) collection of elements A nonempty binary tree has a root element and

the remaining elements (if any) are partitioned into two binary trees

They are called the left and right subtrees of the binary tree

Page 12: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

12 12

Difference Between a Tree & a Binary Tree

A binary tree may be empty; a tree cannot be empty. No node in a binary tree may have a degree more than 2,

whereas there is no limit on the degree of a node in a tree. The subtrees of a binary tree are ordered; those of a tree

are not ordered.

a

b c

a

c b

- different when viewed as a binary tree

- same when viewed as a tree

Page 13: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

13 13

Binary Tree for Expressions

Figure 11.5 Expression Trees

Page 14: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

14 14

Binary Tree Properties

1. The drawing of every binary tree with n elements, n > 0, has exactly n-1 edges. (see its proof on pg. 426)

2. A binary tree of height h, h >= 0, has at least h and at most 2h-1 elements in it. (see its proof on pg. 427)

Page 15: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

15 15

Binary Tree Properties

3. The height of a binary tree that contains n elements, n >= 0, is at least (log2(n+1)) and at most n. (see its proof on pg. 427)

minimum number of elements maximum number of elements

Page 16: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

16 16

Full Binary Tree

A full binary tree of height h has exactly 2h-1 nodes. Numbering the nodes in a full binary tree

– Number the nodes 1 through 2h-1– Number by levels from top to bottom– Within a level, number from left to right (see Fig. 11.6)

Page 17: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

17 17

Node Number Property of Full Binary Tree

Parent of node i is node (i/2), unless i = 1 Node 1 is the root and has no parent

Page 18: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

18 18

Left child of node i is node 2i, unless 2i > n,where n is the total number of nodes.

If 2i > n, node i has no left child.

Node Number Property of Full Binary Tree

Page 19: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

19 19

Right child of node i is node 2i+1, unless 2i+1 > n,where n is the total number of nodes.

If 2i+1 > n, node i has no right child.

Node Number Property of Full Binary Tree

Page 20: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

20 20

Complete Binary Tree with N Nodes

Start with a full binary tree that has at least n nodes Number the nodes as described earlier. The binary tree defined by the nodes numbered 1

through n is the n-node complete binary tree. A full binary tree is a special case of a complete

binary tree See Figure 11.7 for complete binary tree examples See Figure 11.8 for incomplete binary tree

examples

Page 21: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

21 21

Example of Complete Binary Tree

Complete binary tree with 10 nodes. Same node number properties (as in full binary

tree) also hold here.

Page 22: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

22 22

Binary Tree Representation

Array representation Linked representation

Page 23: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

23 23

Array Representation of Binary Tree

The binary tree is represented in an array by storing each element at the array position corresponding to the number assigned to it.

Page 24: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

24 24

Incomplete Binary Trees

Complete binary tree with some missing elements

Fig. 11.8 Incomplete binary trees

Page 25: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

25 25

Right-Skewed Binary Tree

An n node binary tree needs an array whose length is between n+1 and 2n.

Right-skewed binary tree wastes the most space What about left-skewed binary tree?

Page 26: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

26 26

Linked Representation of Binary Tree

The most popular way to present a binary tree Each element is represented by a node that has

two link fields (leftChild and rightChild) plus an element field (see Figure 11.10)

Each binary tree node is represented as an object whose data type is binaryTreeNode (see Program 11.1)

The space required by an n node binary tree isn * sizeof(binaryTreeNode)

Page 27: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

27 27

Linked Representation of Binary Tree

Page 28: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

28 28

Node Class For Linked Binary Tree

template<class T>class binaryTreeNode {private:

T element;binaryTreeNode<T> *leftChild, *rightChild;

public: // 3 constructorsbinaryTreeNode() { leftChild = rightChild = NULL; } // no paramsbinaryTreeNode(const T& theElement) { // element param only

element = theElement; leftChild = rightChild = NULL;}binaryTreeNode(const T& theElement, binaryTreeNode *l,

binaryTreeNode *r) { // element + links params element = theElement; leftChild = l; rightChild = r;

}}

Page 29: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

29 29

Common Binary Tree Operations

Determine the height Determine the number of nodes Make a copy Determine if two binary trees are identical Display the binary tree Delete a tree If it is an expression tree, evaluate the expression If it is an expression tree, obtain the

parenthesized form of the expression

Page 30: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

30 30

Binary Tree Traversal

Many binary tree operations are done by performing a traversal of the binary tree

In a traversal, each element of the binary tree is visited exactly once

During the visit of an element, all actions (make a copy, display, evaluate the operator, etc.) with respect to this element are taken

Page 31: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

31 31

Binary Tree Traversal Methods Preorder

The root of the subtree is processed first before going into the left then right subtree (root, left, right).

Inorder After the complete processing of the left subtree the root

is processed followed by the processing of the complete right subtree (left, root, right).

Postorder The root is processed only after the complete processing

of the left and right subtree (left, right, root). Level order

The tree is processed by levels. So first all nodes on level i are processed from left to right before the first node of level i+1 is visited

Page 32: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

32 32

Preorder Traversal

template<class T> // Program 11.2void preOrder(binaryTreeNode<T> *t){

if (t != NULL) {visit(t); // visit tree rootpreOrder(t->leftChild); // do left subtreepreOrder(t->rightChild); // do right

subtree}

}

Page 33: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

33 33

Preorder Example (visit = print)

a b d g h e i c f j

Page 34: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

34 34

Preorder of Expression Tree

/ * + a b - c d + e f

Gives prefix form of expression.

Page 35: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

35 35

Inorder Traversal

template<class T> // Program 11.3

void inOrder(binaryTreeNode<T> *t)

{

if (t != NULL) {

inOrder(t->leftChild); // do left subtree

visit(t); // visit tree root

inOrder(t->rightChild); // do right subtree

}

}

Page 36: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

36 36

Inorder Example (visit = print)

g d h b e i a f j c

Page 37: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

37 37

Inorder by Projection (Squishing)

Page 38: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

38 38

Inorder of Expression Tree

Gives infix form of expression, which is how we normally write math expressions. What about parentheses?

See Program 11.6 for parenthesized infix form Fully parenthesized output of the above tree?

Page 39: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

39 39

Postorder Traversal

template<class T> // Program 11.4

void postOrder(binaryTreeNode<T> *t)

{

if (t != NULL) {

postOrder(t->leftChild); // do left subtree

postOrder(t->rightChild); // do right subtree

visit(t); // visit tree root

}

}

Page 40: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

40 40

Postorder Example (visit = print)

g h d i e b j f c a

Page 41: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

41 41

Postorder of Expression Tree

a b + c d - * e f + /

Gives postfix form of expression.

Page 42: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

42 42

Level Order Traversal

template <class T> // Program 11.7void levelOrder(binaryTreeNode<T> *t){// Level-order traversal of *t. linkedQueue<binaryTreeNode<T>*> Q; while (t != NULL) { visit(t); // visit t if (t->leftChild) q.push(t->leftChild); // put t's children if (t->rightChild) q.push(t->rightChild); // on queue try {t = q.front();} // get next node to visit catch (queueEmpty) {return;}

q.pop() } }

Page 43: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

43 43

Level Order Example (visit = print)

Add and delete nodes from a queueOutput: a b c d e f g h i j

Page 44: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

44 44

Space and Time Complexity

The space complexity of each of the four traversal algorithm is O(n)

The time complexity of each of the four traversal algorithm is (n)

Page 45: Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.

45 45

Binary Tree ADT, Class, Extensions

See ADT 11.1 for Binary Tree ADT definition See Program 11.8 for Binary Tree abstract class See Programs 11.9 - 11.11 for operation

implementations

Read Sections 11.1 - 11.8


Recommended