Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 (4.1-4.2) Weiss.

Similar presentations


Presentation on theme: "Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 (4.1-4.2) Weiss."— Presentation transcript:

1 Data Structures & Algorithm Analysis Muhammad Hussain Mughal Muhammad.hussain@nu.edu.pk Trees. Binary Trees. Reading: Chap.4 (4.1-4.2) Weiss

2 Books

3

4

5 More Trees Examples Unix / Windows file structure

6 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 T 1,..., T n, where each of these sets is a tree. We call T 1,..., T n the subtrees of the root.

7 Level and Depth Level 1 2 3 4 node (13) degree of a node leaf (terminal) nonterminal parent children sibling degree of a tree (3) ancestor level of a node height of a tree (4) 3 2 13 2 0 0 1 00 0 0 0 1 22 2 33 3 3 3 3 4 4 4

8 Terminology The degree of a node is the number of subtrees of 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.

9 Tree Properties A B C D G EF IH PropertyValue Number of nodes Height Root Node Leaves Interior nodes Number of levels Ancestors of H Descendants of B Siblings of E Right subtree

10 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 datalink 1link 2...link n How many link fields are needed in such a representation?

11 A Tree Node Every tree node: object – useful information children – pointers to its children nodes O OO O O

12 Left Child - Right Sibling ABCD E F G H I J K LM data left child right sibling

13 Tree ADT Objects: any type of objects can be stored in a tree Methods: accessor methods root() – return the root of the tree parent(p) – return the parent of a node children(p) – returns the children of a node query methods size() – returns the number of nodes in the tree isEmpty() - returns true if the tree is empty elements() – returns all elements isRoot(p), isInternal(p), isExternal(p)

14 Tree Traversal Two main methods: Preorder Postorder Recursive definition PREorder: visit the root traverse in preorder the children (subtrees) POSTorder traverse in postorder the children (subtrees) visit the root

15 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 traversal 1. Process the value in the node 2. Traverse the left subtree with a preorder traversal 3. Traverse the right subtree with a preorder traversal Postorder traversal 1. Traverse the left subtree with a postorder traversal 2. Traverse the right subtree with a postorder traversal 3. Process the value in the node

16 Preorder preorder traversal Algorithm preOrder(v) “visit” node v for each child w of v do recursively perform preOrder(w)

17 Postorder postorder traversal Algorithm postOrder(v) for each child w of v do recursively perform postOrder(w) “visit” node v du (disk usage) command in Unix

18 Binary Trees A special class of trees: max degree for each node is 2 Recursive 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

19 Example J IM H L A B C D E F G K

20 Samples of Trees ABABABCGEIDHF Complete Binary Tree Skewed Binary Tree ECD 1 2 3 4 5

21 Maximum Number of Nodes in BT The maximum number of nodes on level i of a binary tree is 2 i-1, i>=1. The maximum nubmer of nodes in a binary tree of depth k is 2 k -1, k>=1.

22 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 G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4 Complete binary tree

23 Sequential Representation A B -- C -- D --. E [1] [2] [3] [4] [5] [6] [7] [8] [9]. [16] [1] [2] [3] [4] [5] [6] [7] [8] [9] ABCDEFGHIABCDEFGHI ABECDABCGEIDHF (1) waste space (2) insertion/deletion problem

24 Linked Representation typedef struct tnode *ptnode; typedef struct tnode { int data; ptnode left, right; }; data leftright data leftright

25 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, Rlr inorder, postorder, preorder

26 CS 10326 Illustrations for Traversals Assume: visiting a node is printing its label Preorder: 1 3 5 4 6 7 8 9 10 11 12 Inorder: 4 5 6 3 1 8 7 9 11 10 12 Postorder: 4 6 5 3 8 11 12 10 9 7 1 1 3 11 98 46 5 7 12 10

27 CS 10327 Illustrations for Traversals (Contd.) Assume: visiting a node is printing its data Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30 Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30 Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 6 15 8 2 37 11 10 14 12 20 27 22 30

28 Arithmetic Expression Using BT +*A*/EDCB inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B

29 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

30 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

31 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 +

32 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);

33 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

34 Application: Evaluation of Expressions +*A*/EDCB inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B


Download ppt "Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 (4.1-4.2) Weiss."

Similar presentations


Ads by Google