data structure -...

54
Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park

Upload: others

Post on 15-Jan-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Data Structure- Binary Tree 1 -

Hanyang University

Jong-Il Park

Division of Computer Science and Engineering, Hanyang University

Basic Tree Concepts

Logical structures

Linear list Tree Graph

Linear structures Non-linear structures

Chap. 5 Chap. 6Chap. 2~4

Division of Computer Science and Engineering, Hanyang University

Linear Lists and Trees

Linear lists are useful for serially ordered data. (e0, e1, e2, …, en-1) Days of week Months in a year Students in this class

Trees are useful for hierarchically ordered data. Employees of a corporation.

President, vice presidents, managers, and so on.

Division of Computer Science and Engineering, Hanyang University

Hierarchical Data and Trees

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

Elements next in the hierarchy are the childrenof the root.

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

Elements that have no children are leaves.

Division of Computer Science and Engineering, Hanyang University

great grand child of root

grand children of root

children of root

Example Tree

President

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

root

Division of Computer Science and Engineering, Hanyang University

Example Tree two types of genealogical charts

Division of Computer Science and Engineering, Hanyang University

Terminology(Cont.) Def. Tree : A finite set of one or more nodes s.t.

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

(2) The remaining nodes are partitioned into n disjoint sets T1, .. , Tn , where each of these sets is a tree. T1, .. , Tn are called the subtrees of the root.

node, branch

degree # of subtrees of a node

Division of Computer Science and Engineering, Hanyang University

Terminology (Cont.) leaf or terminal node

nodes that have degree zero nonterminal nodes

siblings children of the same parent

degree of a tree the maximum of the degree of the nodes in the tree

ancestors of a node all the nodes along the path from the root to that node

Division of Computer Science and Engineering, Hanyang University

Terminology (Cont.) level of a node

defined by letting the root be at level one

height or depth of a tree the maximum level of any node in the tree

Division of Computer Science and Engineering, Hanyang University

Representation of trees:1) Left child-right sibling representation every node has at most one leftmost child and at

most one closest right sibling

Division of Computer Science and Engineering, Hanyang University

Representation of trees: 2) Representation as a degree-two tree

have the two children of a node binary trees left child-right child trees

Division of Computer Science and Engineering, Hanyang University

Representation of trees: 2) Representation as a degree-two tree (Cont.)

Division of Computer Science and Engineering, Hanyang University

BINARY TREES

Division of Computer Science and Engineering, Hanyang University

Definition: A Tree in which each node has no more than 2 children (left subtree and right subtree)

Typical Representation in C

typedef struct Tree_Node *PtrToNode;typedef struct PtrToNode Tree;

struct Tree_Node{

ElementType Element;Tree Left;Tree Right;

};

r

TL TR

16

Binary trees

Division of Computer Science and Engineering, Hanyang University

The abstract data type a binary tree

empty tree having zero nodes consist of a root and two disjoint binary trees

(left subtree and right subtree)

Binary tree Binary search tree

Division of Computer Science and Engineering, Hanyang University

The abstract data type(cont.) distinctions between a binary tree and a tree

a binary tree is a set of nodes either empty or … there is no tree having zero nodes but there is an

empty binary tree in a binary tree, distinguish between the order of the

children; in a tree we do not

the same as a tree

Division of Computer Science and Engineering, Hanyang University

The abstract data type (Cont.)

Division of Computer Science and Engineering, Hanyang University

The abstract data type (Cont.) a skewed binary tree: Figure 5.10(a) a complete binary tree: Figure 5.10(b)

Division of Computer Science and Engineering, Hanyang University

Properties of binary trees Lemma 5.2 [Maximum number of nodes]

1) maximum number of nodes on level i of a binary tree: 2i-1

2) maximum number of nodes in a binary tree of depth k : 2k – 1 Sum of max. no. of nodes on level i

