100 likes | 207 Views
ITEC 2620M Introduction to Data Structures. Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: TEL 3049. Double Linked List and Binary Trees. Key Points of this Lecture. Multiple pointers Doubly linked lists Binary trees Binary search trees.
E N D
ITEC 2620MIntroduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: TEL 3049
Key Points of this Lecture • 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. • Storing two pointers • One to the node following it • A second pointer to the node preceding it • Code
Binary Trees • 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