• 沒有找到結果。

# TREE 1

N/A
N/A
Protected

Share "TREE 1"

Copied!
43
0
0

(1)

Michael Tsai 2017/04/11

(2)

### Reference

Fundamentals of Data Stru ctures in C, 2nd Edition, 2008

Chapter 5

Horowitz, Sahni, and Ande rson-Freed

(3)

### Reference

Data Structures and Algor ithms Made Easy, Second E dition, 2011, CareerMonk Publications, by Karumanc hi

Chapter 6.11

(4)

### 樹

http://www.ahneninfo.com/de/ahnentafel.htm

(5)

In the CS world, we usually draw the tree upside-down.

Root

Leaves What are the

properties of a tree?

A nonlinear, hierarchical way to represent data.

(6)

### Definition

Definition: A tree is a finite set of one or more no des such that

(1) There is a specially designated node called the root.

(2) The remaining nodes are partitioned into disjoi nt sets , where each of these sets is a tree.

(3) are called the subtrees of the root.

Note that the above is a recursive definition.

A node with no subtree, is it a tree?

Is “No node” (null) a tree?

Is the “graph” on the right a tree?

(7)

### Tree Dictionary

Root

Node/Edge (branch)

Degree (of a node):

The number of subtrees of a node

Leaf/Terminal node:

its degree=0

Parent/Children

Siblings

they have the same parent.

Ancestors/Descendants

A

B C D

E F G H I J

K L M

(8)

### Tree Dictionary

Level/depth (of a node):

The number of branch to reach tha t node from the root node.

(i.e., root is at level 0)

Height (of a tree):

The number of levels in a tree

(Note that some definitions start from level 1)

Size (of a tree):

The number of nodes in a tree

Weight (of a tree):

The number of leaves in a tree

Degree (of a tree):

The maximum degree of any node in a t ree

A

B C D

E F G H I J

K L M

Level 0

Level 1

Level 2 Level 3

(9)

### Representing a tree with array

What to store?

The data associated with each node

Array method

Store the data sequentially according to the level of the node

In the array:

How to find the parent of a node?

How to find the children of a node?

A

B C D

E F G H I J

Max degree of the tree=3

A B C D E F G H I J

0 1 2 3 4 5 6 7 8 9 1

0

1 1

1 2

-



- - -

(10)

### Representing a tree with array

Assume degree (of the tree)=d (The example on the right uses d=3)

How to find the parent of a nod e?

Observation:

For node with index i, its pare nt’s index is

How to find the children of a n ode?

Observation:

For node with index i, its chil dren’s indices range from ???

A

B C D

E F G H I J

-



- - -

(11)

### Representing a tree with array

Downside?

If there are many nodes with degree less than d,

then we waste a lot of space in the array.

A

B C D

E G H J

Max degree of the tree=3 K

I

A B C D E G H I J K

0 1 2 3 4 5 6 7 8 9 1 0 1

1 1

2 … 3 1

Waste of space!

(12)

### Representing a tree with linked structure

Assume degree =3 struct TreeNode{

char data;

struct TreeNode* child1;

struct TreeNode* child2;

struct TreeNode* child3;

};

A

B C D

H I A

B \0 \0 \0 C \0 \0 \0 D \0

H \0 \0 \0 I \0 \0 \0

\0 == NULL

Not actually solving the prob?

Wasting the space to store

(13)

### Representing a tree with linked structure

A

B C

1 2 d

Assume degree of tree = d, size o f the tree = n

How many pointers are null?

Total number of pointers:

Number of branch? n-1.

Data child 1 child 2 child 3 child k

Number of wasted

pointers Better if

smaller!

(14)

### 左小孩 - 右兄弟姊妹 表示法

Left child-right sibling representation

Inspired by observations:

1. Each node has a leftmost child ( 是廢話 )

2. Each node has only a immediately-right sibling ( 也是廢話 )

Data left child Right sibling

(15)

A

B C D

E F G H I J

K L M

A

B C D

E F G H I J

K L M

(16)

### LCRS tree

It’s always a degree-two tree

!

Converting to from an arbitrar y-degree tree to a degree-two tree!

Root does not have a right chi ld (because root in the origin al tree does not have a siblin g)

A

B C D

E F G H I J

K L M

A B

C

D E

F

G

H I M J

K

L

(17)

### Binary Tree

Definition: A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtree and t he right subtree.

According to this definition:

Note: “null” (no node) is a valid tree.

Note: the order of the children (left or right) is meaningful.

(18)

### 一些證明

i=0 時 , 為 root 那一層 , 所以只有一個 node, 也就是最 多有個 node. ( 成立 )

## 在 level i 的 node 數目最多為 (root 在 level 0)

(19)

. 喔耶 .

(20)

### 三些證明 ( 誤 )

(2) 代入 (3) 得 (4)

