GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Trees in Python - GeeksforGeeks Trees in PythonLast Updated : 11 Aug, 2026 A tree is a hierarchical, non-linear data structure made up of nodes connected by edges. It starts with a single root node, and every child node has exactly one parent, creating a parent-child relationship. Trees are widely used to represent hierarchical data such as file systems, organizational structures, XML/HTML documents and decision trees. ![]() Basic Tree TerminologiesBefore learning different types of trees, let's understand some important terms used in a tree data structure.
Representation of a NodeA tree is built using nodes, where each node stores some data and references to its child nodes. In Python, a node can be represented by creating a class with a data field and a list to store its children. Example: Here, we create a simple Node class to represent a node in a tree.
Explanation:
Types of Tree Data StructuresTrees come in different forms, each designed for specific use cases. Below are some of the most commonly used tree data structures. 1. Binary Tree: a tree in which each node can have at most two children, called the left child and right child. It is commonly used to represent hierarchical data and forms the basis for many other tree structures.
2. Binary Search Tree (BST): a special type of binary tree where:
This ordering makes searching, insertion and deletion more efficient.
3. AVL Tree: a self-balancing Binary Search Tree. After every insertion or deletion, it automatically balances itself so that the height difference between the left and right subtrees of any node is at most 1.
4. Red Black Tree: another self-balancing Binary Search Tree. It uses color-based balancing rules to maintain an approximately balanced structure, ensuring efficient search, insertion and deletion operations.
5. B-Tree: a balanced tree in which each node can contain multiple keys and multiple child nodes. It is widely used in databases and file systems because it minimizes disk access and efficiently handles large amounts of data.
6. B+ Tree: a variation of the B-Tree in which all data is stored in the leaf nodes, while internal nodes are used only for searching. The leaf nodes are linked together, making B+ Trees ideal for fast sequential access and database indexing.
Properties of a Tree Data StructureSome important properties of a tree data structure are:
Implementation of TreeA way to represent a tree in Python is by creating a Node class. Each node stores its own data and a list of child nodes. We can then connect multiple nodes to form a tree.
Output Root: A Child: B Child: C Child of B: D Explanation:
Tree Traversal TechniquesTree traversal is the process of visiting every node in a tree exactly once in a specific order. Different traversal techniques are used depending on how the tree needs to be processed. Three most common depth-first traversal techniques are:
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |