Binary Tree
Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
Some Terminologies Child and Parent Every node except the root has one parent. A node can have zero or more children. Leaves Leaves are nodes with no children. 1,3,7,8 Sibling nodes with the same parent. 2 and 8; 1 and 4; 3 and 7
Some Terminologies Path A sequence of edges. Length of a path: number of edges on the path Depth of a node = no. of edges from root Root is also a node, depth of root = ? Height of a node = length of longest path to a leaf Height of leaves = ? Height of 2 = ? Height of root = height of tree
Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
Short review on binary tree At most two children for each node A root node at the top (or the tree is empty) Nodes with no child=leaf nodes Left complete tree if: All levels are full except last level Last level filled from left to right
Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
Tree traversals To visit each node of the tree Recursively visiting left and right sub-trees Pre-order (NLR) Node Current Node, Left sub-tree, Right sub-tree In-order (LNR) Node Left, Node, Right Post-order (LRN) Node Left, Right, Node
Tree traversals Examples Pre-order (NLR) 9,4,2,3,8,6,7 Always print the node first
Tree traversals Examples In-order (LNR) 2,4,3,9,6,8,7 Print whenever return from left
Tree traversals Examples Post-order (LRN) 2,3,4,6,7,8,9 Print whenever return from right
Binary Tree Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST) Questions
Binary Search Tree Properties A Binary Tree (nodes at most two children) Node Left sub-tree < Node < Right sub-tree Which one(s) is/are BST(s)? (1)(2) (3)
Binary Search Tree Which one(s) is/are BST(s)? (2) (1) (3)
Binary Search Tree Which one(s) is/are BST(s)? (1)(2) (3)
Binary Search Tree Summary LeftNodeRight BST: Left < Node < Right Heap: Only requiring Parent > Children WHOLEsub-tree WHOLE left (or right) sub-tree X) All items in the sub-tree has to be X) NOT necessarily a Complete tree! Worse case can be a linked list! Same collection of elements can have different BSTs
Binary search tree Find key To look for a node with a certain Key Left < Node < Right Recursively do: 1. Compare(key,node) 2. Key<node: Go left 3. Key>node: Go right
Binary search tree Find the key 8 node 6, Key > 6 Go right, node 8, Key == 8 >
Binary search tree Find the key 3 node 6, Key < 6 Go left, node 2, Key > 2 Go right, node 4, Key < 4 Go left, node 3, Key == 3 O(1) at each level Find a key O(depth) if found O(height) if not found Time complexity=O(height) < > <
Binary search tree FindMin and FindMax Left < Node < Right Min = Left most node Recursively go left Until no more left child Max = Right most node Recursively go right Until no more right child
Binary search tree FindMin and FindMax
Binary search tree Insert To insert a key into BST, similar to Find Key Proceed as if you want to find the key If found, duplicate key do nothing; or update a counter; or insert to a list; or … Otherwise insert X at the last spot.
Binary search tree Insert the key 5 node 6, Key < 6 Go left, node 2, Key > 2 Go right, node 4, Key > 4 Go right, NULL, insert 5 O(1) at each level Again, O(height) < > 5
Binary search tree Delete To delete a key from a BST Proceed as if you want to find the key If found the node n, delete it! that simple Is it really that simple? I wish it was Step two is a bit harder: three cases
Binary search tree Delete the key 1 node 6, Key < 6 Go left, node 2, Key < 2 Go left, node 1, Key == 1 Case one: no child Delete it! < <
Binary search tree Delete the key 8 node 6, Key > 6 Go right, node 8, Key == 8 Case two: one child My parent bypass me, point to my child instead Really no violation? Left < X < Right >
Binary search tree Delete the key 2 node 6, Key < 6 Go left, node 2, Key == 2 Case two: two child Replace node 2 with Min node (m) of right sub-tree Recursively delete m Why choose this way? <
Binary search tree Delete the key 2, before and after
Binary search tree Delete Summary Case 1: node n is a leaf (no child): delete it! Case 2: node n has only one child: Parent bypass n, point to n.child and delete n Case 3: node n has two child: Replace n with smallest node (m) of the right sub-tree Recursively delete m O(height)? Smallest node=left-most node Either has right child; OR No child So, delete m can only be case 1 or 2, the simple cases