Understanding Tree Structures in Computer Science: Types and Traversals Explained
160 likes | 275 Vues
This comprehensive guide explores various tree structures fundamental to computer science, including binary trees, binary search trees, full binary trees, complete binary trees, heaps, and B-trees. Each type is defined by specific rules regarding roots, nodes, and their relationships. Additionally, it outlines tree traversals, such as pre-order, in-order, and post-order, detailing how to process nodes during each traversal method. This resource is essential for students and practitioners looking to deepen their understanding of these critical data structures.
Understanding Tree Structures in Computer Science: Types and Traversals Explained
E N D
Presentation Transcript
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 parent leaf child leaf
Tree Example Root node Node with 2 children Node with right child Leaf node Node with left child
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
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
Full Binary Tree • Every leaf has the same depth
Complete Binary Tree • All new leaves of a full binary tree are added from the left. • Add left-most nodes first
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
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
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
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 recursively template <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()); } }
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 recursively template <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()); } }
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 root template <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; } }