Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 2620M Introduction to Data Structures

Similar presentations


Presentation on theme: "ITEC 2620M Introduction to Data Structures"— Presentation transcript:

1 ITEC 2620M Introduction to Data Structures
Instructor: Prof. Z. Yang Course Website: Office: DB 3049

2 Double Linked List and Binary Trees

3 Key Points Multiple pointers Doubly linked lists Binary trees
Binary search trees

4 Doubly Linked List Designed to allow convenient access from a list node to the next node and also to the preceding node on the list. Store two pointers One to the node following it A second pointer to the node preceding it Code

5 Binary Tree A binary tree is a structure that is either empty or which consists of one node connected to two disjoint (binary) subtrees. disjoint – no common nodes Each node of a binary tree has a value, a pointer to a left child node, and a pointer to a right child node (pointers may be NULL). A node is the parent of its child nodes. Example

6 More Definitions Length, path, ancestor, descendant, height, leaf, internal nodes A minimum-level binary tree has all levels full except the last level. A complete binary tree is a minimum-level binary tree with nodes filled in from the left on the last level. A full binary tree is a binary tree where each node has either 0 or 2 children (also called 2-tree ).

7 Binary Search Trees BST property:
For each node (with a key value of K) in the binary tree, all nodes in the left sub-tree will have key values less than K, and all nodes in the right sub-tree will have key values greater than K.

8 Searching BSTs If node has same key value, return it.
If node has larger key value, search the left sub-tree. If node has smaller key value, search the right sub-tree. if BST is “balanced”, we get binary search a full binary search tree has “ideal” balancing ~ 50% on each side of each node

9 Complexity Code What is the complexity for find? Best Worst Average
root node O(1) Worst end node O(n) Average depends on shape of tree! on average (i.e. reasonably balanced trees), O(logn)

10 Benefits of BST Balanced BSTs have O(logn) worst and average case find – like binary search on an array. BSTs have O(1) insert – like linked lists

11 Recursion and Binary Tree Operations

12 Key Points Divide and conquer Recursion Tree traversals

13 Recursion Divide and Conquer Recursion Example
Break a problem into smaller sub-problems that are easier to solve Recursion What happens when the sub-problems have the same form as the original problem? Solve smaller versions of the same problem repeatedly Recursion! Note: recursion requires a base case that can be solved trivially Example

14 Binary Search Recursive sub-problem Base problem Code
if not found, do binary search in the remaining half to search Base problem return found or failure Code

15 Tree Traversals If we store a phone book in a binary tree, how do we print it? print (in order) everything before the root name, print the root name, print everything after the root name Recursive sub-problem print left sub-tree, print this node, print right sub-tree Base problem no sub-trees Preorder traversal, postorder traversal

16 Why Recursion? What is the first node to print?
Follow left till null, print that node What is the second node to print? Follow left till null If right != null, print left-most node of right sub-tree Else, print parent node What is the third node to print? Conditions from left-most node of right sub-tree or parent... Exponential conditions!! Cannot do non-recursively Definition of a binary tree is recursive Binary tree operations are all recursive


Download ppt "ITEC 2620M Introduction to Data Structures"

Similar presentations


Ads by Google