250 likes | 258 Views
Learn about binary tree structure, insertion, deletion, traversal methods, and binary search tree activity.
E N D
Binary tree structure Root Node stores the first data item Node can have up to 2 descendants Leaf Nodes have no descendants Root Node Node Node Node Node Leaf Leaf Leaf Leaf Leaf
Descendants are the children Parent Left Child Right Child
Binary tree structure – subtrees and branches Root Branch Left Subtree Right Subtree
Which way? • Less than – Goes to the Left • More than – Goes to the Right
Adding items to a Binary Tree 5 3 7 4 9 1 6 2 8 5 3 7 9 1 4 6 2 8
Constructing a binary tree Daniel Charles George Belinda Cheryl Fred • The following data items are to be stored in a binary tree: • Daniel, Charles, Belinda, Cheryl, George and Fred.
Add a name to the binary tree • To add the name Pete and Edward Daniel Charles George Belinda Cheryl Fred Pete Edward
Adding items to a Search Tree W A T F O R D Y X W A Y T X F D O R
A .- B -... C -.-. D -.. E . F ..-. G --. Morse Code Binary Search Tree Activity H .... I .. J .--- K -.- L .-.. M -- N -. O --- P .--. Q --.- R .-. S ... T - U ..- V ...- W .-- X -..- Y -.-- Z --.. Morse Root ? ?
Morse Code Binary Search Tree Root E T I A N M S U R W D K G O H V F L P J B X C Y Z Q
Binary Tree Traversal Pre-Order In-Order Post-Order
What is traversal? • Methods of looking at each node in a tree, in turn • Recursive • Pre-Order, In-Order, Post-Order • Recursively Defined – Calls itself within itself • Must have an End Condition
Pre order Traversal Draw an outline around the tree starting to the left of the root, output each node as you pass to it’s left Root Node John Adam Simon Adam Dan Richard Output:John, Adam, Adam, Dan, Simon, Richard
Post order Traversal Draw an outline around the tree starting to the left of the root, output each node as you pass to the right of it. Root Node John Adam Simon Adam Dan Richard Output:Adam, Dan, Adam, Richard, Simon, John
In order Traversal Draw an outline around the tree starting to the left of the root, output each node as you pass beneath it Root Node John Adam Simon Adam Dan Richard Output:Adam, Adam, Dan, John, Richard, Simon
Golden Rules Pre – Left Post – Right In - Under
Pre-Order Traversal 8 4 2 1 3 6 5 7 12 10 9 11 14 13 15 8 12 4 10 14 2 6 1 3 5 7 9 11 13 15
In-Order Traversal 1-2-3-4-5-6-7-8-9-10-11-12-13-14-15 8 12 4 10 14 2 6 1 3 5 7 9 11 13 15
Post-Order Traversal 1 3 2 5 7 6 4 9 11 10 13 15 14 12 8 8 12 4 10 14 2 6 1 3 5 7 9 11 13 15
Binary Tree – Insertion & Deletion Root Node stores the first data item Node can have up to 2 descendants Leaf Nodes have no descendants Root Node Node Node Node Node Leaf Leaf Leaf Leaf Leaf
Structure of a Node Each node stores 4 pieces of Data This allows the tree structure to be stored with an array Position Data Item Right Pointer Address Left Pointer Address
Insert 4 into tree Insert 8 into tree 5 3 7 9 1 4 6 2 8
Inserting a Node Pseudo Code CurrentNode = Root Repeat If NewNode < CurrentNode travel Left Else travel Right CurrentNode = Node Reached Until Node = Null Create Node(NewNode)
Deleting a Node • 2 Methods of removing a Node within a Tree • Remove Node and all its sub Nodes • Store Sub Nodes on a temp store (array) • Reinsert each Node stored on temp store OR • Set Data Item of deleted node to Null • Leave node within structure