(4) 減 (1) 得 . 喔耶 .

(21)

### Full binary tree

Definition: a full binary tree of depth k is a bina ry tree of depth k having nodes, .

In other words, the maximum size tree of depth k (f ull)

All nodes except the leaves have two children

1

2 3

4 5 6 7

Full binary tree of depth 3

(22)

### Complete binary tree

Definition: A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k.

All leaves at level k-1 and k-2 (the last two levels) have no “missing ones

1

2 3

4 5

1

2 3

4 5 6

1 2

1

2 3

5 6

Yes Yes Yes No

1 2

4 5

No

(23)

### Height of a complete binary tree

Exercise: if a complete binary tree has n node, wha t’s the height of the tree?

Hint: a full binary tree of height k has nodes

Hint: what is the minimum size of such a tree in te rms of k?

Hint: use a ceiling or floor function

(24)

### Binary Tree Traversal

How do we traverse each node in the given binary tree?

When we reach a certain node, there are three possible act ions:

1. Go to left child (use L to represent left branch)

2. Go to right child (use R to represent right branch)

3. Process the data of this node (use V to represent visiti ng this node)

1

2 3

4 5 6 7

(25)

### Binary Tree Traversal

Usually L takes place before R.

Then we have 3 possibilities:

VLR: preorder

LVR: inorder

LRV: postorder

Preorder: ABCGE

Please write down the results of inorder & postorde r traversals.

A

B E C G

(26)

### Recursive traversal

Use recursive implementation for tree traversal:

void inorder(treePointer ptr) { inorder(ptr->leftChild);

visit(ptr);

inorder(ptr->rightChild);

}

A

B E C G

(27)

Use a stack to help:

for(;;) {

for(;node;node=node->leftChild) push(node);

node=pop();

if (!node) break;

printf(“%s”, node->data);

node=node->rightChild;

}

A

B E C G

(28)

### Arithmetic Expression

Example: 1+2*3-5/(4+5)/5

We have:

Operand – 1, 2, 3, 5, 4, 5, etc.

Operator – + * - /

Parenthesis– (,)

Feature 1: left-to-right associativity

Feather 2: infix: operator is between two operands

Association order is according to the priority of the oper ator

Example: multiplication’s priority is larger than additio n

(29)

### Alternative expressions

Postfix: put the operator after the two operands

Example

2+3*4 2 3 4 * +

a*b+5  ?

(1+2)*7  ?

a*b/c ?

(a/(b-c+d))*(e-a)*c?

a/b-c+d*?

e-a*c?

(30)

### Binary tree with arithmetic expression

Every arithmetic expression can be converted to an expression tree

Preorder  prefix

Inorder  infix

Postorder  postfix

Exercise:

Draw the arithmetic expression tree of (a/(b-c+d))*(e-a)*c

*

- 5

2 3

(31)

### Level-order traversal

What if we use a queue to help with the traversal?

for(;;) {

ptr=delete();

if (ptr) {

printf(“%s”, ptr->data);

} else break;

}

A

B E C G

(32)

Problem: looking for the grade of a particular stud ent in the university database.

Assumptions:

We know the student ID (key)

Use the key to find the location where the data is stored

Frequent removal of students who dropped out

(33)

Definition: A binary search tree is a binary tree. It may be empty. If it is not empty then it satisfies the following pr operties:

1. The root has a key.

2. The keys (if any) in the left subtree are smaller than th e key in the root

3. The keys (if any) in the right subtree are larger than th e key in the root

4. The left and right subtrees are also binary search trees

(Hidden) All keys are distinct.

Note that the definition is recursive. root.key

<root.key >root.key

(34)

Are these binary search trees?

What’s next when we find the node?

20

15 25

12 10 22

5 40

2

60

70

65 80

Yes

No Yes

No. 55

HW1 65

HW2 65

HW3

Extra -20000

(35)

### BST struct definition

struct BinarySearchTreeNode { int data;

struct BinarySearchTreeNode *left;

struct BinarySearchTreeNode *right;

};

(36)

struct TreeNode* find(struct TreeNode* root, int data) { if (root==NULL) return NULL;

if (data==root->data) return root;

if (data<root->data) return find(root->left,data);

return find(root->right, data);

}

Time complexity = O(??)

A: O(h), h: height of the tree.

Worst case: Average case:

12 22

10 15 25

Binary Search Tree

Algorithm usually is like:

(1) If the key matches the key of this node, then we process and return.

(2) If key is larger or smaller, then use a

recursive call to process left or right branch.

(37)

### Other operations

Q: How to find the largest or smallest key in the BST?

A: Keep going right(right), until reaching NULL (a lea f).

Q: How to list all keys in a binary search tree in ascen ding order?

A: Perform inorder traversal of the BST.

20

12 22

10 15 25

(38)

