710 likes | 836 Views
Trees, Trees, and More Trees. Trees, Trees, and More Trees.
E N D
Trees, Trees, and More Trees By looking at forests of terms, awesome animations, and complete examples, we hope to get at the root of trees. Hopefully, each of you leaves with something new and exciting learned about trees. Try not to lose sight of the forest through the trees. Sorry, this was all very bad. I hope it didn’t leaf a bad taste in your mouth.
TREES A binary tree is a set of elements that is either empty or contains a single element (the root of the tree) and whose remaining elements are partitioned into two disjoint subsets, each of which itself is a binary tree.
root H Right subtree rooted at P Left subtree P D L W G A R E
H root level 0 Left subtree Right subtree P D level 1 L W G A level 2 R E level 3
A is the root of the tree. A is the parent of B and C. A is an ancestor of all nodes. A B C D E F G N O L M H I J K
B and C are siblings. J is a descendent of B. A B C D E F G N O L M H I J K
Height of binary tree : • Number of nodes on the longest path from the root to a leaf. • The height of the empty tree is 0. • The height of a single node tree is 1. • Note: (not an AP term) the definition of “height” is not from some “cs bible” – some will define other ways
Height of binary tree ? A B C D E F G N O L M H I J K
Height of binary tree ? A B C D E F G N O L M H I J K Height = 4
Full Binary : A recursive definition A binary tree is full if the tree is empty or if the tree's left and right subtrees have the same height and both are full
Binary Search Tree • A binary search tree : • every node in the left subtree is less than or equal to its parent node. • Every node in the right subtree is greater than its parent node • When the tree is traversed in order, the values of the nodes will be in order.
To insert to a binary search tree: if value less than go left else go right
Insert : 15 8 25 6 14 24 20 22 30 13 26 15
Insert : 14 8 25 6 14 24 20 22 30 13 26 15 8
Insert : 14 8 25 6 14 24 20 22 30 13 26 15 8 25
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 14
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14 20
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14 20 22
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 6 24 14 30 20 22
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 24 6 14 30 20 13 22
Insert : 15 8 25 6 14 24 20 22 30 13 26 15 8 25 24 6 14 30 26 13 20 22
Tree Traversals See also: animations on web site
Inorder traversal of a binary tree left…root…right A B C D E F G N O L M H I J K
Inorder traversal of a binary tree left…root…right A B C D E F G N O L M H I J K H D I B J E K A L F M C N G O
Preorder traversal of a binary tree: root…left…right A B C D E F G N O L M H I J K
Preorder traversal of a binary tree: root…left…right A B C D E F G N O L M H I J K A B D H I E J KC F L M G N O
Postorder traversal of a binary tree: left…right…ROOT A B C D E F G N O L M H I J K
Postorder traversal of a binary tree: left…right…ROOT A B C D E F G N O L M H I J K H I D J K E B L M F N O G C A
Inorder traversal ? L B W X E F G T E P A Q
Inorder traversal : L B W X E F G T E P A Q XA B E Q L P F W T G E
Preorder traversal ? L B W X E F G T E P A Q
Preorder traversal: L B W X E F G T E P A Q L B X A E Q W F PG T E
Postorder traversal ? L B W X E F G T E P A Q
Postorder traversal : L B W X E F G T E P A Q A X Q E B P F T E G W L
breadth-first-order tree traversal • ROW (or level) order traversal A B C F G D E H I
breadth-first-order tree traversal • ROW (or level) order traversal A B C D E F G H I A B C F G D E H I
Inorder Traversal ? 15 8 25 24 6 14 30 26 13 20 22
Inorder Traversal : 15 8 25 24 6 14 30 26 13 20 22 6 8 13 14 15 20 22 24 25 26 30
Preorder Traversal ? 15 8 25 24 6 14 30 26 13 20 22
Preorder Traversal : 15 8 25 24 6 14 30 26 13 20 22 15 8 6 14 13 25 24 20 22 30 26
Postorder Traversal ? 15 8 25 24 6 14 30 26 13 20 22
Postorder Traversal : 15 8 25 24 6 14 30 26 13 20 22 6 13 14 8 22 20 24 26 30 25 15
Our Tree: 15 8 25 24 6 14 30 26 13 20 22 What is the height of the tree?
Height : 15 8 25 24 6 14 30 26 13 20 22 Height = 5
Deleting a node from a binary tree L D P H C A F J
To delete a leaf... L D P H C A F J