1 / 16

Introduction to Data Structures: Double Linked List and Binary Trees

Learn about the concepts of doubly linked lists and binary trees, including their properties, operations, and traversal methods. This course provides a comprehensive understanding of these data structures and their applications.

woosley
Download Presentation

Introduction to Data Structures: Double Linked List and Binary Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITEC 2620MIntroduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: DB 3049

  2. Double Linked Listand 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 • 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 • 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 • 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? • Follow left till null • 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

More Related