trees a tree's a tree. how many more do you need to look at? --ronald reagan

16
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Upload: diana-wright

Post on 27-Dec-2015

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

TREESA tree's a tree. How many more do you need to look at?

--Ronald Reagan

Page 2: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Tree Rules• One root• Each node may have 1 or more children• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root

root

child

parent

leaf leaf

Page 3: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Tree Example

Root node

Node with 2 children

Node with right child

Leaf node

Node with left child

Page 4: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Tree Examples

Page 5: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Tree Examples

Page 6: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Binary Tree Rules• One root• Each node can have a maximum of 2 child nodes

• Left child• Right child

• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root

Page 7: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Binary Search Tree

• One root• Each node can have a maximum of 2 child nodes

• Left child• Right child

• The left child’s value is less than the node.• The right child’s value is greater than the node.• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root

Page 8: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan
Page 9: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Full Binary Tree• Every leaf has the same depth

Page 10: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Complete Binary Tree• All new leaves of a full binary tree are added from the left.

• Add left-most nodes first

Page 11: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Heap• One root• Each node can have a maximum of 2 child nodes

• Left child• Right child

• Complete binary tree• Every node must have max children except…• Leaf level can be incomplete if all nodes are as far left as possible

• The node’s value is >= the value of its children• The left child’s value is less than (or equal to) the node.• The right child’s value is less than (or equal to) the node.

• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root

Page 12: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

B Tree

• One root• Each node can have MINIMUM entries (key indices) for children nodes up to twice the value

of MINIMUM for a single node.• The entries are stored in an array, sorted from the smallest to largest• The number of subtrees below a nonleaf node is always one more than the number of entries

in the node.• For any nonleaf node an entry at index i is greater than all the entries in subtree number i of

that node and an entry at index i is less than all the entries in subtree number i+ 1 of the node.• Data items are only stored in leaves• All leaves have the same depth• Each node except the root has exactly 1 parent• Moving up a tree to each parent leads to the root

Page 13: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Tree Traversals (pp 500)• Climbing all nodes of a tree• Traversals are named by when the root is processed and

“root” implies all roots of all subtrees• Pre-order traversal• In-order traversal• Post-order traversal

Page 14: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Pre-Order Traversal• Root is processed previous to its subtrees• 1. process root• 2. process nodes in left subtree recursively• 3. process nodes in right subtree recursivelytemplate <class Item>void preorder_print(binary_tree_node <Item>* node_ptr){ if (node_ptr != NULL) { //1. process root std::cout << node_ptr->data() << std::endl; //2. process left preorder_print(node_ptr->left()); //3. process right preorder_print(node_ptr->right()); }}

Page 15: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

In-Order Traversal• Root is processed between its subtrees• 1. process nodes in left subtree recursively• 2. process root• 3. process nodes in right subtree recursivelytemplate <class Item>void inorder_print(binary_tree_node <Item>* node_ptr){ if (node_ptr != NULL) { //1. process left inorder_print(node_ptr->left()); //2. process root std::cout << node_ptr->data() << std::endl; //3. process right inorder_print(node_ptr->right()); }}

Page 16: TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan

Post-Order Traversal• Root is processed after its subtrees• 1. process nodes in left subtree recursively• 2. process nodes in right subtree recursively• 3. process roottemplate <class Item>void postorder_print(binary_tree_node <Item>* node_ptr){ if (node_ptr != NULL) { //1. process left postorder_print(node_ptr->left()); //2. process right postorder_print(node_ptr->right()); //3. process root std::cout << node_ptr->data() << std::endl; }}