160 likes | 171 Views
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.
E N D
ITEC 2620MIntroduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: DB 3049
Key Points • Multiple pointers • Doubly linked lists • Binary trees • Binary search trees
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
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
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 ).
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.
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
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)
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
Key Points • Divide and conquer • Recursion • Tree traversals
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
Binary Search • Recursive sub-problem • if not found, do binary search in the remaining half to search • Base problem • return found or failure • Code
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
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