Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.

Similar presentations


Presentation on theme: "Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees."— Presentation transcript:

1

2 Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees

3 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.

4 3 Trees Family Trees. Organisation Structure Charts. Program Design. Structure of a chapter in a book.

5 4 Parts of a Tree

6 5 nodes

7 6 Parts of a Tree parent node

8 7 Parts of a Tree child nodes parent node

9 8 Parts of a Tree child nodes parent node

10 9 Parts of a Tree root node

11 10 Parts of a Tree leaf nodes

12 11 Parts of a Tree sub-tree

13 12 Parts of a Tree sub-tree

14 13 Parts of a Tree sub-tree

15 14 Parts of a Tree sub-tree

16 15 Binary Tree Each node can have at most 2 children

17 16 Traversal Systematic way of visiting all the nodes. Methods: –Preorder, Inorder, and Postorder They all traverse the left subtree before the right subtree. The name of the traversal method depends on when the node is visited.

18 17 Preorder Traversal Visit the node. Traverse the left subtree. Traverse the right subtree.

19 18 Example: Preorder 31 43 64 20 4056 28334759 89 43 31 20 28 40 33 64 56 4759 89

20 19 Inorder Traversal Traverse the left subtree. Visit the node. Traverse the right subtree.

21 20 Example: Inorder 31 43 64 20 4056 28334759 89 20 31 28 40 33 43 56 4759 64 89

22 21 Postorder Traversal Traverse the left subtree. Traverse the right subtree. Visit the node.

23 22 Example: Postorder 31 43 64 20 4056 28334759 89 20 28 40 31 33 56 4759 64 89 43

24 23 Expression Tree A Binary Tree built with operands and operators. Also known as a parse tree. Used in compilers.

25 24 Example: Expression Tree / + / 1 3* 67 4 1/3 + 6*7 / 4

26 25 Notation Preorder –Prefix Notation Inorder –Infix Notation Postorder –Postfix Notation

27 26 Example: Infix / + / 1 3* 67 4 1 / 3 + * 67 / 4

28 27 7 / / / 1 + / 3* 6 4 Example: Postfix Recall: Reverse Polish Notation 1 13 3 6 6 7 7 * * 4 4 / / + +

29 28 / + / 1 3* 67 4 Example: Prefix +/13/*674

30 29 Binary Search Tree A Binary Tree such that: Every node entry has a unique key. All the keys in the left subtree of a node are less than the key of the node. All the keys in the right subtree of a node are greater than the key of the node.

31 30 Example 1: 31 43 64 20 4056 28334759 89 key is an integer

32 31 Example 1: 31 43 64 20 4056 28334759 89 key is an integer

33 32 Insert Create new node for the item. Find a parent node. Attach new node as a leaf.

34 33 Insert 31 43 64 20 4056 28334759 89 Example: 57

35 34 Insert 31 43 64 20 4056 28334759 89 Example: 57

36 35 Search: Checklist if target key is less than current node’s key, search the left sub-tree. else, if target key is greater than current node’s key, search the right sub-tree. returns: –if found, pointer to node containing target key. –otherwise, NULL pointer.

37 36 Search 31 43 64 20 4056 28334759 89 Example: 59 57 found 36

38 37 Search 31 43 64 20 4056 28334759 89 Example: 61 57 failed 37

39 38 Revision Binary Tree Preorder, Inorder, Postorder Traveral Expression Trees Prefix, Infix, and Postfix notation Insert and Search

40 39 Revision: Reading Kruse 9 Standish 9 Langsam 5 Deitel & Deitel 12.7 Preparation Next lecture: Binary Search Trees (Information Retrieval) Read Chapter 9.2 in Kruse et al.


Download ppt "Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees."

Similar presentations


Ads by Google