Search if there exists the same key in the BST (Recall the rule: each key in the BST is unique)

If not found, insert at the last location (where we cannot find the key)

Insert: 11

20

12 22

10 15 25

1 1

(39)

if (root==NULL) {

root=(struct BinarySearchTreeNode*)malloc(sizeof(struct BinarySea rchTreeNode));

if (root==NULL) {

printf("Error\n");

exit(-1);

}

root->data=data;

root->left=NULL;

root->right=NULL;

}else{

if (data<root->data)

root->left=Insert(root->left,data);

else if (data>root->data)

root->right=Insert(root->right,data);

}

return root;

}

If larger or smaller, use a recursive call to

process.

Finding

NULL means we have

reached a leaf and the key is not found.  insert at this location.

(40)

First find the location of the node

Then, according to the conditions:

The node with the key

has no branch (degree=0)

Remove the node and then done!

20

12 22

10 15 25

1 1

(41)

If the node with the key has one branch (degree=1)

Then get its only child and attach to the parent

Example: remove 25

Q: How to remember the pointer?

(Karumanchi p.152)

20

12 22

10 15 25

1 1

(42)

What if both branches of the node with the key exist (degree=2)?

Example: remove 12

Find the largest node of the left branch (or the smallest of

the right branch)

Remove that node and move it to where the node with the key was.

Q: What if that node still has child node(s)?

20

12 22

10 15 25

1 1

A: the re wo uld b

e onl y one

child .

(43)

printf(“error\n”);

return NULL;

} else if (data < root->data)

root->left=delete(root->left, data);

else if (data > root->data)

root->right=delete(root->right, data);

else { // data == root->data

if (root->left && root->right) { //two children temp=findmax(root->left);

root->data=temp->data;

root->left=delete(root->left,root->data);

}else{ // one child or no child temp=root;

if (root->left==NULL) root=root->right;

if (root->right==NULL) root=root->left;

free(temp);

}

return root; } }

smaller, use a recursive call to

process.

If we have found the key, process it here.

level. If we

delete the

current node, we can use this to connect the

parent node to a child

node.

The case presented here is of a geriatric patient who presented to us with a fracture at the right parasymphysis and left dentoalveolar region of the mandible and was su ﬀering

Consider the following example where the left graph is G and the right graph is the spanning tree with the minimum total routing cost.. Finally, there are

 In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children, the first child is called the left child and the second one is called

The execution of a comparison-based algorithm can be described by a comparison tree, and the tree depth is the greatest number of comparisons, i.e., the worst-case

For 5 to be the precise limit of f(x) as x approaches 3, we must not only be able to bring the difference between f(x) and 5 below each of these three numbers; we must be able

• Tree lifetime: When the first node is dead in tree T, the rounds number of the node surviving is the lifetime of the tree. The residual energy of node is denoted as E)), where

Describe and analyze a recursive algorithm to reconstruct an arbitrary full binary tree, given its preorder and postorder node sequences as

• Goal is to construct a no-arbitrage interest rate tree consistent with the yields and/or yield volatilities of zero-coupon bonds of all maturities.. – This procedure is

• As the binary quadratic programming program is NP-hard in general, identifying polynomially solvable subclasses of binary quadratic programming problems not only offers the-

Primal-dual approach for the mixed domination problem in trees Although we have presented Algorithm 3 for ﬁnding a minimum mixed dominating set in a tree, it is still desire to

• Definition: A max tree is a tree in which the key v alue in each node is no smaller (larger) than the k ey values in its children (if any). • Definition: A max heap is a

• An algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output.. • An algorithm is

In digital systems, a register transfer operation is a basic operation that consists of a transfer of binary information from one set of registers into another set of

A Boolean function described by an algebraic expression consists of binary variables, the constant 0 and 1, and the logic operation symbols.. For a given value of the binary

(It is also acceptable to have either just an image region or just a text region.) The layout and ordering of the slides is specified in a language called SMIL.. SMIL is covered in

Examples of thermal image (left) and processed binary images (middle and right) of

The min-max and the max-min k-split problem are defined similarly except that the objectives are to minimize the maximum subgraph, and to maximize the minimum subgraph respectively..

Given a graph and a set of p sources, the problem of finding the minimum routing cost spanning tree (MRCT) is NP-hard for any constant p &gt; 1 .. When p = 1, i.e., there is only

There is no general formula for counting the number of transitive binary relations on A... The poset A in the above example is not

(Shift the binary point the number of places equal to the value of the exponent. Shift right if the exponent is positive, or left if the exponent is negative.).. From left to

• For a given set of probabilities, our goal is to construct a binary search tree whose expected search is smallest.. We call such a

• Grow the binomial tree from these three nodes until time T to obtain a binomial-trinomial tree with..

• We need to make each barrier coincide with a layer of the binomial tree for better convergence.. • The idea is to choose a Δt such