Binary Trees Linked list are unary trees! Corresponding Book Sections • Data Structures: Section 7.1, 7.2 What Are Trees? A tree is a hierarchical data structure made up of nodes with parent and child relationships/references to other nodes 1 2 6 11 3 7 4 8 12 5 10 9 13 14 Tree Node Vocabulary • • • • • • • • Parent node - the node a node descends from in the tree Child node - node that descends from a given node in the tree Root node - node at the top of the tree Internal node - node with at least one child External node (leaf) - node with no children Sibling nodes - nodes with the same parent Ancestor - parent, grandparent, etc. of a node Descendent - child, grandchild, etc. of a node Tree Vocabulary • Height - How how many nodes away from a leaf node that a node is • Depth - How many nodes away from the root node that a node is • Subtree - Tree formed by taking a node from a given tree, as well as all descendants of that node Analyzing a Tree • • What is the root node? 1 What are the children of node 3? Descendants? • Who is the parent of node 6? Ancestors? • What is the height of this tree? What is the depth of node 8? • What are the siblings of node 8? 2 6 11 3 7 4 8 12 5 10 9 13 14 Examples of Tree Usage in Computer Science • File Systems - B-Trees • Java TreeMap - Red-Black Tree • Machine Learning - Decision Trees • AI - Mini-Max Trees • Heaps - We will get to this later in the semester • Printing tests! Decision Tree Temperature <35F Wind Direction Temperature <20F Wind Speed <4 Walk Drive >=20F East Precipitation >=4 No Yes >=3 Walk Not East Wind Speed Precipitation No Wind Speed Drive >=35F Walk <3 Drive Yes Drive <6 Walk >=6 Drive Trie (Dictionary Tree) http://blog.ivank.net/trie-in-as3.html Tree Traversals • Pre-order - A node is visited before its descendants • Post-order - A node is visited after its descendants Tree Traversals 1 2 6 11 3 7 4 8 12 5 10 9 13 14 Assuming left to right child traversal, what is the Pre Order traversal? What is the Post Order Traversal? Binary Search Tree • A Binary Tree is where a node has up to two children only • A Binary Search Tree is a tree where it is maintained that every value to the left (down the left child) of a node is less than the node, and every value to the right (down the right child of node is greater than the node Binary Search Tree Example D B A E C F Binary Search Tree Example E D C B A Big Oh of Binary Search Trees • Lookup time • O(lg n) if balanced • O(n) possible worst case if not balanced • Insertion time - same thing! • Deletion time? Will revisit after coding! In Order Traversal • Nodes are visited according to their logical ordering • How would you write the in-order traversal of a Binary Search Tree? • What should be printed first? second? last? • Write the pseudo-code!
© Copyright 2026 Paperzz