Data Structures & Algorithm Analysis
Muhammad Hussain [email protected]
Trees. Binary Trees.Reading: Chap.4 (4.1-4.2) Weiss
Definition of Tree
A tree is a finite set of one or more nodes such that:There is a specially designated node called the root.The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree.We call T1, ..., Tn the subtrees of the root.
Level and Depth
K L
E F
B
G
C
M
H I J
D
A
Level
1
2
3
4
node (13)degree of a nodeleaf (terminal)nonterminalparentchildrensiblingdegree of a tree (3)ancestorlevel of a nodeheight of a tree (4)
3
2 1 3
2 0 0 1 0 0
0 0 0
1
2 2 2
3 3 3 3 3 3
4 4 4
Terminology
The degree of a node is the number of subtreesof the node
The degree of A is 3; the degree of C is 1.
The node with degree 0 is a leaf or terminal node.A node that has subtrees is the parent of the roots of the subtrees.The roots of these subtrees are the children of the node.Children of the same parent are siblings.The ancestors of a node are all the nodes along the path from the root to the node.
Tree Properties
A
B C
D
G
E F
IH
Property ValueNumber of nodesHeightRoot NodeLeavesInterior nodesNumber of levelsAncestors of HDescendants of BSiblings of ERight subtree
Representation of Trees
List Representation( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )The root comes first, followed by a list of sub-trees
data link 1 link 2 ... link n
How many link fields are needed in such a representation?
A Tree Node
Every tree node:object – useful informationchildren – pointers to its children nodes
O
O O
O
O
Tree ADT
Objects: any type of objects can be stored in a treeMethods:accessor methods
root() – return the root of the treeparent(p) – return the parent of a nodechildren(p) – returns the children of a node
query methodssize() – returns the number of nodes in the tree isEmpty() - returns true if the tree is emptyelements() – returns all elementsisRoot(p), isInternal(p), isExternal(p)
Tree Traversal
Two main methods:PreorderPostorder
Recursive definition
PREorder: visit the roottraverse in preorder the children (subtrees)
POSTordertraverse in postorder the children (subtrees)visit the root
Trees
Tree traversals:Inorder traversal – prints the node values in ascending order
1. Traverse the left subtree with an inorder traversal
2. Process the value in the node (i.e., print the node value)
3. Traverse the right subtree with an inorder traversal
Preorder traversal1. Process the value in the node2. Traverse the left subtree with a preorder
traversal3. Traverse the right subtree with a preorder
traversal
Postorder traversal1. Traverse the left subtree with a postorder
traversal2. Traverse the right subtree with a postorder
traversal3. Process the value in the node
Preorder
preorder traversalAlgorithm preOrder(v)
“visit” node vfor each child w of v do
recursively perform preOrder(w)
Postorder
postorder traversalAlgorithm postOrder(v)
for each child w of v do recursively perform postOrder(w)“visit” node v
du (disk usage) command in Unix
Binary Trees
A special class of trees: max degree for each node is 2Recursive 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 the right subtree.Any tree can be transformed into binary tree.
by left child-right sibling representation
Maximum Number of Nodes in BT
The maximum number of nodes on level i of a binary tree is 2i-1, i>=1.The maximum nubmer of nodes in a binary tree of depth k is 2k-1, k>=1.
Full BT vs. Complete BT
A full binary tree of depth k is a binary tree of depth k having 2 -1 nodes, k>=0.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.
k
A
B C
GE
I
D
H
F
A
B C
GE
K
D
J
F
IH ONML
Full binary tree of depth 4Complete binary tree
Sequential Representation
AB--C------D--.E
[1][2][3][4][5][6][7][8][9].[16]
[1][2][3][4][5][6][7][8][9]
ABCDEFGHI
A
B
E
C
D
A
B C
GE
I
D
H
F
(1) waste space(2) insertion/deletion problem
Linked Representationtypedef struct tnode *ptnode;typedef struct tnode { int data; ptnode left, right;};
dataleft
right
data
left right
Binary Tree Traversals
Let L, V, and R stand for moving left, visiting the node, and moving right.There are six possible combinations of traversal
lRr, lrR, Rlr, Rrl, rRl, rlR
Adopt convention that we traverse left before right, only 3 traversals remain
lRr, lrR, Rlrinorder, postorder, preorder
CS 103 26
Illustrations for Traversals
Assume: visiting a node is printing its labelPreorder: 1 3 5 4 6 7 8 9 10 11 12Inorder:4 5 6 3 1 8 7 9 11 10 12Postorder:4 6 5 3 8 11 12 10 9 7 1
1
3
11
98
4 6
5
7
12
10
CS 103 27
Illustrations for Traversals (Contd.)
Assume: visiting a node is printing its dataPreorder: 15 8 2 6 3 711 10 12 14 20 27 22 30Inorder: 2 3 6 7 8 10 1112 14 15 20 22 27 30Postorder: 3 7 6 2 10 1412 11 8 22 30 27 20 15
6
158
2
3 7
11
10
14
12
20
27
22 30
Arithmetic Expression Using BT
+
*
A
*
/
E
D
C
B
inorder traversalA / B * C * D + Einfix expressionpreorder traversal+ * * / A B C D Eprefix expressionpostorder traversalA B / C * D * E +postfix expressionlevel order traversal+ * E * D / C A B
Inorder Traversal (recursive version)
void inorder(ptnode ptr)/* inorder tree traversal */{ if (ptr) { inorder(ptr->left); printf(“%d”, ptr->data); indorder(ptr->right); }}
A / B * C * D + E
Preorder Traversal (recursive version)
void preorder(ptnode ptr)/* preorder tree traversal */{ if (ptr) { printf(“%d”, ptr->data); preorder(ptr->left); predorder(ptr->right); }}
+ * * / A B C D E
Postorder Traversal (recursive version)
void postorder(ptnode ptr)/* postorder tree traversal */{ if (ptr) { postorder(ptr->left); postdorder(ptr->right); printf(“%d”, ptr->data); }}
A B / C * D * E +
Level Order Traversal(using queue)
void levelOrder(ptnode ptr)/* level order tree traversal */{ int front = rear = 0; ptnode queue[MAX_QUEUE_SIZE]; if (!ptr) return; /* empty queue */ enqueue(front, &rear, ptr); for (;;) { ptr = dequeue(&front, rear);
if (ptr) { printf(“%d”, ptr->data); if (ptr->left) enqueue(front, &rear, ptr->left); if (ptr->right) enqueue(front, &rear, ptr->right); } else break; }}
+ * E * D / C A B