{"id":315679,"date":"2020-12-28T15:01:03","date_gmt":"2020-12-28T15:01:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=315679"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=315679","title":{"rendered":"Splay-\u0434\u0435\u0440\u0435\u0432\u043e. \u041f\u043e\u0438\u0441\u043a"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<blockquote>\n<p><strong>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0411\u0443\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432 \u043a\u0443\u0440\u0441\u0430 <\/strong><a href=\"https:\/\/otus.pw\/ffJk\/\"><strong>&#171;\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445&#187;<\/strong><\/a><strong> \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 <\/strong><a href=\"https:\/\/otus.pw\/hRbq\/\"><strong>\u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440 \u043f\u043e \u0442\u0435\u043c\u0435 &#171;\u0417\u0430\u043f\u043e\u0432\u0435\u0434\u043d\u0438\u043a\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u043f\u043e\u0438\u0441\u043a\u0430.&#187;<\/strong><\/a><\/p>\n<p>\u0410 \u0441\u0435\u0439\u0447\u0430\u0441 \u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0441 \u0432\u0430\u043c\u0438 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u043e\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430.<\/p>\n<\/blockquote>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/516\/202\/71e\/51620271ee4d5b230d9acaef9951467c\" width=\"780\" height=\"439\"><figcaption><\/figcaption><\/figure>\n<hr>\n<p>\u041d\u0430\u0438\u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u0430\u043a \u043f\u043e\u0438\u0441\u043a, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430, \u0434\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 (Binary Search Tree) \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 O(n). \u041d\u0430\u0438\u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0435\u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043d\u0430\u0438\u0445\u0443\u0434\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043e O(log n) \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u0445 \u0438 <a href=\"https:\/\/www.geeksforgeeks.org\/avl-tree-set-1-insertion\/\">\u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432<\/a>.<\/p>\n<p><strong>\u041c\u043e\u0436\u0435\u043c \u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0447\u0435\u043c \u0442\u043e\u0442, \u0447\u0442\u043e \u043d\u0430\u043c \u0434\u0430\u044e\u0442 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u0435 \u0438\u043b\u0438 \u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u044c\u044f?<\/strong><\/p>\n<p>\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u043c \u0438 <a href=\"https:\/\/www.geeksforgeeks.org\/avl-tree-set-1-insertion\/\">\u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c<\/a>, Splay-\u0434\u0435\u0440\u0435\u0432\u043e (\u0438\u043b\u0438 <em>\u043a\u043e\u0441\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/em>) \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <a href=\"http:\/\/en.wikipedia.org\/wiki\/Self-balancing_binary_search_tree\">\u0441\u0430\u043c\u043e\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u0443\u044e\u0449\u0438\u043c\u0441\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0430<\/a>. \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f splay-\u0434\u0435\u0440\u0435\u0432\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u043b\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f, \u0432 \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 O(1) \u043f\u0440\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435. \u0412\u0441\u044f \u0441\u0443\u0442\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0441\u044b\u043b\u043e\u043a (\u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 80% \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u043d\u0430 20% \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432). \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0441\u0435\u0431\u0435 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u044b \u043a\u043b\u044e\u0447\u0435\u0439, \u0438 \u043b\u0438\u0448\u044c \u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0438\u0437 \u043d\u0438\u0445 \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e, \u0447\u0442\u043e \u0432\u0435\u0441\u044c\u043c\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0412\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e splay-\u0434\u0435\u0440\u0435\u0432\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 O(log n), \u0433\u0434\u0435 n &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0435\u0440\u0435\u0432\u0435. \u041b\u044e\u0431\u0430\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0422\u044d\u0442\u0430(n).<\/p>\n<h3>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430&nbsp;<\/h3>\n<p>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 splay-\u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435, \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f (\u0438\u0441\u043a\u043e\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u0440\u0435\u043d\u044c \u2014 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f splay). \u0415\u0441\u043b\u0438 \u043f\u043e\u0438\u0441\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f \u0443\u0441\u043f\u0435\u0445\u043e\u043c, \u0442\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0432\u0435\u0440\u0445 \u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u043c \u043a\u043e\u0440\u043d\u0435\u043c. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u0440\u043d\u0435\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0443\u0437\u0435\u043b, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0431\u044b\u043b \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u043e \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f NULL.<\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0443\u0437\u043b\u0443 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u043b\u0443\u0447\u0430\u0438:<\/p>\n<p><strong>1.<\/strong> <strong>\u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c.<\/strong> \u041c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c, \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u0443\u0437\u0435\u043b, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f, \u0443\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c.<\/p>\n<p><strong>2. Zig: \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043a\u043e\u0440\u043d\u044e<\/strong><em> <\/em>(\u0443 \u0443\u0437\u043b\u0430 \u043d\u0435\u0442 \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f). \u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043a\u043e\u0440\u043d\u044f (\u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u0430\u0432\u044b\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442), \u043b\u0438\u0431\u043e \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u043b\u0435\u0432\u044b\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442).<\/p>\n<p>T1, T2 \u0438 T3 \u2014 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0434\u0435\u0440\u0435\u0432\u0430 \u0441 \u043a\u043e\u0440\u043d\u0435\u043c y (\u0441\u043b\u0435\u0432\u0430) \u0438\u043b\u0438 x (\u0441\u043f\u0440\u0430\u0432\u0430)<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e4d\/45f\/7a1\/e4d45f7a1f0f045e7bb3481b47945e10.png\" width=\"929\" height=\"182\"><figcaption><\/figcaption><\/figure>\n<p><strong>3. \u0423 <em>\u0443\u0437\u043b\u0430 \u0435\u0441\u0442\u044c \u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c, \u0438 \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c<\/em><\/strong>. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b:<\/p>\n<p><strong>\u0430) Zig-Zig \u0438 Zag-Zag.<\/strong> \u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u0434\u0432\u0430 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u0430 \u0432\u043f\u0440\u0430\u0432\u043e) \u0418\u041b\u0418 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c (\u0434\u0432\u0430 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u0430 \u0432\u043b\u0435\u0432\u043e).<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9ec\/912\/e5a\/9ec912e5a4f73d572477afea0c7164e8.png\" width=\"931\" height=\"530\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0431) Zig-Zag \u0438 Zag-Zig.<\/strong> \u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443, \u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442 \u0432\u043b\u0435\u0432\u043e \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u043e\u043c \u0432\u043f\u0440\u0430\u0432\u043e) \u0418\u041b\u0418 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442 \u0432\u043f\u0440\u0430\u0432\u043e \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u043e\u043c \u0432\u043b\u0435\u0432\u043e).<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d2e\/815\/4e8\/d2e8154e86fce1b46333c3d4e0095219.png\" width=\"936\" height=\"526\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f0e\/c05\/819\/f0ec0581924b4c84c9d800fd08d29bd1.png\" width=\"942\" height=\"324\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u043b\u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u0430 (splay) \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0432 \u043a\u043e\u0440\u0435\u043d\u044c, \u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0443\u0440\u0430\u0432\u043d\u043e\u0432\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0434\u0435\u0440\u0435\u0432\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u0448\u0435, \u0432\u044b\u0441\u043e\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1.<\/p>\n<h4> \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/h4>\n<h3>C++<\/h3>\n<pre><code class=\"cpp\">#include &lt;bits\/stdc++.h&gt;  using namespace std;   \/\/ An AVL tree node  class node  {  \tpublic:  \tint key;  \tnode *left, *right;  };   \/* Helper function that allocates  a new node with the given key and  \tNULL left and right pointers. *\/ node* newNode(int key)  {  \tnode* Node = new node();  \tNode-&gt;key = key;  \tNode-&gt;left = Node-&gt;right = NULL;  \treturn (Node);  }   \/\/ A utility function to right  \/\/ rotate subtree rooted with y  \/\/ See the diagram given above.  node *rightRotate(node *x)  {  \tnode *y = x-&gt;left;  \tx-&gt;left = y-&gt;right;  \ty-&gt;right = x;  \treturn y;  }   \/\/ A utility function to left  \/\/ rotate subtree rooted with x  \/\/ See the diagram given above.  node *leftRotate(node *x)  {  \tnode *y = x-&gt;right;  \tx-&gt;right = y-&gt;left;  \ty-&gt;left = x;  \treturn y;  }   \/\/ This function brings the key at  \/\/ root if key is present in tree.  \/\/ If key is not present, then it  \/\/ brings the last accessed item at  \/\/ root. This function modifies the  \/\/ tree and returns the new root  node *splay(node *root, int key)  {  \t\/\/ Base cases: root is NULL or  \t\/\/ key is present at root  \tif (root == NULL || root-&gt;key == key)  \t\treturn root;   \t\/\/ Key lies in left subtree  \tif (root-&gt;key &gt; key)  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;left == NULL) return root;   \t\t\/\/ Zig-Zig (Left Left)  \t\tif (root-&gt;left-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ First recursively bring the  \t\t\t\/\/ key as root of left-left  \t\t\troot-&gt;left-&gt;left = splay(root-&gt;left-&gt;left, key);   \t\t\t\/\/ Do first rotation for root,  \t\t\t\/\/ second rotation is done after else  \t\t\troot = rightRotate(root);  \t\t}  \t\telse if (root-&gt;left-&gt;key &lt; key) \/\/ Zig-Zag (Left Right)  \t\t{  \t\t\t\/\/ First recursively bring  \t\t\t\/\/ the key as root of left-right  \t\t\troot-&gt;left-&gt;right = splay(root-&gt;left-&gt;right, key);   \t\t\t\/\/ Do first rotation for root-&gt;left  \t\t\tif (root-&gt;left-&gt;right != NULL)  \t\t\t\troot-&gt;left = leftRotate(root-&gt;left);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;left == NULL)? root: rightRotate(root);  \t}  \telse \/\/ Key lies in right subtree  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;right == NULL) return root;   \t\t\/\/ Zag-Zig (Right Left)  \t\tif (root-&gt;right-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ Bring the key as root of right-left  \t\t\troot-&gt;right-&gt;left = splay(root-&gt;right-&gt;left, key);   \t\t\t\/\/ Do first rotation for root-&gt;right  \t\t\tif (root-&gt;right-&gt;left != NULL)  \t\t\t\troot-&gt;right = rightRotate(root-&gt;right);  \t\t}  \t\telse if (root-&gt;right-&gt;key &lt; key)\/\/ Zag-Zag (Right Right)  \t\t{  \t\t\t\/\/ Bring the key as root of  \t\t\t\/\/ right-right and do first rotation  \t\t\troot-&gt;right-&gt;right = splay(root-&gt;right-&gt;right, key);  \t\t\troot = leftRotate(root);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;right == NULL)? root: leftRotate(root);  \t}  }   \/\/ The search function for Splay tree.  \/\/ Note that this function returns the  \/\/ new root of Splay Tree. If key is  \/\/ present in tree then, it is moved to root.  node *search(node *root, int key)  {  \treturn splay(root, key);  }   \/\/ A utility function to print  \/\/ preorder traversal of the tree.  \/\/ The function also prints height of every node  void preOrder(node *root)  {  \tif (root != NULL)  \t{  \t\tcout&lt;&lt;root-&gt;key&lt;&lt;\" \";  \t\tpreOrder(root-&gt;left);  \t\tpreOrder(root-&gt;right);  \t}  }   \/* Driver code*\/ int main()  {  \tnode *root = newNode(100);  \troot-&gt;left = newNode(50);  \troot-&gt;right = newNode(200);  \troot-&gt;left-&gt;left = newNode(40);  \troot-&gt;left-&gt;left-&gt;left = newNode(30);  \troot-&gt;left-&gt;left-&gt;left-&gt;left = newNode(20);   \troot = search(root, 20);  \tcout &lt;&lt; \"Preorder traversal of the modified Splay tree is \\n\";  \tpreOrder(root);  \treturn 0;  }   \/\/ This code is contributed by rathbhupendra <\/code><\/pre>\n<h3>C<\/h3>\n<pre><code class=\"cs\">\/\/ The code is adopted from http:\/\/goo.gl\/SDH9hH  #include&lt;stdio.h&gt;  #include&lt;stdlib.h&gt;   \/\/ An AVL tree node  struct node  {  \tint key;  \tstruct node *left, *right;  };   \/* Helper function that allocates a new node with the given key and  \tNULL left and right pointers. *\/ struct node* newNode(int key)  {  \tstruct node* node = (struct node*)malloc(sizeof(struct node));  \tnode-&gt;key = key;  \tnode-&gt;left = node-&gt;right = NULL;  \treturn (node);  }   \/\/ A utility function to right rotate subtree rooted with y  \/\/ See the diagram given above.  struct node *rightRotate(struct node *x)  {  \tstruct node *y = x-&gt;left;  \tx-&gt;left = y-&gt;right;  \ty-&gt;right = x;  \treturn y;  }   \/\/ A utility function to left rotate subtree rooted with x  \/\/ See the diagram given above.  struct node *leftRotate(struct node *x)  {  \tstruct node *y = x-&gt;right;  \tx-&gt;right = y-&gt;left;  \ty-&gt;left = x;  \treturn y;  }   \/\/ This function brings the key at root if key is present in tree.  \/\/ If key is not present, then it brings the last accessed item at  \/\/ root. This function modifies the tree and returns the new root  struct node *splay(struct node *root, int key)  {  \t\/\/ Base cases: root is NULL or key is present at root  \tif (root == NULL || root-&gt;key == key)  \t\treturn root;   \t\/\/ Key lies in left subtree  \tif (root-&gt;key &gt; key)  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;left == NULL) return root;   \t\t\/\/ Zig-Zig (Left Left)  \t\tif (root-&gt;left-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ First recursively bring the key as root of left-left  \t\t\troot-&gt;left-&gt;left = splay(root-&gt;left-&gt;left, key);   \t\t\t\/\/ Do first rotation for root, second rotation is done after else  \t\t\troot = rightRotate(root);  \t\t}  \t\telse if (root-&gt;left-&gt;key &lt; key) \/\/ Zig-Zag (Left Right)  \t\t{  \t\t\t\/\/ First recursively bring the key as root of left-right  \t\t\troot-&gt;left-&gt;right = splay(root-&gt;left-&gt;right, key);   \t\t\t\/\/ Do first rotation for root-&gt;left  \t\t\tif (root-&gt;left-&gt;right != NULL)  \t\t\t\troot-&gt;left = leftRotate(root-&gt;left);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;left == NULL)? root: rightRotate(root);  \t}  \telse \/\/ Key lies in right subtree  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;right == NULL) return root;   \t\t\/\/ Zag-Zig (Right Left)  \t\tif (root-&gt;right-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ Bring the key as root of right-left  \t\t\troot-&gt;right-&gt;left = splay(root-&gt;right-&gt;left, key);   \t\t\t\/\/ Do first rotation for root-&gt;right  \t\t\tif (root-&gt;right-&gt;left != NULL)  \t\t\t\troot-&gt;right = rightRotate(root-&gt;right);  \t\t}  \t\telse if (root-&gt;right-&gt;key &lt; key)\/\/ Zag-Zag (Right Right)  \t\t{  \t\t\t\/\/ Bring the key as root of right-right and do first rotation  \t\t\troot-&gt;right-&gt;right = splay(root-&gt;right-&gt;right, key);  \t\t\troot = leftRotate(root);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;right == NULL)? root: leftRotate(root);  \t}  }   \/\/ The search function for Splay tree. Note that this function  \/\/ returns the new root of Splay Tree. If key is present in tree  \/\/ then, it is moved to root.  struct node *search(struct node *root, int key)  {  \treturn splay(root, key);  }   \/\/ A utility function to print preorder traversal of the tree.  \/\/ The function also prints height of every node  void preOrder(struct node *root)  {  \tif (root != NULL)  \t{  \t\tprintf(\"%d \", root-&gt;key);  \t\tpreOrder(root-&gt;left);  \t\tpreOrder(root-&gt;right);  \t}  }   \/* Driver program to test above function*\/ int main()  {  \tstruct node *root = newNode(100);  \troot-&gt;left = newNode(50);  \troot-&gt;right = newNode(200);  \troot-&gt;left-&gt;left = newNode(40);  \troot-&gt;left-&gt;left-&gt;left = newNode(30);  \troot-&gt;left-&gt;left-&gt;left-&gt;left = newNode(20);   \troot = search(root, 20);  \tprintf(\"Preorder traversal of the modified Splay tree is \\n\");  \tpreOrder(root);  \treturn 0;  }  <\/code><\/pre>\n<h3>Java<\/h3>\n<pre><code class=\"java\">\/\/ Java implementation for above approach  class GFG  {   \/\/ An AVL tree node  static class node  {   \tint key;  \tnode left, right;  };   \/* Helper function that allocates  a new node with the given key and  \tnull left and right pointers. *\/ static node newNode(int key)  {  \tnode Node = new node();  \tNode.key = key;  \tNode.left = Node.right = null;  \treturn (Node);  }   \/\/ A utility function to right  \/\/ rotate subtree rooted with y  \/\/ See the diagram given above.  static node rightRotate(node x)  {  \tnode y = x.left;  \tx.left = y.right;  \ty.right = x;  \treturn y;  }   \/\/ A utility function to left  \/\/ rotate subtree rooted with x  \/\/ See the diagram given above.  static node leftRotate(node x)  {  \tnode y = x.right;  \tx.right = y.left;  \ty.left = x;  \treturn y;  }   \/\/ This function brings the key at  \/\/ root if key is present in tree.  \/\/ If key is not present, then it  \/\/ brings the last accessed item at  \/\/ root. This function modifies the  \/\/ tree and returns the new root  static node splay(node root, int key)  {  \t\/\/ Base cases: root is null or  \t\/\/ key is present at root  \tif (root == null || root.key == key)  \t\treturn root;   \t\/\/ Key lies in left subtree  \tif (root.key &gt; key)  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root.left == null) return root;   \t\t\/\/ Zig-Zig (Left Left)  \t\tif (root.left.key &gt; key)  \t\t{  \t\t\t\/\/ First recursively bring the  \t\t\t\/\/ key as root of left-left  \t\t\troot.left.left = splay(root.left.left, key);   \t\t\t\/\/ Do first rotation for root,  \t\t\t\/\/ second rotation is done after else  \t\t\troot = rightRotate(root);  \t\t}  \t\telse if (root.left.key &lt; key) \/\/ Zig-Zag (Left Right)  \t\t{  \t\t\t\/\/ First recursively bring  \t\t\t\/\/ the key as root of left-right  \t\t\troot.left.right = splay(root.left.right, key);   \t\t\t\/\/ Do first rotation for root.left  \t\t\tif (root.left.right != null)  \t\t\t\troot.left = leftRotate(root.left);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root.left == null) ?  \t\t\t\t\t\t\troot : rightRotate(root);  \t}  \telse \/\/ Key lies in right subtree  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root.right == null) return root;   \t\t\/\/ Zag-Zig (Right Left)  \t\tif (root.right.key &gt; key)  \t\t{  \t\t\t\/\/ Bring the key as root of right-left  \t\t\troot.right.left = splay(root.right.left, key);   \t\t\t\/\/ Do first rotation for root.right  \t\t\tif (root.right.left != null)  \t\t\t\troot.right = rightRotate(root.right);  \t\t}  \t\telse if (root.right.key &lt; key)\/\/ Zag-Zag (Right Right)  \t\t{  \t\t\t\/\/ Bring the key as root of  \t\t\t\/\/ right-right and do first rotation  \t\t\troot.right.right = splay(root.right.right, key);  \t\t\troot = leftRotate(root);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root.right == null) ?  \t\t\t\t\t\t\troot : leftRotate(root);  \t}  }   \/\/ The search function for Splay tree.  \/\/ Note that this function returns the  \/\/ new root of Splay Tree. If key is  \/\/ present in tree then, it is moved to root.  static node search(node root, int key)  {  \treturn splay(root, key);  }   \/\/ A utility function to print  \/\/ preorder traversal of the tree.  \/\/ The function also prints height of every node  static void preOrder(node root)  {  \tif (root != null)  \t{  \t\tSystem.out.print(root.key + \" \");  \t\tpreOrder(root.left);  \t\tpreOrder(root.right);  \t}  }   \/\/ Driver code  public static void main(String[] args)  {  \tnode root = newNode(100);  \troot.left = newNode(50);  \troot.right = newNode(200);  \troot.left.left = newNode(40);  \troot.left.left.left = newNode(30);  \troot.left.left.left.left = newNode(20);   \troot = search(root, 20);  \tSystem.out.print(\"Preorder traversal of the\" +  \t\t\t\t\t\" modified Splay tree is \\n\");  \tpreOrder(root);  }  }   \/\/ This code is contributed by 29AjayKumar  <\/code><\/pre>\n<h3>C#<\/h3>\n<pre><code class=\"cs\">\/\/ C# implementation for above approach  using System;   class GFG  {   \/\/ An AVL tree node  public class node  {   \tpublic int key;  \tpublic node left, right;  };   \/* Helper function that allocates  a new node with the given key and  null left and right pointers. *\/ static node newNode(int key)  {  \tnode Node = new node();  \tNode.key = key;  \tNode.left = Node.right = null;  \treturn (Node);  }   \/\/ A utility function to right  \/\/ rotate subtree rooted with y  \/\/ See the diagram given above.  static node rightRotate(node x)  {  \tnode y = x.left;  \tx.left = y.right;  \ty.right = x;  \treturn y;  }   \/\/ A utility function to left  \/\/ rotate subtree rooted with x  \/\/ See the diagram given above.  static node leftRotate(node x)  {  \tnode y = x.right;  \tx.right = y.left;  \ty.left = x;  \treturn y;  }   \/\/ This function brings the key at  \/\/ root if key is present in tree.  \/\/ If key is not present, then it  \/\/ brings the last accessed item at  \/\/ root. This function modifies the  \/\/ tree and returns the new root  static node splay(node root, int key)  {  \t\/\/ Base cases: root is null or  \t\/\/ key is present at root  \tif (root == null || root.key == key)  \t\treturn root;   \t\/\/ Key lies in left subtree  \tif (root.key &gt; key)  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root.left == null) return root;   \t\t\/\/ Zig-Zig (Left Left)  \t\tif (root.left.key &gt; key)  \t\t{  \t\t\t\/\/ First recursively bring the  \t\t\t\/\/ key as root of left-left  \t\t\troot.left.left = splay(root.left.left, key);   \t\t\t\/\/ Do first rotation for root,  \t\t\t\/\/ second rotation is done after else  \t\t\troot = rightRotate(root);  \t\t}  \t\telse if (root.left.key &lt; key) \/\/ Zig-Zag (Left Right)  \t\t{  \t\t\t\/\/ First recursively bring  \t\t\t\/\/ the key as root of left-right  \t\t\troot.left.right = splay(root.left.right, key);   \t\t\t\/\/ Do first rotation for root.left  \t\t\tif (root.left.right != null)  \t\t\t\troot.left = leftRotate(root.left);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root.left == null) ?  \t\t\t\t\t\t\troot : rightRotate(root);  \t}  \telse \/\/ Key lies in right subtree  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root.right == null) return root;   \t\t\/\/ Zag-Zig (Right Left)  \t\tif (root.right.key &gt; key)  \t\t{  \t\t\t\/\/ Bring the key as root of right-left  \t\t\troot.right.left = splay(root.right.left, key);   \t\t\t\/\/ Do first rotation for root.right  \t\t\tif (root.right.left != null)  \t\t\t\troot.right = rightRotate(root.right);  \t\t}  \t\telse if (root.right.key &lt; key)\/\/ Zag-Zag (Right Right)  \t\t{  \t\t\t\/\/ Bring the key as root of  \t\t\t\/\/ right-right and do first rotation  \t\t\troot.right.right = splay(root.right.right, key);  \t\t\troot = leftRotate(root);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root.right == null) ?  \t\t\t\t\t\t\troot : leftRotate(root);  \t}  }   \/\/ The search function for Splay tree.  \/\/ Note that this function returns the  \/\/ new root of Splay Tree. If key is  \/\/ present in tree then, it is moved to root.  static node search(node root, int key)  {  \treturn splay(root, key);  }   \/\/ A utility function to print  \/\/ preorder traversal of the tree.  \/\/ The function also prints height of every node  static void preOrder(node root)  {  \tif (root != null)  \t{  \t\tConsole.Write(root.key + \" \");  \t\tpreOrder(root.left);  \t\tpreOrder(root.right);  \t}  }   \/\/ Driver code  public static void Main(String[] args)  {  \tnode root = newNode(100);  \troot.left = newNode(50);  \troot.right = newNode(200);  \troot.left.left = newNode(40);  \troot.left.left.left = newNode(30);  \troot.left.left.left.left = newNode(20);   \troot = search(root, 20);  \tConsole.Write(\"Preorder traversal of the\" +  \t\t\t\t\" modified Splay tree is \\n\");  \tpreOrder(root);  }  }   \/\/ This code is contributed by 29AjayKumar <\/code><\/pre>\n<p><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/strong><\/p>\n<p><code>Preorder traversal of the modified Splay tree is 20 50 30 40 100 200<\/code><\/p>\n<h3>\u0420\u0435\u0437\u044e\u043c\u0435<\/h3>\n<p><strong>1)<\/strong> Splay-\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0427\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0433\u043a\u043e \u043d\u0430\u0439\u0442\u0438. \u0420\u0435\u0434\u043a\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0435 \u043c\u0435\u0448\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u0438\u0441\u043a\u0435.<\/p>\n<p><strong>2) <\/strong>\u0412\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e splay-\u0434\u0435\u0440\u0435\u0432\u043e\u043c \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 O(log n). \u041c\u043e\u0436\u043d\u043e \u0441\u0442\u0440\u043e\u0433\u043e \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e Splay-\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 O(log n) \u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u0440\u0438 \u043b\u044e\u0431\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 (\u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430)<\/p>\n<p><strong>3)<\/strong> Splay-\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u043c\u0438 \u0438 <a href=\"https:\/\/www.geeksforgeeks.org\/avl-tree-set-1-insertion\/\">\u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c\u0438<\/a>, \u0442\u0430\u043a \u043a\u0430\u043a \u0443\u0437\u043b\u044b splay-\u0434\u0435\u0440\u0435\u0432\u0430 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439.<\/p>\n<p><strong>4)<\/strong> \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 <a href=\"https:\/\/www.geeksforgeeks.org\/avl-tree-set-1-insertion\/\">\u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u0430<\/a>, splay-\u0434\u0435\u0440\u0435\u0432\u043e \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u0430\u0436\u0435 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0447\u0442\u0435\u043d\u0438\u044f, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u043f\u043e\u0438\u0441\u043a.<\/p>\n<h4>\u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 Splay-\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432<\/h4>\n<p>Splay-\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0441\u0442\u0430\u043b\u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0435\u043d\u043d\u043e\u0439 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 30 \u043b\u0435\u0442, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u0431\u044b\u0441\u0442\u0440\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0434\u043b\u044f \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>Splay-\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 Windows NT (\u0432 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u0441\u0435\u0442\u0438 \u0438 \u043a\u043e\u0434\u0435 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b), \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0435 gcc \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 GNU C++, \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u0435 \u0441\u0442\u0440\u043e\u043a sed, \u0441\u0435\u0442\u0435\u0432\u044b\u0445 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u0430\u0445 Fore Systems, \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Unix malloc, \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u043c\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u044f\u0445 \u044f\u0434\u0440\u0430 Linux \u0438 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 (\u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a: <a href=\"http:\/\/www.cs.berkeley.edu\/~jrs\/61b\/lec\/36\">http:\/\/www.cs.berkeley.edu\/~jrs\/61b\/lec\/36<\/a>)<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0442\u0430\u043a\u0436\u0435 <a href=\"https:\/\/www.geeksforgeeks.org\/splay-tree-set-2-insert-delete\/\">Splay Tree | Set 2 (Insert)<\/a>.<\/p>\n<p><strong>\u0421\u0441\u044b\u043b\u043a\u0438:<\/strong><\/p>\n<p><a href=\"http:\/\/www.cs.berkeley.edu\/~jrs\/61b\/lec\/36\">http:\/\/www.cs.berkeley.edu\/~jrs\/61b\/lec\/36<\/a><\/p>\n<p><a href=\"http:\/\/www.cs.cornell.edu\/courses\/cs3110\/2009fa\/recitations\/rec-splay.html\">http:\/\/www.cs.cornell.edu\/courses\/cs3110\/2009fa\/recitations\/rec-splay.html<\/a><\/p>\n<p><a href=\"http:\/\/courses.cs.washington.edu\/courses\/cse326\/01au\/lectures\/SplayTrees.ppt\">http:\/\/courses.cs.washington.edu\/courses\/cse326\/01au\/lectures\/SplayTrees.ppt<\/a><\/p>\n<hr>\n<blockquote>\n<p><a href=\"https:\/\/otus.pw\/ffJk\/\"><strong>\u0423\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043a\u0443\u0440\u0441\u0435<\/strong><\/a><strong> &#171;\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445&#187;.<\/p>\n<p><\/strong><a href=\"https:\/\/otus.pw\/hRbq\/\"><strong>\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440<\/strong><\/a><strong> \u043f\u043e \u0442\u0435\u043c\u0435 &#171;\u0417\u0430\u043f\u043e\u0432\u0435\u0434\u043d\u0438\u043a\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u043f\u043e\u0438\u0441\u043a\u0430.&#187;<\/strong><\/p>\n<\/blockquote>\n<h3> \u0420\u0435\u043a\u043b\u0430\u043c\u0430 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0430<\/h3>\n<p>\u041f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441 \u0432 OTUS \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u043e\u0432\u043e\u0433\u043e\u0434\u043d\u0438\u0435 \u0441\u043a\u0438\u0434\u043a\u0438 \u043d\u0430 \u0432\u0441\u0435 \u043a\u0443\u0440\u0441\u044b. \u041e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043f\u043e\u043b\u043d\u044b\u043c \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043a\u0443\u0440\u0441\u043e\u0432 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 \u043d\u0438\u0436\u0435. \u0422\u0430\u043a\u0436\u0435 \u0443 \u0432\u0441\u0435\u0445 \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u0435\u0441\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0430\u0442\u0443&nbsp;<a href=\"https:\/\/otus.ru\/gifts\/?utm_source=habr&amp;utm_medium=affilate&amp;utm_campaign=otus&amp;utm_term=new_yea_gifts_21.12.2020\">\u043f\u043e\u0434\u0430\u0440\u043e\u0447\u043d\u044b\u0439 \u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442 \u043d\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0432 OTUS<\/a>.<\/p>\n<p><em>\u041a\u0441\u0442\u0430\u0442\u0438, \u043e &#171;\u043a\u0440\u0430\u0441\u0438\u0432\u043e\u0439 \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0435&#187; \u043e\u043d\u043b\u0430\u0439\u043d-\u0441\u0435\u0440\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0432 \u043c\u044b&nbsp;<\/em><a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/533778\/\"><em>\u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/em><\/a><em>.<\/em><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/57d\/1a7\/330\/57d1a733008803ed858d76dc2dfdb445.jpg\" width=\"780\" height=\"300\"><figcaption><\/figcaption><\/figure>\n<p><a href=\"https:\/\/otus.pw\/EipQ\/\"><strong>\u0417\u0410\u0411\u0420\u0410\u0422\u042c \u0421\u041a\u0418\u0414\u041a\u0423<\/strong><\/a><\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/535316\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/535316\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<blockquote>\n<p><strong>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u0411\u0443\u0434\u0443\u0449\u0438\u0445 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432 \u043a\u0443\u0440\u0441\u0430 <\/strong><a href=\"https:\/\/otus.pw\/ffJk\/\"><strong>&#171;\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445&#187;<\/strong><\/a><strong> \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u043d\u0430 <\/strong><a href=\"https:\/\/otus.pw\/hRbq\/\"><strong>\u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0432\u0435\u0431\u0438\u043d\u0430\u0440 \u043f\u043e \u0442\u0435\u043c\u0435 &#171;\u0417\u0430\u043f\u043e\u0432\u0435\u0434\u043d\u0438\u043a\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u043f\u043e\u0438\u0441\u043a\u0430.&#187;<\/strong><\/a><\/p>\n<p>\u0410 \u0441\u0435\u0439\u0447\u0430\u0441 \u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0441 \u0432\u0430\u043c\u0438 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u043e\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0433\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430.<\/p>\n<\/blockquote>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<hr>\n<p>\u041d\u0430\u0438\u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u0430\u043a \u043f\u043e\u0438\u0441\u043a, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430, \u0434\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 (Binary Search Tree) \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 O(n). \u041d\u0430\u0438\u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0435\u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043d\u0430\u0438\u0445\u0443\u0434\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043e O(log n) \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u0445 \u0438 <a href=\"https:\/\/www.geeksforgeeks.org\/avl-tree-set-1-insertion\/\">\u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432<\/a>.<\/p>\n<p><strong>\u041c\u043e\u0436\u0435\u043c \u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0447\u0435\u043c \u0442\u043e\u0442, \u0447\u0442\u043e \u043d\u0430\u043c \u0434\u0430\u044e\u0442 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u0435 \u0438\u043b\u0438 \u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u044c\u044f?<\/strong><\/p>\n<p>\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u044b\u043c \u0438 <a href=\"https:\/\/www.geeksforgeeks.org\/avl-tree-set-1-insertion\/\">\u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c<\/a>, Splay-\u0434\u0435\u0440\u0435\u0432\u043e (\u0438\u043b\u0438 <em>\u043a\u043e\u0441\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e<\/em>) \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <a href=\"http:\/\/en.wikipedia.org\/wiki\/Self-balancing_binary_search_tree\">\u0441\u0430\u043c\u043e\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u0443\u044e\u0449\u0438\u043c\u0441\u044f \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0430<\/a>. \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f splay-\u0434\u0435\u0440\u0435\u0432\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u043b\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f, \u0432 \u043a\u043e\u0440\u0435\u043d\u044c \u0434\u0435\u0440\u0435\u0432\u0430, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 O(1) \u043f\u0440\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u0435. \u0412\u0441\u044f \u0441\u0443\u0442\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u044e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0441\u044b\u043b\u043e\u043a (\u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 80% \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u043d\u0430 20% \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432). \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0441\u0435\u0431\u0435 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u044b \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u044b \u043a\u043b\u044e\u0447\u0435\u0439, \u0438 \u043b\u0438\u0448\u044c \u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0438\u0437 \u043d\u0438\u0445 \u043e\u0431\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u043e, \u0447\u0442\u043e \u0432\u0435\u0441\u044c\u043c\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445.<\/p>\n<p>\u0412\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e splay-\u0434\u0435\u0440\u0435\u0432\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 O(log n), \u0433\u0434\u0435 n &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0435\u0440\u0435\u0432\u0435. \u041b\u044e\u0431\u0430\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0422\u044d\u0442\u0430(n).<\/p>\n<h3>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430&nbsp;<\/h3>\n<p>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 splay-\u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435, \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f (\u0438\u0441\u043a\u043e\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u0440\u0435\u043d\u044c \u2014 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f splay). \u0415\u0441\u043b\u0438 \u043f\u043e\u0438\u0441\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f \u0443\u0441\u043f\u0435\u0445\u043e\u043c, \u0442\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0432\u0435\u0440\u0445 \u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u043c \u043a\u043e\u0440\u043d\u0435\u043c. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u0440\u043d\u0435\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0443\u0437\u0435\u043b, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0431\u044b\u043b \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u043e \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f NULL.<\/p>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0443\u0437\u043b\u0443 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u043b\u0443\u0447\u0430\u0438:<\/p>\n<p><strong>1.<\/strong> <strong>\u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c.<\/strong> \u041c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043a\u043e\u0440\u0435\u043d\u044c, \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u0443\u0437\u0435\u043b, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f, \u0443\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c.<\/p>\n<p><strong>2. Zig: \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u043a\u043e\u0440\u043d\u044e<\/strong><em> <\/em>(\u0443 \u0443\u0437\u043b\u0430 \u043d\u0435\u0442 \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f). \u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043a\u043e\u0440\u043d\u044f (\u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u0430\u0432\u044b\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442), \u043b\u0438\u0431\u043e \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c \u043b\u0435\u0432\u044b\u0439 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442).<\/p>\n<p>T1, T2 \u0438 T3 \u2014 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0434\u0435\u0440\u0435\u0432\u0430 \u0441 \u043a\u043e\u0440\u043d\u0435\u043c y (\u0441\u043b\u0435\u0432\u0430) \u0438\u043b\u0438 x (\u0441\u043f\u0440\u0430\u0432\u0430)<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>3. \u0423 <em>\u0443\u0437\u043b\u0430 \u0435\u0441\u0442\u044c \u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c, \u0438 \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c<\/em><\/strong>. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b:<\/p>\n<p><strong>\u0430) Zig-Zig \u0438 Zag-Zag.<\/strong> \u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u0434\u0432\u0430 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u0430 \u0432\u043f\u0440\u0430\u0432\u043e) \u0418\u041b\u0418 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0438 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c (\u0434\u0432\u0430 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u0430 \u0432\u043b\u0435\u0432\u043e).<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0431) Zig-Zag \u0438 Zag-Zig.<\/strong> \u0423\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443, \u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442 \u0432\u043b\u0435\u0432\u043e \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u043e\u043c \u0432\u043f\u0440\u0430\u0432\u043e) \u0418\u041b\u0418 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u043c \u043f\u0440\u0430\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f (\u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442 \u0432\u043f\u0440\u0430\u0432\u043e \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u043e\u043c \u0432\u043b\u0435\u0432\u043e).<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440:<\/strong><\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u043b\u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u043e\u0442\u0430 (splay) \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0432 \u043a\u043e\u0440\u0435\u043d\u044c, \u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u0443\u0440\u0430\u0432\u043d\u043e\u0432\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0434\u0435\u0440\u0435\u0432\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u0448\u0435, \u0432\u044b\u0441\u043e\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1.<\/p>\n<h4> \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:<\/h4>\n<h3>C++<\/h3>\n<pre><code class=\"cpp\">#include &lt;bits\/stdc++.h&gt;  using namespace std;   \/\/ An AVL tree node  class node  {  \tpublic:  \tint key;  \tnode *left, *right;  };   \/* Helper function that allocates  a new node with the given key and  \tNULL left and right pointers. *\/ node* newNode(int key)  {  \tnode* Node = new node();  \tNode-&gt;key = key;  \tNode-&gt;left = Node-&gt;right = NULL;  \treturn (Node);  }   \/\/ A utility function to right  \/\/ rotate subtree rooted with y  \/\/ See the diagram given above.  node *rightRotate(node *x)  {  \tnode *y = x-&gt;left;  \tx-&gt;left = y-&gt;right;  \ty-&gt;right = x;  \treturn y;  }   \/\/ A utility function to left  \/\/ rotate subtree rooted with x  \/\/ See the diagram given above.  node *leftRotate(node *x)  {  \tnode *y = x-&gt;right;  \tx-&gt;right = y-&gt;left;  \ty-&gt;left = x;  \treturn y;  }   \/\/ This function brings the key at  \/\/ root if key is present in tree.  \/\/ If key is not present, then it  \/\/ brings the last accessed item at  \/\/ root. This function modifies the  \/\/ tree and returns the new root  node *splay(node *root, int key)  {  \t\/\/ Base cases: root is NULL or  \t\/\/ key is present at root  \tif (root == NULL || root-&gt;key == key)  \t\treturn root;   \t\/\/ Key lies in left subtree  \tif (root-&gt;key &gt; key)  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;left == NULL) return root;   \t\t\/\/ Zig-Zig (Left Left)  \t\tif (root-&gt;left-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ First recursively bring the  \t\t\t\/\/ key as root of left-left  \t\t\troot-&gt;left-&gt;left = splay(root-&gt;left-&gt;left, key);   \t\t\t\/\/ Do first rotation for root,  \t\t\t\/\/ second rotation is done after else  \t\t\troot = rightRotate(root);  \t\t}  \t\telse if (root-&gt;left-&gt;key &lt; key) \/\/ Zig-Zag (Left Right)  \t\t{  \t\t\t\/\/ First recursively bring  \t\t\t\/\/ the key as root of left-right  \t\t\troot-&gt;left-&gt;right = splay(root-&gt;left-&gt;right, key);   \t\t\t\/\/ Do first rotation for root-&gt;left  \t\t\tif (root-&gt;left-&gt;right != NULL)  \t\t\t\troot-&gt;left = leftRotate(root-&gt;left);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;left == NULL)? root: rightRotate(root);  \t}  \telse \/\/ Key lies in right subtree  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;right == NULL) return root;   \t\t\/\/ Zag-Zig (Right Left)  \t\tif (root-&gt;right-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ Bring the key as root of right-left  \t\t\troot-&gt;right-&gt;left = splay(root-&gt;right-&gt;left, key);   \t\t\t\/\/ Do first rotation for root-&gt;right  \t\t\tif (root-&gt;right-&gt;left != NULL)  \t\t\t\troot-&gt;right = rightRotate(root-&gt;right);  \t\t}  \t\telse if (root-&gt;right-&gt;key &lt; key)\/\/ Zag-Zag (Right Right)  \t\t{  \t\t\t\/\/ Bring the key as root of  \t\t\t\/\/ right-right and do first rotation  \t\t\troot-&gt;right-&gt;right = splay(root-&gt;right-&gt;right, key);  \t\t\troot = leftRotate(root);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;right == NULL)? root: leftRotate(root);  \t}  }   \/\/ The search function for Splay tree.  \/\/ Note that this function returns the  \/\/ new root of Splay Tree. If key is  \/\/ present in tree then, it is moved to root.  node *search(node *root, int key)  {  \treturn splay(root, key);  }   \/\/ A utility function to print  \/\/ preorder traversal of the tree.  \/\/ The function also prints height of every node  void preOrder(node *root)  {  \tif (root != NULL)  \t{  \t\tcout&lt;&lt;root-&gt;key&lt;&lt;\" \";  \t\tpreOrder(root-&gt;left);  \t\tpreOrder(root-&gt;right);  \t}  }   \/* Driver code*\/ int main()  {  \tnode *root = newNode(100);  \troot-&gt;left = newNode(50);  \troot-&gt;right = newNode(200);  \troot-&gt;left-&gt;left = newNode(40);  \troot-&gt;left-&gt;left-&gt;left = newNode(30);  \troot-&gt;left-&gt;left-&gt;left-&gt;left = newNode(20);   \troot = search(root, 20);  \tcout &lt;&lt; \"Preorder traversal of the modified Splay tree is \\n\";  \tpreOrder(root);  \treturn 0;  }   \/\/ This code is contributed by rathbhupendra <\/code><\/pre>\n<h3>C<\/h3>\n<pre><code class=\"cs\">\/\/ The code is adopted from http:\/\/goo.gl\/SDH9hH  #include&lt;stdio.h&gt;  #include&lt;stdlib.h&gt;   \/\/ An AVL tree node  struct node  {  \tint key;  \tstruct node *left, *right;  };   \/* Helper function that allocates a new node with the given key and  \tNULL left and right pointers. *\/ struct node* newNode(int key)  {  \tstruct node* node = (struct node*)malloc(sizeof(struct node));  \tnode-&gt;key = key;  \tnode-&gt;left = node-&gt;right = NULL;  \treturn (node);  }   \/\/ A utility function to right rotate subtree rooted with y  \/\/ See the diagram given above.  struct node *rightRotate(struct node *x)  {  \tstruct node *y = x-&gt;left;  \tx-&gt;left = y-&gt;right;  \ty-&gt;right = x;  \treturn y;  }   \/\/ A utility function to left rotate subtree rooted with x  \/\/ See the diagram given above.  struct node *leftRotate(struct node *x)  {  \tstruct node *y = x-&gt;right;  \tx-&gt;right = y-&gt;left;  \ty-&gt;left = x;  \treturn y;  }   \/\/ This function brings the key at root if key is present in tree.  \/\/ If key is not present, then it brings the last accessed item at  \/\/ root. This function modifies the tree and returns the new root  struct node *splay(struct node *root, int key)  {  \t\/\/ Base cases: root is NULL or key is present at root  \tif (root == NULL || root-&gt;key == key)  \t\treturn root;   \t\/\/ Key lies in left subtree  \tif (root-&gt;key &gt; key)  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;left == NULL) return root;   \t\t\/\/ Zig-Zig (Left Left)  \t\tif (root-&gt;left-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ First recursively bring the key as root of left-left  \t\t\troot-&gt;left-&gt;left = splay(root-&gt;left-&gt;left, key);   \t\t\t\/\/ Do first rotation for root, second rotation is done after else  \t\t\troot = rightRotate(root);  \t\t}  \t\telse if (root-&gt;left-&gt;key &lt; key) \/\/ Zig-Zag (Left Right)  \t\t{  \t\t\t\/\/ First recursively bring the key as root of left-right  \t\t\troot-&gt;left-&gt;right = splay(root-&gt;left-&gt;right, key);   \t\t\t\/\/ Do first rotation for root-&gt;left  \t\t\tif (root-&gt;left-&gt;right != NULL)  \t\t\t\troot-&gt;left = leftRotate(root-&gt;left);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;left == NULL)? root: rightRotate(root);  \t}  \telse \/\/ Key lies in right subtree  \t{  \t\t\/\/ Key is not in tree, we are done  \t\tif (root-&gt;right == NULL) return root;   \t\t\/\/ Zag-Zig (Right Left)  \t\tif (root-&gt;right-&gt;key &gt; key)  \t\t{  \t\t\t\/\/ Bring the key as root of right-left  \t\t\troot-&gt;right-&gt;left = splay(root-&gt;right-&gt;left, key);   \t\t\t\/\/ Do first rotation for root-&gt;right  \t\t\tif (root-&gt;right-&gt;left != NULL)  \t\t\t\troot-&gt;right = rightRotate(root-&gt;right);  \t\t}  \t\telse if (root-&gt;right-&gt;key &lt; key)\/\/ Zag-Zag (Right Right)  \t\t{  \t\t\t\/\/ Bring the key as root of right-right and do first rotation  \t\t\troot-&gt;right-&gt;right = splay(root-&gt;right-&gt;right, key);  \t\t\troot = leftRotate(root);  \t\t}   \t\t\/\/ Do second rotation for root  \t\treturn (root-&gt;right == NULL)? root: leftRotate(root);  \t}  }   \/\/ The search function for Splay tree. Note that this function  \/\/ returns the new root of Splay Tree. If key is present in tree  \/\/ then, it is moved to root.  struct node *search(struct node *root, int key)  {  \treturn splay(root, key);  }<\/code><\/pre>\n<\/hr>\n<\/blockquote>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-315679","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/315679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=315679"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/315679\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=315679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=315679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=315679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}