C ++ Implementation of left Tree

Source: Internet
Author: User

/* <Br/> different from the Balance Tree (the balance tree has a very small depth, which means that the number of edges passing through any node is very small ), the left-side tree is not designed to <br/> quickly access all nodes. It aims to quickly access the smallest node and quickly restore the heap after modifying the tree. <Br/> the left-side tree is a type of heap that can be merged and is often used in priority queues. </P> <p> the left-biased tree has two properties: <br/> 1) the properties of the heap (Note: Generally, the heap is a complete binary tree, but not here ): <br/> A [Parent (I)]> = A [I] or a [Parent (I)] <= A [I], that is, the parent node is greater than (or less) the value of the subnode (this "value" can be regarded as the key or priority ). </P> <p> 2) Left-biased nature: <br/> the distance between the left subnode and the right subnode is no less than that of the right subnode, that is, dist (left (I )) ≥dist (right (I )). <Br/> the distance between the nodes is equal to the distance of the right sub-node plus 1, that is, dist (I) = dist (right (I) + 1; if the right child node of a node is a null node, <br/> the distance is 0. To satisfy the requirements, the distance between the empty nodes is-1. </P> <p> here, the left-side tree provides the following functions: <br/> 1) insert a node, time complexity O (logn) <br/> 2) obtain the root node (that is, the smallest node (if it is the smallest heap) or the largest node (if it is the largest heap), time complexity O (1) <br/> 3) delete the root node deleteroot, time complexity O (logn) <br/> 4) merge two left biased trees merge, time complexity O (logn) <br/> */<br/> # include <iostream> <br/> # include <functional> <br/> # include <cassert> <br/> using namespace std; </P> <p> // node data structure <br/> template <class type> <br/> struct node <br/>{< br/> type data; // data <br/> int Dist; // distance <br/> node <t Ype> * left; // left son pointer <br/> node <type> * right; // right son pointer </P> <p> node (type Val) <br/>{< br/> DATA = val; <br/> Dist = 0; <br/> left = right = NULL; <br/>}</P> <p> ~ Node () <br/>{< br/>}< br/>}; </P> <p> // left biased tree <br/> template <class type, class compare = less <type>/* Small heap by default */> <br/> class leftisttree <br/>{< br/> PRIVATE: <br/> node <type> * root; </P> <p> // Delete the left tree with the specified node as the root <br/> void Delete (node <type> * node) <br/>{< br/> If (null! = Node) <br/>{< br/> Delete (node-> left); <br/> Delete (node-> right); <br/> delete node; <br/> // node = NULL; <br/>}</P> <p> // determines whether the specified node has a left son. <br/> // static bool hasleft (node <type> * node) <br/> // {<br/> // If (! Node) return false; // The empty node does not have a left son <br/> // return node-> left; <br/> // </P> <p> // obtain the left son of a specified node <br/> static node <type> * & left (node <type> * & node) <br/>{< br/> assert (null! = Node); <br/> return node-> left; <br/>}</P> <p> // determines whether the specified node has a right son. <br/> // static bool hasright (node <type> * node) <br/> // {<br/> // If (! Node) return false; // The empty node does not have the right son <br/> // return node-> right; <br/> // </P> <p> // obtain the right son of a specified node <br/> static node <type> * & right (node <type> * & node) <br/>{< br/> assert (null! = Node); <br/> return node-> right; <br/>}</P> <p> // obtain the distance from a specified node <br/> static int dist (node <type> * node) <br/>{< br/> If (null = node) Return-1; // specify the distance between empty nodes as-1 <br/> return node-> Dist; <br/>}</P> <p> // switch left pointer and right pointer <br/> static void swap (node <type> * & left, node <type> * & right) <br/> {<br/> node <type> * temp = left; <br/> left = right; <br/> right = temp; <br/>}</P> <p> // merge two left-biased trees (return the root node of the left-biased tree after merging) <br/> static node <type> * & Merge (node <type> * & T1, node <type> * & T2) <br/> {</P> <p> If (null = T1) <br/> return T2; <br/> else if (null = t2) <br/> return T1; </P> <p> // confirm T1, who are the merged root nodes of T2 to meet the heap nature of the left Tree (A [Parent (I)]> = A [I] or a [Parent (I)] <= A [I]). <Br/> // but the "heap" here is not necessarily a full Binary Tree <br/> // note that it is a recursive process <br/> If (compare () (T2-> data, T1-> data) // compare is a comparison rule, which determines whether the heap is a large heap or a small heap <br/> swap (T1, T2 ); <br/> right (T1) = Merge (right (T1), T2); </P> <p> // sorts the Left and Right sons, to meet the left-biased nature of the tree, that is, the distance between the left subnode of the node is not less than the distance between the right subnode <br/> If (Dist (right (T1)> dist (left (T1) <br/> swap (left (T1), right (T1 )); <br/> // adjust the distance <br/> If (null = right (T1) <br/> T1-> Dist = 0; <br/> else T1-> Dist = dist (right (T1) + 1; </P> <p> Return T1; <br/>}</P> <p> // output the left-biased tree with the specified node as the root (recursive first-order traversal) <br/> void print (node <type> * node) <br/> {<br/> If (null! = Node) <br/>{</P> <p> cout <node-> data <Endl; </P> <p> cout <"Node" <node-> data <":"; <br/> If (null! = Node-> left) <br/> Print (node-> left); <br/> else <br/> cout <"null "; <br/> cout <Endl; </P> <p> cout <"Node" <node-> data <"has the right son :"; <br/> If (null! = Node-> right) <br/> Print (node-> right); <br/> else <br/> cout <"null "; <br/> cout <Endl; <br/>}< br/> Public: </P> <p> leftisttree (): Root (null) <br/>{< br/>}</P> <p> ~ Leftisttree () <br/>{< br/> Delete (Root ); <br/>}</P> <p> // insert an element to the left-side tree. <br/> void insert (type Val) <br/>{< br/> node <type> * newnode; <br/> newnode = new node <type> (VAL ); <br/> root = Merge (root, newnode ); <br/>}</P> <p> // Delete the root node of the left-side tree. <br/> void deleteroot () <br/>{< br/> If (null = root) <br/>{< br/> cout <"Warning: left tree blank "<Endl; <br/> return; <br/>}< br/> node <type> * P = root; <br/> root = Merge (p-> left, p-> right); <br /> Delete P; <br/>}</P> <p> // merge two left-biased trees. <br/> // after merging, T2 is an empty tree. <Br/> void Merge (leftisttree <type, compare> & T2) <br/> {<br/> root = Merge (root, t2.root ); <br/> t2.root = NULL; <br/>}</P> <p> // obtain the root node value (equivalent to the priority) <br/> type root () <br/>{< br/> If (null = root) <br/>{< br/> cout <"left Tree blank" <Endl; <br/> return NULL; <br/>}< br/> return root-> data; <br/>}</P> <p> // output the elements in the left-side tree. <br/> void print () <br/>{< br/> Print (Root); <br/> cout <Endl; <br/>}< br/> }; </P> <p> int main () <br/>{< br/> // create a left-side tree for a small heap <br/>{< br/> leftisttree <double> tree; <br/> tree. insert (5); <br/> tree. insert (4); <br/> tree. insert (3); <br/> tree. insert (8); <br/> tree. insert (1); <br/> tree. insert (2); <br/> tree. insert (9); <br/> tree. insert (7); <br/> tree. insert (6); <br/> tree. insert (0); <br/> cout <"first left Tree:" <Endl; <br/> tree. print (); </P> <p> tree. deleteroot (); <br/> tree. deleteroot (); <br/> cout <"New Left biased tree:" <Endl; <br/> tree. print (); </P> <p> cout <"New Root Node of the left-biased tree:" <tree. root () <Endl; </P> <p> leftisttree <double> TR; <br/> tr. insert (4.3); <br/> tr. insert (9.6); <br/> tr. insert (5.5); <br/> tr. insert (6.6); </P> <p> cout <"second left Tree:" <Endl; <br/> tr. print (); <br/> cout <"root node of the second left-biased tree:" <tr. root () <Endl; </P> <p> tree. merge (TR); <br/> cout <"the merged left Tree is:" <Endl; <br/> tree. print (); <br/>}</P> <p> cout <"-----------------------------------" <Endl; </P> <p> // create a left-side tree for a large heap <br/> {<br/> leftisttree <double, greater <double> tree2; <br/> tree2.insert (5); <br/> tree2.insert (4); <br/> tree2.insert (3); <br/> tree2.insert (8 ); <br/> tree2.insert (1); <br/> tree2.insert (2); <br/> tree2.insert (9); <br/> tree2.insert (7 ); <br/> tree2.insert (6); <br/> tree2.insert (0); <br/> cout <"first left Tree:" <Endl; <br/> tree2.print (); </P> <p> tree2.deleteroot (); <br/> tree2.deleteroot (); <br/> cout <"New left-side tree:" <Endl; <br/> tree2.print (); </P> <p> cout <"New Root Node of the left-biased tree:" <tree2.root () <Endl; </P> <p> leftisttree <double, greater <double> tr2; <br/> tr2.insert (4.3); <br/> tr2.insert (9.6 ); <br/> tr2.insert (5.5); <br/> tr2.insert (6.6); </P> <p> cout <"second left Tree:" <Endl; <br/> tr2.print (); <br/> cout <"root node of the second left-biased tree:" <tr2.root () <Endl; </P> <p> tree2.merge (tr2); <br/> cout <"the left-side tree after merging is:" <Endl; <br/> tree2.print (); <br/>}</P> <p> return 0; <br/>}

 

References:

Http://www.docin.com/p-1775556.html

Http://www.ozobo.cn /? P = 78

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.