1 / 14

Trees

Trees. Overview. Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary Tree. Tree. A tree is a collection of nodes and directed edges , satisfying the following properties:

Download Presentation

Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Trees

  2. Overview • Tree • Representation and Terminology • Binary Trees • Binary Search Trees • Pointer-Based Representation of a Binary Tree • Array-Based Representation of a Binary Tree

  3. Tree • A tree is a collection of nodes and directed edges, satisfying the following properties: • There is one specially designated node called the root, which has no edges pointing to it. • Every node except the root has exactly one edge pointing to it. • There is a unique path (of nodes and edges) from the root to each node.

  4. Trees: NotTrees: root edge node GraphicalRepresentation • Trees, as defined on the preceding slide, are typically drawn with circles (or rectangles) representing the nodes and arrows representing the edges. • The root is typically placed at the top of the diagram, with the rest of the tree below it. Leaf nodes

  5. Terminology (Cont’d.) • If an edge goes from node a to node b, then a is called the parent of b, and b is called a child of a. • Children of the same parent are called siblings. • If there is a path from a to b, then a is called an ancestor of b, and b is called a descendent of a. • A node with all of its descendants is called a subtree. • If a node has no children, then it is called a leaf of the tree. • If a node has no parent (there will be exactly one of these), then it is the root of the tree.

  6. A B C F D E G H I J K Terminology: Example • A is the root • D, E, G, H, J & K are leaves • B is the parent of D, E & F • D, E & F are siblings and children of B • I, J & K are descendants of B • A & B are ancestors of I subtree

  7. (These two binary trees are distinct.) Binary Trees • Intuitively, a binary tree is LIKE a tree in which each node has no more than two children. • Formally, a binary tree is a set T of nodes such that either: • T is empty, or • T consists of a single node, r, called the root, and two (non-overlapping) binary trees, called the left and rightsubtrees of r. • UNLIKE trees, binary trees may be empty, and they distinguish between left and rightsubtrees.

  8. Data Data Data Data Data Data Data Left_child Left_child null null null Left_child null Right_child null null null Right_child Right_child null Sample Binary Tree Root

  9. 21 John 3 34 Brenda Peter 55 2 8 Amy Mary Tom 5 13 Binary Search Trees • A binary search tree is a binary tree in which each node, n, has a value satisfying the following properties: • n’s value is > all values in its left subtree, TLeft, • n’s value is < all values in its right subtree, TRight, and • TLeftand TRightare both binary search trees.

  10. Terminology (Cont’d.) • Intuitively, the level of a node is the number of nodes on a path from the root to the node. • Formally, level of node, n: • If n is the root of a tree, then it is at level 1. • Otherwise, its level is 1 greater than the level of its parent. • Height of binary tree, T: • If T is empty, its height is 0. • Otherwise, its height is the maximum level of its nodes, or, equivalently, 1 greater than the height of the root’s taller subtree. Namely, height(T) = 1 + max { height( TLeft ), height(TRight ) }

  11. Terminology (Cont’d.) • Intuitively, a binary tree is full if it has no missing nodes. • Formally, a binary tree of height h is fullif • It is empty (h = 0). • Otherwise, the root’s subtrees are full binary trees of height h – 1. • If not empty, each node has2 children, except the nodes at level h which have no children. • Alternatively, the binary tree has all of its leaves at level h.

  12. Terminology (Cont’d.) • Intuitively, a binary tree of height h is complete if it is full down to level h – 1, and level h is filled from left to right. • Formally, a binary tree of height h is complete if • All nodes at level h – 2 and above have 2 children each, • If a node at level h – 1 has children, all nodes to its left at the same level have 2 children each, and • If a node at level h – 1 has 1 child, it is a left child.

  13. Terminology (Cont’d.) • A binary tree is balanced if the difference in height between any node’s left and right subtree is  1. • Note that: • A full binary tree is also complete. • A complete binary tree is not always full. • Full and complete binary trees are also balanced. • Balanced binary trees are not always full or complete.

  14. Operations on Tree • Different operations on a Tree are as follows • Insertion • Deletion • Searching • Traversal • Pre-Order • In-Order • Post- Order • Please follow the Power Point Presentations on Insertion, Deletion and Traversal using Binary Search Tree.

More Related