to a node in a linked list • A linked list node has a reference to zero or one other link nodes • A tree node has a reference to zero or more other tree nodes
In other words, the node on top is a parent node to the node directly below, which is called the child node • An edge, which represents this hierarchal relationship, connects the nodes
several different types based on their position in the tree 1.Root node • The node at the “top” of the tree • The only node without a parent node 2.Interior node • A node with a parent and at least one child node
children 4.Parent node • The node one level above the current node that is connected to the current node by an edge 5.Child node • The node one level below the current node that is connected to the current node by an edge
parent, parent’s parent, extending to the root node • All nodes on the path from current node to root node Descendant nodes • The current node’s children, children’s children, extending to all of the leaf nodes • All nodes on all the paths from current node to the leaf nodes
root node to current node • For example, the root is at depth 0, its children at depth 1, their children are at depth 2, etc. • The height is the number of edges in the path from the root to a tree’s furthest (deepest) leaf • So the height is the maximum depth
No, because node 24’s left subtree’s height is 3 (three), and node 24’s right subtree’s height is 0 (zero) • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
visit each node of a binary tree • Here are the 3 most common traversal algorithms (all recursive) 1. Preorder traversal 2. Inorder traversal 3. Postorder traversal
binary tree in which: 1. The value of each node in the left subtree is less than the value of the root node 2. The value of each node in the right subtree is greater than the value of the root node 3. Contains no nodes of equal value
Yes, because each left child is less than its parent, each right child is greater than its parent, and no values are repeated • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18
No, because node 60, which is the left child of node 5, has a greater value than its parent • Node 60 should be the right child of node 58 • Tree of Integers 50 24 77 89 63 32 11 71 58 16 5 73 20 14 18 60
trees? 1.Quick to add, get, or remove a node • All these operations are O(log2 n) 2.Efficient to sort and search for data • That is why called a binary “search” tree
• For example, a linked list is the most extremely unbalanced binary tree • As a tree becomes more and more unbalanced, the Big-O for add, get, and remove operations will change from a fast O(log2 n) to a slow O(n)
not exist a. Draw the new node here as a root or a child node 2. Base case 2: if current node and new node have same value a. Do not add new node, because no duplicates are allowed
is less than current node’s value a. Add new node to left subtree 4. Recursive case 2: if new node’s value is greater than current node’s value a. Add new node to the right subtree
M, so move to right child node • O is less than P, so move to left child node • O is greater than N, so O is N’s right child node • Tree of Strings O N P M
M, so move to left child node • F is less than G, so move to left child node • F is greater than C, so F becomes C’s right child node • Tree of Strings F C G Q O N P M
M, so move to left • D is less than G, so move to left • D is greater than C, so move to right node • D is less than F, so D becomes F’s left node • Tree of Strings D A H F C G Q O N P M
M, so move to left node • K is greater than G, so move to right node • K is greater than H, so K becomes H’s right node • Tree of Strings K D A H F C G Q O N P M
(if such a node exists) 2. If deleted node is a leaf, you are finished 3. If deleted node has a single child node, replace the deleted node with the child node
a. Replace the deleted node with the rightmost node in the left subtree b. If the rightmost node in the left subtree has a left child, replace the rightmost node in the left subtree with its left child
deleted node with the rightmost node in the left subtree? • Have to preserve the order of binary search tree • All nodes on left subtree should be less than root and all nodes on right subtree should be greater than root
is the inorder predecessor (next smallest node) of the root node • So all other nodes in left subtree should be less than this node and all other nodes in right subtree should be greater than this node
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Get outside and go for a stroll