Lemma 5.3 [Relation between # of leaf nodes and degree-2 nodes] n0: # of leaf nodes n2: # of nodes of degree 2 n0 = n2 + 1

Division of Computer Science and Engineering, Hanyang University

Properties of binary trees (Cont.) Lemma 5.3 [Relation between # of leaf nodes and

degree-2 nodes] (Cont.) Proof:

n1 : # of nodes of degree one n : the total number of nodes n = n0 + n1 + n2 ----- (5.1) B : the number of branches n = B + 1

• every node except the root has a branch leading into it B = n1 + 2n2

n = B + 1 = n1 + 2n2 + 1 ----- (5.2) n0 = n2 + 1 from Eq. 5.1 and 5.2

Division of Computer Science and Engineering, Hanyang University

Properties of binary trees (Cont.) Definition: a full binary tree of depth k is a binary

tree of depth k having 2k - 1 nodes

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

Division of Computer Science and Engineering, Hanyang University

BINARY TREE REPRESENTATION

Division of Computer Science and Engineering, Hanyang University

Array representation

can easily determine the locations of the parent, left child, and right child of any node

Lemma 5.4: in a complete binary tree, for any node i1) parent(i) = 2) leftchild(i) = 2i3) rightchild(i) = 2i + 1

2/i

Division of Computer Science and Engineering, Hanyang University

Array representation (Cont.)

for the skewed tree, space is wasted

Division of Computer Science and Engineering, Hanyang University

Linked representation general inadequacies of sequential representation

wasteful for a skewed tree insertion and deletion of nodes from the middle of a

tree require memory movement

linked representation

typedef struct node *treePointer;typedef struct node {

int data;treePointer leftChild,

rightChild;};

typedef struct node *treePointer;typedef struct node {

int data;treePointer leftChild,

rightChild;};

Division of Computer Science and Engineering, Hanyang University

Linked representation (Cont.)

/* global variable */treePointer root;/* global variable */treePointer root;

Division of Computer Science and Engineering, Hanyang University

• Binary Search Tree: representing ordered sets of elements

• Expression Tree: intermediate representation for expressions used by the compiler

Expression Tree: An Example(a+b*c)+((d*e+f)*g)

+

+

+

*

a * g

b c * f

d e

29

Application of binary trees

Division of Computer Science and Engineering, Hanyang University

BINARY TREE TRAVERSAL

Division of Computer Science and Engineering, Hanyang University

Introduction traverse a tree or visit each node in the tree exactly

once the order in which the nodes are visited let L, V, and R stand for moving left, visiting the node,

and moving right six traversals: LVR, LRV, VLR, VRL, RVL, RLV consider only that we traverse left before right

• LVR: inorder -> infix• LRV: postorder -> postfix• VLR: preorder -> prefix

Division of Computer Science and Engineering, Hanyang University

Three ways to visit or traverse every node of a tree T, whose root is rand whose subtrees are T1, T2,..., Tk.

• Preorder: Visit r, then recursively do a pre-order traversal of T1, T2,..., Tk.

[+,+,a,*,b,c,*,+,*,d,e,f,g] Example: Depth-First Search

• Postorder: Recursively do post-order traversal of T1, T2,..., Tk, and then visit r.

[a,b,c,*,+,d,e,*,f,+,g,*,+] Example: Code generation for Compilers

• Inorder: (for binary tree) Do in-order traversal of TL, visit r, then do in-order traversal of TR.

[a,+,b,*,c,+,d,*,e,+,f,*,g] Example: Binary Search Tree

Traversal of binary trees

32

Division of Computer Science and Engineering, Hanyang University

Inorder traversal

move left until we cannot go farther, and then visit the node, move one node to the right and continue

Division of Computer Science and Engineering, Hanyang University

- Left subtree root right subtree

Inorder traversal (Cont.)

Division of Computer Science and Engineering, Hanyang University

Preorder traversal visit a node, traverse left, and continue if we cannot continue, move right and begin again or

move back

Division of Computer Science and Engineering, Hanyang University

Postorder traversal

Division of Computer Science and Engineering, Hanyang University

Iterative inorder traversal to implement the tree traversal method without using

recursion by using iterators

Division of Computer Science and Engineering, Hanyang University

Level-Order Traversal a traversal that requires a queue

visit the root first, then the root's left child, followed by the root's right child

Division of Computer Science and Engineering, Hanyang University

Traversal without a stack

1. add a parent field to each node can trace way back up to any root and down again

or

2. represent binary trees as threaded binary trees require two bits per node use the leftChild and rightChild fields to maintain the

paths back to the root

Division of Computer Science and Engineering, Hanyang University

ADDITIONAL BINARY TREE OPERATIONS

Division of Computer Science and Engineering, Hanyang University

Copying binary trees Modification of the postorder traversal algorithm

Division of Computer Science and Engineering, Hanyang University

Testing equality determine the equality of two binary trees

equivalent if two binary trees have the same topology and the identical data topology: every branch in one tree corresponds to a branch in

the second tree in the same order

Division of Computer Science and Engineering, Hanyang University

The satisfiability problem the formulas of the propositional calculus

a variable is an expression if x and y are expressions, x ∧ y, x ∨ y, ¬x are

expressions can use parentheses to alter the normal order of

evaluation x1 ∨ (x2 ∧¬x3)

the satisfiability problem for formulas of propositional calculus ask whether or not there is an assignment of values to

the variables that causes the value of the expression to be true

Division of Computer Science and Engineering, Hanyang University

The satisfiability problem (Cont.)

(x1 ∧¬x2) ∨ (¬x1 ∧ x3) ∨¬x3

let (x1, x2, x3) take on all possible combinations of true and false check the formula for each combination to evaluate an expression, need to traverse its tree in postorder

Division of Computer Science and Engineering, Hanyang University

The satisfiability problem (Cont.) define this node structure in C

• True and False : constants

typedef enum {not,and,or,true,false} logical;typedef enum {not,and,or,true,false} logical;

typedef struct node *treePointer;typedef struct node {

treePointer leftChild;logical data;short int value;treePointer rightChild;};

typedef struct node *treePointer;typedef struct node {

treePointer leftChild;logical data;short int value;treePointer rightChild;};

Division of Computer Science and Engineering, Hanyang University

The satisfiability problem (Cont.)

See Program 5.9.

Division of Computer Science and Engineering, Hanyang University

THREADED BINARY TREES

Division of Computer Science and Engineering, Hanyang University

Threads there are more 0-links than actual pointers

n + 1 0-links and 2n total links where # of nodes is n

replace the 0-links by pointers, called threads1) replace the rightChild field(value = 0) in node p by the

inorder successor of p2) replace the leftChild(value = 0) at node p by the

inorder predecessor of p

Division of Computer Science and Engineering, Hanyang University

Threads (Cont.)

distinguish between threads and normal pointers add two boolean fields, leftThread and rightThread if tleftThread == TRUE, then tleftChild is a thread

Division of Computer Science and Engineering, Hanyang University

Threads (Cont.)

assume a head node for all threaded binary trees the original tree is the left subtree of the head node

Division of Computer Science and Engineering, Hanyang University

Threads (Cont.)

Division of Computer Science and Engineering, Hanyang University

Inorder traversal of a threaded binary tree

if xrightThread == true, then the inorder successor of x is xrightChild

if xrightThread == false, then the inorder successor of x is obtained by following a path of left-child links from the right child of x until a node with leftThread == true

Division of Computer Science and Engineering, Hanyang University

Inorder traversal of a threaded binary tree (Cont.)

Program 5.10

Division of Computer Science and Engineering, Hanyang University

Inserting a node into a threaded binary tree

how to make insertions into a threaded tree

the case of inserting r as the right child of a node s if s has an empty right subtree, then a simple insertion if s has non-empty right subtree, then make this right

subtree as the right subtree of r

Division of Computer Science and Engineering, Hanyang University

Inserting a node into a threaded binary tree (Cont.)

Division of Computer Science and Engineering, Hanyang University

Inserting a node into a threaded binary tree (Cont.)